Skip to content
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

[Checkbox Group] Parent checkbox API suggestion #1067

Open
vladmoroz opened this issue Dec 12, 2024 · 3 comments
Open

[Checkbox Group] Parent checkbox API suggestion #1067

vladmoroz opened this issue Dec 12, 2024 · 3 comments
Labels
breaking change checkbox group The React component. component: checkbox This is the name of the generic UI component, not the React module!

Comments

@vladmoroz
Copy link
Contributor

vladmoroz commented Dec 12, 2024

I've found that the relationship between <Checkbox parent> and <CheckboxGroup allValues={...}> isn't obvious/easily memorable. You need to maintain in your head that two props on separate components are needed to make it work.

My proposal, instead of:

<CheckboxGroup allValues={['a', 'b', 'c']} value={['a']} onValueChange={...}>
  <Checkbox.Root parent>
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="a">
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="b">
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="c">
    <Checkbox.Indicator />
  </Checkbox.Root>
</CheckboxGroup>

Set child names on the parent checkbox directly via a group prop:

<CheckboxGroup value={["a"]} onValueChange={...}>
  <Checkbox.Root group={['a', 'b', 'c']} >
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="a">
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="b">
    <Checkbox.Indicator />
  </Checkbox.Root>

  <Checkbox.Root name="c">
    <Checkbox.Indicator />
  </Checkbox.Root>
</CheckboxGroup>

CheckboxGroup would pass down the current group value to the parent checkbox which then would be responsible for rendering itself as ticked/unticked/indeterminate, and the group prop is memorable because of its relationship to CheckboxGroup. Also, one fewer prop to have in the API.

I thought about groupValues too, but decided that I'm not a fan of how the values part misleads you in thinking that you might need to set a value on the child checkboxes instead of the name prop; also, group is shorter and feels somewhat more declarative

Search keywords:

@vladmoroz vladmoroz added component: checkbox This is the name of the generic UI component, not the React module! checkbox group The React component. breaking change labels Dec 12, 2024
@mj12albert
Copy link
Member

mj12albert commented Dec 12, 2024

IIRC the reason for this is that putting allValues={['a', 'b', 'c']} (by any name) one level higher than <Checkbox.Root name="a"> is necessary to SSR the checked state correctly

(I think if it weren't for this, the group value could even just be derived from the names of the individual checkboxes instead of being a prop)

@vladmoroz
Copy link
Contributor Author

IIRC the reason for this is that putting allValues={['a', 'b', 'c']} (by any name) one level higher than <Checkbox.Root name="a"> is necessary to SSR the checked state correctly

(I think if it weren't for this, the group value could even just be derived from the names of the individual checkboxes instead of being a prop)

A <Checkbox> with group prop could still read the current value from the parent <CheckboxGroup> context even when rendering on the server, no?

@atomiks
Copy link
Contributor

atomiks commented Dec 13, 2024

Checkbox.Root needs to read allValues, but if this is via a prop like group it may be able to still work with some implementation changes of how children checkboxes receive getChildProps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change checkbox group The React component. component: checkbox This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

3 participants