From 57a30a1cc3ef1a1d9b18617b3ef1faebc818f035 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 3 Sep 2024 23:34:21 +0200 Subject: [PATCH] [FEAT] generize the form model --- front2/src/components/form/FormGroup.tsx | 11 +- front2/src/components/form/FormInput.tsx | 37 ++++++ front2/src/components/form/FormNumber.tsx | 62 ++++++++++ front2/src/components/form/FormSelect.tsx | 42 +++++++ .../components/form/FormSelectMultiple.tsx | 42 +++++++ front2/src/components/form/FormTextarea.tsx | 37 ++++++ front2/src/components/form/Formidable.tsx | 2 + front2/src/components/popup/ConfirmPopUp.tsx | 1 - .../src/components/popup/TrackEditPopUp.tsx | 111 ++++++------------ .../src/components/select/SelectMultiple.tsx | 19 ++- front2/src/components/select/SelectSingle.tsx | 10 +- 11 files changed, 275 insertions(+), 99 deletions(-) create mode 100644 front2/src/components/form/FormInput.tsx create mode 100644 front2/src/components/form/FormNumber.tsx create mode 100644 front2/src/components/form/FormSelect.tsx create mode 100644 front2/src/components/form/FormSelectMultiple.tsx create mode 100644 front2/src/components/form/FormTextarea.tsx diff --git a/front2/src/components/form/FormGroup.tsx b/front2/src/components/form/FormGroup.tsx index d0bdcb5..6e84095 100644 --- a/front2/src/components/form/FormGroup.tsx +++ b/front2/src/components/form/FormGroup.tsx @@ -1,12 +1,7 @@ -import { ReactElement, ReactNode } from 'react'; +import { ReactNode } from 'react'; -import { Button, Flex, IconButton, Text, Tooltip } from '@chakra-ui/react'; -import { - MdErrorOutline, - MdHelpOutline, - MdLabelImportant, - MdRefresh, -} from 'react-icons/md'; +import { Flex, Text } from '@chakra-ui/react'; +import { MdErrorOutline, MdHelpOutline, MdRefresh } from 'react-icons/md'; export type FormGroupProps = { error?: ReactNode; diff --git a/front2/src/components/form/FormInput.tsx b/front2/src/components/form/FormInput.tsx new file mode 100644 index 0000000..64fcc94 --- /dev/null +++ b/front2/src/components/form/FormInput.tsx @@ -0,0 +1,37 @@ +import { RefObject } from 'react'; + +import { Input } from '@chakra-ui/react'; + +import { FormGroup } from '@/components/form/FormGroup'; +import { UseFormidableReturn } from '@/components/form/Formidable'; + +export type FormInputProps = { + form: UseFormidableReturn; + variableName: string; + ref?: RefObject; + label?: string; + placeholder?: string; + isRequired?: boolean; +}; + +export const FormInput = ({ + form, + variableName, + ref, + placeholder, + ...rest +}: FormInputProps) => { + return ( + form.restoreValue({ [variableName]: true })} + {...rest} + > + form.setValues({ [variableName]: e.target.value })} + /> + + ); +}; diff --git a/front2/src/components/form/FormNumber.tsx b/front2/src/components/form/FormNumber.tsx new file mode 100644 index 0000000..1af79aa --- /dev/null +++ b/front2/src/components/form/FormNumber.tsx @@ -0,0 +1,62 @@ +import { RefObject } from 'react'; + +import { + Input, + NumberDecrementStepper, + NumberIncrementStepper, + NumberInput, + NumberInputField, + NumberInputProps, + NumberInputStepper, +} from '@chakra-ui/react'; + +import { FormGroup } from '@/components/form/FormGroup'; +import { UseFormidableReturn } from '@/components/form/Formidable'; + +export type FormNumberProps = Pick< + NumberInputProps, + 'step' | 'defaultValue' | 'min' | 'max' +> & { + form: UseFormidableReturn; + variableName: string; + ref?: RefObject; + label?: string; + placeholder?: string; + isRequired?: boolean; +}; + +export const FormNumber = ({ + form, + variableName, + ref, + placeholder, + step, + min, + max, + defaultValue, + ...rest +}: FormNumberProps) => { + return ( + form.restoreValue({ [variableName]: true })} + {...rest} + > + form.setValues({ [variableName]: value })} + step={step} + defaultValue={defaultValue} + min={min} + max={max} + > + + + + + + + + ); +}; diff --git a/front2/src/components/form/FormSelect.tsx b/front2/src/components/form/FormSelect.tsx new file mode 100644 index 0000000..ffc6075 --- /dev/null +++ b/front2/src/components/form/FormSelect.tsx @@ -0,0 +1,42 @@ +import { RefObject } from 'react'; + +import { FormGroup } from '@/components/form/FormGroup'; +import { UseFormidableReturn } from '@/components/form/Formidable'; +import { SelectSingle } from '@/components/select/SelectSingle'; + +export type FormSelectProps = { + form: UseFormidableReturn; + variableName: string; + ref?: RefObject; + label?: string; + placeholder?: string; + isRequired?: boolean; + options?: object[]; + keyInputValue?: string; +}; + +export const FormSelect = ({ + form, + variableName, + ref, + placeholder, + options, + keyInputValue = 'name', + ...rest +}: FormSelectProps) => { + return ( + form.restoreValue({ [variableName]: true })} + {...rest} + > + form.setValues({ [variableName]: value })} + keyValue={keyInputValue} + /> + + ); +}; diff --git a/front2/src/components/form/FormSelectMultiple.tsx b/front2/src/components/form/FormSelectMultiple.tsx new file mode 100644 index 0000000..f127b4c --- /dev/null +++ b/front2/src/components/form/FormSelectMultiple.tsx @@ -0,0 +1,42 @@ +import { RefObject } from 'react'; + +import { FormGroup } from '@/components/form/FormGroup'; +import { UseFormidableReturn } from '@/components/form/Formidable'; +import { SelectMultiple } from '@/components/select/SelectMultiple'; + +export type FormSelectMultipleProps = { + form: UseFormidableReturn; + variableName: string; + ref?: RefObject; + label?: string; + placeholder?: string; + isRequired?: boolean; + options?: object[]; + keyInputValue?: string; +}; + +export const FormSelectMultiple = ({ + form, + variableName, + ref, + placeholder, + options, + keyInputValue = 'name', + ...rest +}: FormSelectMultipleProps) => { + return ( + form.restoreValue({ [variableName]: true })} + {...rest} + > + form.setValues({ [variableName]: value })} + keyValue={keyInputValue} + /> + + ); +}; diff --git a/front2/src/components/form/FormTextarea.tsx b/front2/src/components/form/FormTextarea.tsx new file mode 100644 index 0000000..473f8dc --- /dev/null +++ b/front2/src/components/form/FormTextarea.tsx @@ -0,0 +1,37 @@ +import { RefObject } from 'react'; + +import { Textarea } from '@chakra-ui/react'; + +import { FormGroup } from '@/components/form/FormGroup'; +import { UseFormidableReturn } from '@/components/form/Formidable'; + +export type FormTextareaProps = { + form: UseFormidableReturn; + variableName: string; + ref?: RefObject; + label?: string; + placeholder?: string; + isRequired?: boolean; +}; + +export const FormTextarea = ({ + form, + variableName, + ref, + placeholder, + ...rest +}: FormTextareaProps) => { + return ( + form.restoreValue({ [variableName]: true })} + {...rest} + > +