-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added field descriptions & updated tests (#2914)
- Loading branch information
1 parent
49869ff
commit 82d8388
Showing
31 changed files
with
406 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 25 additions & 14 deletions
39
packages/ui/src/components/organisms/Form/DynamicForm/fields/CheckboxField/CheckboxField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import { Checkbox } from '@/components/atoms'; | ||
import { Checkbox, Label } from '@/components/atoms'; | ||
import { useDynamicForm } from '../../context'; | ||
import { useElement, useField } from '../../hooks/external'; | ||
import { useRequired } from '../../hooks/external/useRequired'; | ||
import { useMountEvent } from '../../hooks/internal/useMountEvent'; | ||
import { useUnmountEvent } from '../../hooks/internal/useUnmountEvent'; | ||
import { FieldDescription } from '../../layouts/FieldDescription'; | ||
import { FieldErrors } from '../../layouts/FieldErrors'; | ||
import { FieldLayout } from '../../layouts/FieldLayout'; | ||
import { TDynamicFormField } from '../../types'; | ||
import { ICommonFieldParams, TDynamicFormField } from '../../types'; | ||
import { useStack } from '../FieldList/providers/StackProvider'; | ||
|
||
export const CheckboxField: TDynamicFormField = ({ element }) => { | ||
export const CheckboxField: TDynamicFormField<ICommonFieldParams> = ({ element }) => { | ||
useMountEvent(element); | ||
useUnmountEvent(element); | ||
|
||
const { label } = element.params || {}; | ||
const { stack } = useStack(); | ||
const { id } = useElement(element, stack); | ||
const { value, onChange, onFocus, onBlur, disabled } = useField<boolean | undefined>( | ||
element, | ||
stack, | ||
); | ||
const { values } = useDynamicForm(); | ||
const isRequired = useRequired(element, values); | ||
|
||
return ( | ||
<FieldLayout element={element} layout="horizontal"> | ||
<Checkbox | ||
id={id} | ||
checked={Boolean(value)} | ||
onCheckedChange={onChange} | ||
disabled={disabled} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
/> | ||
<div className="flex flex-col"> | ||
<div className="flex flex-row flex-nowrap items-center gap-2"> | ||
<Checkbox | ||
id={id} | ||
checked={Boolean(value)} | ||
onCheckedChange={onChange} | ||
disabled={disabled} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
/> | ||
<Label id={`${id}-label`} htmlFor={`${id}`}> | ||
{`${isRequired ? `${label}` : `${label} (optional)`} `} | ||
</Label> | ||
</div> | ||
<FieldDescription element={element} /> | ||
<FieldErrors element={element} /> | ||
</FieldLayout> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.