-
Notifications
You must be signed in to change notification settings - Fork 142
feat(v3): CheckboxGroup, FieldCheckboxGroup #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a955d8c
feat(wip/FieldCheckboxGroup): CheckboxGroup, stories and small style …
ntatoud 4358f78
wip: CheckboxGroup
ntatoud 1e8fd98
fix: improve checkbox-group api
ntatoud b79f345
wip: working version
ntatoud 31ae710
feat: wip: Easy to use version
ntatoud 637b0af
fix: remove FieldCheckboxGroup for now
ntatoud d11738a
refactor: simple checkbox group
ntatoud ba9382b
feat(field-checkbox-group): add stories and tests
ntatoud a08db54
fix: small issues
ntatoud 09c02e9
fix: remove unused types
ntatoud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { zodResolver } from '@hookform/resolvers/zod'; | ||
| import { Meta } from '@storybook/react-vite'; | ||
| import { useForm } from 'react-hook-form'; | ||
| import { z } from 'zod'; | ||
|
|
||
| import { zu } from '@/lib/zod/zod-utils'; | ||
|
|
||
| import { FormFieldController } from '@/components/form'; | ||
| import { onSubmit } from '@/components/form/docs.utils'; | ||
| import { FieldCheckboxGroup } from '@/components/form/field-checkbox-group'; | ||
| import { Button } from '@/components/ui/button'; | ||
|
|
||
| import { Form, FormField, FormFieldHelper, FormFieldLabel } from '../'; | ||
|
|
||
| export default { | ||
| title: 'Form/FieldCheckboxGroup', | ||
| component: FieldCheckboxGroup, | ||
| } satisfies Meta<typeof FieldCheckboxGroup>; | ||
|
|
||
| const zFormSchema = () => | ||
| z.object({ | ||
| bears: zu.array.nonEmpty( | ||
| z.string().array(), | ||
| 'Please select your favorite bearstronaut' | ||
| ), | ||
| }); | ||
|
|
||
| const formOptions = { | ||
| mode: 'onBlur', | ||
| resolver: zodResolver(zFormSchema()), | ||
| defaultValues: { | ||
| bears: [] as string[], | ||
| }, | ||
| } as const; | ||
|
|
||
| const astrobears = [ | ||
| { value: 'bearstrong', label: 'Bearstrong' }, | ||
| { value: 'pawdrin', label: 'Buzz Pawdrin' }, | ||
| { value: 'grizzlyrin', label: 'Yuri Grizzlyrin' }, | ||
| ]; | ||
|
|
||
| export const Default = () => { | ||
| const form = useForm(formOptions); | ||
|
|
||
| return ( | ||
| <Form {...form} onSubmit={onSubmit}> | ||
| <div className="flex flex-col gap-4"> | ||
| <FormField> | ||
| <FormFieldLabel>Bearstronaut</FormFieldLabel> | ||
| <FormFieldHelper>Select your favorite bearstronaut</FormFieldHelper> | ||
| <FormFieldController | ||
| type="checkbox-group" | ||
| control={form.control} | ||
| name="bears" | ||
| options={astrobears} | ||
| /> | ||
| </FormField> | ||
| <div> | ||
| <Button type="submit">Submit</Button> | ||
| </div> | ||
| </div> | ||
| </Form> | ||
| ); | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { ComponentProps, ReactNode } from 'react'; | ||
| import { Controller, FieldPath, FieldValues } from 'react-hook-form'; | ||
|
|
||
| import { cn } from '@/lib/tailwind/utils'; | ||
|
|
||
| import { FormFieldError } from '@/components/form'; | ||
| import { useFormField } from '@/components/form/form-field'; | ||
| import { FieldProps } from '@/components/form/form-field-controller'; | ||
| import { Checkbox, CheckboxProps } from '@/components/ui/checkbox'; | ||
| import { CheckboxGroup } from '@/components/ui/checkbox-group'; | ||
|
|
||
| type CheckboxOption = Omit<CheckboxProps, 'children' | 'render'> & { | ||
| label: ReactNode; | ||
| }; | ||
| export type FieldCheckboxGroupProps< | ||
| TFIeldValues extends FieldValues = FieldValues, | ||
| TName extends FieldPath<TFIeldValues> = FieldPath<TFIeldValues>, | ||
| > = FieldProps< | ||
| TFIeldValues, | ||
| TName, | ||
| { | ||
| type: 'checkbox-group'; | ||
| options: Array<CheckboxOption>; | ||
| containerProps?: ComponentProps<'div'>; | ||
| } & ComponentProps<typeof CheckboxGroup> | ||
| >; | ||
|
|
||
| export const FieldCheckboxGroup = < | ||
| TFIeldValues extends FieldValues = FieldValues, | ||
| TName extends FieldPath<TFIeldValues> = FieldPath<TFIeldValues>, | ||
| >( | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| props: FieldCheckboxGroupProps<TFIeldValues, TName> | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) => { | ||
| const { | ||
| type, | ||
| name, | ||
| control, | ||
| defaultValue, | ||
| disabled, | ||
| shouldUnregister, | ||
| containerProps, | ||
| options, | ||
| ...rest | ||
| } = props; | ||
|
|
||
| const ctx = useFormField(); | ||
|
|
||
| return ( | ||
| <Controller | ||
| name={name} | ||
| control={control} | ||
| defaultValue={defaultValue} | ||
| disabled={disabled} | ||
| shouldUnregister={shouldUnregister} | ||
| render={({ field: { value, onChange, ...field }, fieldState }) => { | ||
| const isInvalid = fieldState.error ? true : undefined; | ||
| return ( | ||
| <div | ||
| {...containerProps} | ||
| className={cn('', containerProps?.className)} | ||
| > | ||
| <CheckboxGroup | ||
| id={ctx.id} | ||
| aria-invalid={isInvalid} | ||
| aria-labelledby={ctx.labelId} | ||
| aria-describedby={ | ||
| !fieldState.error | ||
| ? `${ctx.descriptionId}` | ||
| : `${ctx.descriptionId} ${ctx.errorId}` | ||
| } | ||
| value={value} | ||
| onValueChange={onChange} | ||
| {...rest} | ||
| > | ||
| {options.map(({ label, ...option }) => ( | ||
| <Checkbox | ||
| key={option.value} | ||
| {...option} | ||
| aria-invalid={isInvalid} | ||
| {...field} | ||
| > | ||
| {label} | ||
| </Checkbox> | ||
| ))} | ||
| </CheckboxGroup> | ||
| <FormFieldError /> | ||
| </div> | ||
| ); | ||
| }} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { Meta } from '@storybook/react-vite'; | ||
| import { useState } from 'react'; | ||
|
|
||
| import { Checkbox } from '@/components/ui/checkbox'; | ||
| import { useCheckboxGroup } from '@/components/ui/checkbox.utils'; | ||
| import { CheckboxGroup } from '@/components/ui/checkbox-group'; | ||
| export default { | ||
| title: 'CheckboxGroup', | ||
| component: CheckboxGroup, | ||
| } satisfies Meta<typeof CheckboxGroup>; | ||
|
|
||
| const astrobears = [ | ||
| { value: 'bearstrong', label: 'Bearstrong', disabled: false }, | ||
| { value: 'pawdrin', label: 'Buzz Pawdrin', disabled: false }, | ||
| { value: 'grizzlyrin', label: 'Yuri Grizzlyrin', disabled: true }, | ||
| ] as const; | ||
|
|
||
| export const Default = () => { | ||
| return ( | ||
| <CheckboxGroup> | ||
| {astrobears.map((option) => ( | ||
| <Checkbox key={option.value} value={option.value}> | ||
| {option.label} | ||
| </Checkbox> | ||
| ))} | ||
| </CheckboxGroup> | ||
| ); | ||
| }; | ||
|
|
||
| export const DefaultValue = () => { | ||
| return ( | ||
| <CheckboxGroup defaultValue={['bearstrong']}> | ||
| {astrobears.map((option) => ( | ||
| <Checkbox key={option.value} value={option.value}> | ||
| {option.label} | ||
| </Checkbox> | ||
| ))} | ||
| </CheckboxGroup> | ||
| ); | ||
| }; | ||
|
|
||
| export const Disabled = () => { | ||
| return ( | ||
| <CheckboxGroup disabled> | ||
| {astrobears.map((option) => ( | ||
| <Checkbox key={option.value} value={option.value}> | ||
| {option.label} | ||
| </Checkbox> | ||
| ))} | ||
| </CheckboxGroup> | ||
| ); | ||
| }; | ||
|
|
||
| export const DisabledOption = () => { | ||
| return ( | ||
| <CheckboxGroup> | ||
| {astrobears.map((option) => ( | ||
| <Checkbox | ||
| key={option.value} | ||
| value={option.value} | ||
| disabled={option.disabled} | ||
| > | ||
| {option.label} | ||
| </Checkbox> | ||
| ))} | ||
| </CheckboxGroup> | ||
| ); | ||
| }; | ||
|
|
||
| export const ParentCheckbox = () => { | ||
| const [value, setValue] = useState<string[]>([]); | ||
|
|
||
| return ( | ||
| <CheckboxGroup | ||
| value={value} | ||
| onValueChange={setValue} | ||
| allValues={astrobears.map((bear) => bear.value)} | ||
| > | ||
| <Checkbox name="astrobears" parent> | ||
| Astrobears | ||
| </Checkbox> | ||
| <div className="flex flex-col gap-1 pl-4"> | ||
| {astrobears.map((option) => ( | ||
| <Checkbox key={option.value} value={option.value}> | ||
| {option.label} | ||
| </Checkbox> | ||
| ))} | ||
| </div> | ||
| </CheckboxGroup> | ||
| ); | ||
| }; | ||
|
|
||
| const nestedBears = [ | ||
| { value: 'bearstrong', label: 'Bearstrong', children: undefined }, | ||
| { value: 'pawdrin', label: 'Buzz Pawdrin', children: undefined }, | ||
| { | ||
| value: 'grizzlyrin', | ||
| label: 'Yuri Grizzlyrin', | ||
| children: [ | ||
| { value: 'mini-grizzlyrin-1', label: 'Mini grizzlyrin 1' }, | ||
| { value: 'mini-grizzlyrin-2', label: 'Mini grizzlyrin 2' }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| export const NestedParentCheckbox = () => { | ||
| const { | ||
| main: { indeterminate, ...main }, | ||
| nested, | ||
| } = useCheckboxGroup(nestedBears, { | ||
| nestedKey: 'grizzlyrin', | ||
| mainDefaultValue: [], | ||
| nestedDefaultValue: [], | ||
| }); | ||
|
|
||
| return ( | ||
| <CheckboxGroup {...main}> | ||
| <Checkbox name="astrobears" parent indeterminate={indeterminate}> | ||
| Astrobears | ||
| </Checkbox> | ||
| <div className="space-y-1 pl-4"> | ||
| {nestedBears.map((bear) => { | ||
| if (!bear.children) { | ||
| return ( | ||
| <Checkbox key={bear.value} value={bear.value}> | ||
| {bear.label} | ||
| </Checkbox> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <CheckboxGroup key={bear.value} {...nested}> | ||
| <Checkbox value={bear.value} parent> | ||
| {bear.label} | ||
| </Checkbox> | ||
| <div className="space-y-1 pl-4"> | ||
| {bear.children.map((bear) => ( | ||
| <Checkbox key={bear.value} value={bear.value}> | ||
| {bear.label} | ||
| </Checkbox> | ||
| ))} | ||
| </div> | ||
| </CheckboxGroup> | ||
| ); | ||
| })} | ||
| </div> | ||
| </CheckboxGroup> | ||
| ); | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { CheckboxGroup as CheckboxGroupPrimitive } from '@base-ui-components/react/checkbox-group'; | ||
|
|
||
| import { cn } from '@/lib/tailwind/utils'; | ||
|
|
||
| export type BaseCheckboxGroupProps = CheckboxGroupPrimitive.Props; | ||
|
|
||
| export function CheckboxGroup(props: BaseCheckboxGroupProps) { | ||
| return ( | ||
| <CheckboxGroupPrimitive | ||
| {...props} | ||
| className={cn('flex flex-col items-start gap-1', props?.className)} | ||
| /> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.