From 3b98127ace92d9279b346f646ae4b38245d1b9af Mon Sep 17 00:00:00 2001 From: Jeroen Branje Date: Mon, 26 Feb 2024 14:53:34 +0100 Subject: [PATCH] feat(design-system): Add drag and drop form atom Signed-off-by: Jeroen Branje --- .../DragAndDrop/DragAndDropField.stories.tsx | 32 ++++++++ .../Form/DragAndDrop/DragAndDropField.tsx | 81 +++++++++++++++++++ .../DragAndDropFieldProps.ui.test.tsx | 25 ++++++ .../Atoms/Form/DragAndDrop/index.ts | 1 + .../src/components/Atoms/Form/index.ts | 1 + apps/design-system/src/index.ts | 2 +- 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.stories.tsx create mode 100644 apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.tsx create mode 100644 apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropFieldProps.ui.test.tsx create mode 100644 apps/design-system/src/components/Atoms/Form/DragAndDrop/index.ts diff --git a/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.stories.tsx b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.stories.tsx new file mode 100644 index 00000000..3f71ed36 --- /dev/null +++ b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.stories.tsx @@ -0,0 +1,32 @@ +import { Meta, Story } from '@storybook/react' + +import { DragAndDropField } from './DragAndDropField' + +export default { + component: DragAndDropField, + title: 'Components/Form/DragAndDropField', +} as Meta + +const FileFieldTemplate: Story = ({ label, name, value, error, inputRef, onChange, onDrop }) => ( + +) + +export const FileFieldComponent = FileFieldTemplate.bind({}) + +FileFieldComponent.args = { + label: 'label', + name: 'name', + value: [], + error: 'ERROR', + inputRef: null, + onChange: () => {}, + onDrop: () => {}, +} diff --git a/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.tsx b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.tsx new file mode 100644 index 00000000..4d9f3ab0 --- /dev/null +++ b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropField.tsx @@ -0,0 +1,81 @@ +import { PhotoIcon } from '@heroicons/react/24/outline' +import { isEmpty } from 'ramda' +import { FC, JSXElementConstructor, ReactElement, useState } from 'react' +import { RefCallBack } from 'react-hook-form' + +interface DragAndDropFieldProps { + label: string | ReactElement> + name: string + value: any + error?: string + inputRef: RefCallBack + onDrop: (event: React.DragEvent) => void + onChange: (event: React.ChangeEvent) => void +} + +export const DragAndDropField: FC = ({ + label, + name, + value, + error = '', + inputRef, + onDrop, + onChange, +}) => { + const [isDragActive, setIsDragActive] = useState(false) + + const handleDrop = (event: React.DragEvent) => { + event.preventDefault() + setIsDragActive(false) + onDrop(event) + } + + const handleDragEnter = () => { + setIsDragActive(true) + } + + const handleDragLeave = () => { + setIsDragActive(false) + } + + return ( + <> + +
event.preventDefault()} + > +
+
+
+ {value} + {!isEmpty(error) &&

{error}

} + + ) +} diff --git a/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropFieldProps.ui.test.tsx b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropFieldProps.ui.test.tsx new file mode 100644 index 00000000..76027347 --- /dev/null +++ b/apps/design-system/src/components/Atoms/Form/DragAndDrop/DragAndDropFieldProps.ui.test.tsx @@ -0,0 +1,25 @@ +import { render, screen } from '@testing-library/react' + +import { DragAndDropField } from './DragAndDropField' + +describe('atoms/DragAndDropField', () => { + describe('render', () => { + it('should render DragAndDropField', () => { + // when ... we rendering component + render( + {}} + name="NAME" + onDrop={() => {}} + onChange={() => {}} + />, + ) + + // then ... it should return as expected + const element = screen.getByText('LABEL') + expect(element).toBeInTheDocument() + }) + }) +}) diff --git a/apps/design-system/src/components/Atoms/Form/DragAndDrop/index.ts b/apps/design-system/src/components/Atoms/Form/DragAndDrop/index.ts new file mode 100644 index 00000000..a84e479c --- /dev/null +++ b/apps/design-system/src/components/Atoms/Form/DragAndDrop/index.ts @@ -0,0 +1 @@ +export { DragAndDropField } from './DragAndDropField' diff --git a/apps/design-system/src/components/Atoms/Form/index.ts b/apps/design-system/src/components/Atoms/Form/index.ts index e3607b7a..de8d0ff9 100644 --- a/apps/design-system/src/components/Atoms/Form/index.ts +++ b/apps/design-system/src/components/Atoms/Form/index.ts @@ -1,3 +1,4 @@ export { Checkbox, Checkboxes } from './Checkbox' +export { DragAndDropField } from './DragAndDrop' export { TextField } from './Input' export { TextareaField } from './Textarea' diff --git a/apps/design-system/src/index.ts b/apps/design-system/src/index.ts index 710c7824..ff1c24c6 100644 --- a/apps/design-system/src/index.ts +++ b/apps/design-system/src/index.ts @@ -3,7 +3,7 @@ export { Button } from './components/Atoms/Button' export { Card } from './components/Atoms/Card' export { ContactPerson } from './components/Atoms/ContactPerson' export { Date } from './components/Atoms/Date' -export { Checkbox, Checkboxes, TextField, TextareaField } from './components/Atoms/Form' +export { Checkbox, Checkboxes, DragAndDropField, FileField, TextField, TextareaField } from './components/Atoms/Form' export { Grid, GridRow } from './components/Atoms/Grid' export { Heading } from './components/Atoms/Heading' export { HeadingWithTooltip } from './components/Atoms/HeadingWithTooltip'