From 9007a39139751b6f7c4527bb26f17d722e43e17e Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Date: Sat, 30 Nov 2024 02:48:42 -0400 Subject: [PATCH] Add new FormFieldTypes --- src/Form/index.tsx | 67 +++++++++++++++++++++++++++++++++++++++++++--- src/Form/styled.ts | 37 +++++++++++++++++++++++-- src/Form/utils.ts | 9 +++++++ src/constants.ts | 2 ++ 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/src/Form/index.tsx b/src/Form/index.tsx index 04cbcaf..a7da955 100644 --- a/src/Form/index.tsx +++ b/src/Form/index.tsx @@ -1,12 +1,24 @@ -import { Button, DatePicker, Flex, NumberInput, Select, SelectItem, Text, TextInput } from '@tremor/react'; -import React, { useState } from 'react'; +import { + Button, + DatePicker, + Flex, + MultiSelect, + MultiSelectItem, + NumberInput, + Select, + SelectItem, + Text, + TextInput, + Textarea, +} from '@tremor/react'; +import { useState } from 'react'; import { Controller, useFieldArray, useForm } from 'react-hook-form'; import { VirtualSelect } from '../VirtualSelect'; import { FormFieldTypes } from '../constants'; import { StyledFieldGroup, StyledFormContainer } from '../styled'; import type { FormSettings } from './formSettings'; -import { StyledCheckbox } from './styled'; -import { extractData, getFieldBaseProps, onSelectChange } from './utils'; +import { StyledCheckbox, StyledFileInput } from './styled'; +import { extractData, getFieldBaseProps, onMultiSelectChange, onSelectChange } from './utils'; export const Form = ({ initialData, onSave, onCancel, saveLabel, columns = 3 }: FormSettings) => { const { @@ -122,10 +134,57 @@ export const Form = ({ initialData, onSave, onCancel, saveLabel, colum )} /> ); + case FormFieldTypes.MultiSelect: + return ( + ( + + field.value && (field.value as string[]).length > 0 + ? (field.value as string[]).includes( + c.value.toString(), + ) + : undefined, + ) + ?.map((c) => String(c.value)) ?? [] + } + onValueChange={(e) => onMultiSelectChange(e, field.onChange)} + disabled={disable || updatedField.disabled} + className='my-1' + ref={field.ref} + > + {(initialData[index].options ?? []).map((u) => ( + + {u.label} + + ))} + + )} + /> + ); case FormFieldTypes.Checkbox: return ; case FormFieldTypes.Number: return ; + case FormFieldTypes.TextArea: + return