Skip to content

Commit

Permalink
SHARED(Frontend): Include a file drop zone by default on desktop with…
Browse files Browse the repository at this point in the history
… the FileUpload component
  • Loading branch information
pkoivisto committed Aug 26, 2024
1 parent 48acb91 commit 39f848f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
5 changes: 5 additions & 0 deletions frontend/packages/shared/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Released]

## [1.10.8] - 2024-08-26

### Changed
- Include a file drop zone by default on desktop with the file upload component

## [1.10.7] - 2024-08-07

### Added
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opetushallitus/kieli-ja-kaantajatutkinnot.shared",
"version": "1.10.7",
"version": "1.10.8",
"description": "Shared Frontend Package",
"exports": {
"./components": "./src/components/index.tsx",
Expand Down
44 changes: 25 additions & 19 deletions frontend/packages/shared/src/components/FileUpload/FileUpload.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
@use '../../styles/abstracts/colors';
@use '../../styles/abstracts/maps';

.custom-fileupload input {
width: fit-content;
border: 1px dashed colors.$color-secondary;
font-size: 120%;
display: none;
}
.custom-fileupload {
input {
display: none;
}

.custom-fileupload.file-upload-error input {
border: 1px dashed colors.$color-red-500;
}
&.file-upload-error {
.file-dropzone {
border-top: 1px dashed colors.$color-red-500;
border-right: 1px dashed colors.$color-red-500;
border-bottom: 1px dashed colors.$color-red-500;
p {
color: colors.$color-red-500;
}
}
}

.custom-fileupload.file-upload-error input::file-selector-button {
background: colors.$color-red-500;
}

.custom-fileupload input::file-selector-button {
background: colors.$color-secondary;
border: 1px solid colors.$color-primary;
color: colors.$color-primary;
padding: 10px;
}
.file-dropzone {
align-content: center;
height: 100%;
border-top: 1px dashed colors.$color-secondary;
border-right: 1px dashed colors.$color-secondary;
border-bottom: 1px dashed colors.$color-secondary;
padding-left: 1vw;
padding-right: 10vw;
cursor: copy;
}
}
33 changes: 26 additions & 7 deletions frontend/packages/shared/src/components/FileUpload/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,67 @@
import FileUploadIcon from '@mui/icons-material/FileUpload';
import { Button } from '@mui/material';
import { ChangeEvent, FC } from 'react';
import { ChangeEvent, DragEvent, FC } from 'react';
import './FileUpload.scss';
import { Color, Variant } from '../../enums';
import { Text } from '../../components';
import { useWindowProperties } from 'hooks';

type FileUploadProps = {
accept?: string;
error?: boolean;
onChange: (files: FileList) => void;
labelText: string;
buttonText: string;
dropZoneText: string;
};

export const FileUpload: FC<FileUploadProps> = ({
accept,
onChange,
error,
labelText,
buttonText,
dropZoneText,
}) => {
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.files) {
onChange(event.target.files);
}
};
const { isPhone } = useWindowProperties();

return (
<div
className={
error ? 'custom-fileupload file-upload-error' : 'custom-fileupload'
}
className={`columns custom-fileupload ${
error ? 'file-upload-error' : ''
}`}
>
<Button
component="label"
variant={Variant.Contained}
color={error ? Color.Error : Color.Secondary}
startIcon={<FileUploadIcon />}
>
{labelText}
{buttonText}
<input
id="custom-fileupload"
accept={accept}
type="file"
onChange={handleChange}
/>
</Button>
{!isPhone && (
<div
className="grow file-dropzone"
onDragOver={(e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
}}
onDrop={(e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
onChange(e.dataTransfer.files);
}}
>
<Text>{dropZoneText}</Text>
</div>
)}
</div>
);
};

0 comments on commit 39f848f

Please sign in to comment.