Skip to content

CheckboxGroup with indeterminate/select all checkbox #5220

Open
@snowystinger

Description

@snowystinger

Discussed in #5215

Originally posted by richbachman October 6, 2023
Hi team. First off, great work on the react-aria-components work. I'm really enjoying seeing it grow. I'm working on using the Checkbox and CheckboxGroup components, and am struggling with coming up with a way to have a select all option with an indeterminate state. Here's what I have so far, but something just isn't clicking. I didn't see an example on the doc site, but do you have one worked up somewhere else?

Thanks in advance.

const CheckboxGroupExample = () => {
  const [selected, setSelected] = React.useState(["subscribe", "test"]);

  const allSelected = selected.length === 3;
  const indeterminate =
    selected.length < 3 && selected.length > 0 && !allSelected;

  return (
    <>
      <CheckboxGroup
        errorText="This input does not meet the requirements."
        helpText="Help text that should hopefully just be one line but at most may stretch to two lines."
        label="Group label"
        value={selected}
        onChange={setSelected}
      >
        <Checkbox
          value="select-all"
          isIndeterminate={indeterminate}
          isSelected={allSelected}
        >
          Select all
        </Checkbox>
        <Checkbox
          value="subscribe"
        >
          Subscribe
        </Checkbox>
        <Checkbox
          value="test"
        >
          Test
        </Checkbox>
        <Checkbox
          value="another"
        >
          Another
        </Checkbox>
      </CheckboxGroup>
      <p>{selected}</p>
    </>
  );
};
```</div>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions