Skip to content

Commit

Permalink
Add new FormFieldTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarfgq authored and geoperez committed Nov 30, 2024
1 parent 6124281 commit 9007a39
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
67 changes: 63 additions & 4 deletions src/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -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 = <T, TData>({ initialData, onSave, onCancel, saveLabel, columns = 3 }: FormSettings<T, TData>) => {
const {
Expand Down Expand Up @@ -122,10 +134,57 @@ export const Form = <T, TData>({ initialData, onSave, onCancel, saveLabel, colum
)}
/>
);
case FormFieldTypes.MultiSelect:
return (
<Controller
name={`table.${index}.value`}
rules={{
required: !updatedField.notRequired,
}}
control={control}
render={({ field }) => (
<MultiSelect
value={
initialData[index].options
?.filter((c) =>
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) => (
<MultiSelectItem key={u.value} value={u.value.toString()}>
{u.label}
</MultiSelectItem>
))}
</MultiSelect>
)}
/>
);
case FormFieldTypes.Checkbox:
return <StyledCheckbox {...fieldProps} />;
case FormFieldTypes.Number:
return <NumberInput {...fieldProps} />;
case FormFieldTypes.TextArea:
return <Textarea {...fieldProps} className='max-w-full' />;
case FormFieldTypes.File:
return (
<StyledFileInput
id='file'
name='file'
type='file'
className='mt-1'
accept='.pdf'
/>
);
default:
return <TextInput {...fieldProps} type='text' />;
}
Expand Down
37 changes: 35 additions & 2 deletions src/Form/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const InputBase = tw.input`
focus:bg-gray-200
disabled:text-[#999999]
focus:outline-none
text-tremor-content
text-tremor-content
dark:text-dark-tremor-content
bg-tremor-background
bg-tremor-background
dark:bg-dark-tremor-background
text-xs
`;
Expand All @@ -25,3 +25,36 @@ export const StyledCheckbox = tw(InputBase)`
mr-4
ml-3
`;

export const StyledFileInput = tw(InputBase)`
text-sm
rounded-lg
file:-my-1.5
file:-ml-2.5
file:h-[34px]
file:cursor-pointer
file:rounded-l-tremor-small
file:rounded-r-none
file:border-0
file:px-3
file:py-1.5
file:text-tremor-default
file:outline-none
file:border-solid
file:border-tremor-border
file:bg-tremor-background-muted
file:text-tremor-content
hover:file:bg-tremor-background-subtle/80
file:dark:border-dark-tremor-border
file:dark:bg-dark-tremor-background-muted
hover:file:dark:bg-dark-tremor-background-subtle/30
file:[border-inline-end-width:1px]
file:[margin-inline-end:0.75rem]
focus:outline-none
disabled:pointer-events-none
file:disabled:pointer-events-none
file:disabled:bg-tremor-background-subtle
file:disabled:text-tremor-content
file:disabled:dark:border-gray-700
file:disabled:dark:bg-dark-tremor-background-subtle
`;
9 changes: 9 additions & 0 deletions src/Form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ export const onSelectChange = (event: string, callback: (x: string) => void, def
callback(event);
if (defaultCallback) defaultCallback(event);
};

export const onMultiSelectChange = (
event: string[],
callback: (x: string[]) => void,
defaultCallback?: (x: string[]) => void,
) => {
callback(event);
if (defaultCallback) defaultCallback(event);
};
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export enum FormFieldTypes {
VirtualSelect = 'virtualselect',
Checkbox = 'checkbox',
TextArea = 'textarea',
MultiSelect = 'multiselect',
File = 'file',
}

export type ClassNameComponent = {
Expand Down

0 comments on commit 9007a39

Please sign in to comment.