Skip to content

Add CheckboxGroup component #798

@devongovett

Description

@devongovett

🙋 Feature Request

We have a Checkbox component already, but it would be nice to support a CheckboxGroup component to more easily create a group of checkboxes, similar to RadioGroup. It should support labeling the entire group of checkboxes, along with controlling the selection state of the checkboxes as a group rather than individually.

💁 Possible Solution

API should be similar to RadioGroup. The value prop could accept an array of values, matching the value prop on each individual Checkbox. Many other props can be applied to the group and propagate to each individual checkbox (e.g. disabled/readonly state, validation state, etc.).

import {ValueBase, InputBase, Validation, LabelableProps} from '@react-types/shared';

interface CheckboxGroupProps extends ValueBase<string[]>, InputBase, Validation, LabelableProps {
  /** Name for the checkboxes when submitting via HTML form. Applies to all checkboxes automatically. */
  name?: string
}

interface CheckboxGroupState {
  /** Current selected values. */
  value: string[],
  /** Sets the selected values. */
  setValue(value: string[]): void,
  /** Adds a value to the set of selected values. */
  addValue(value: string): void,
  /** Removes a value from the set of selected values. */
  removeValue(value: string): void,
  /** Toggles a value in the set of selected values. */
  toggleValue(value: string): void
}

declare function useCheckboxGroupState(props: CheckboxGroupProps): CheckboxGroupState;

interface CheckboxGroupAria {
  /** Props for the checkbox group label element. */
  labelProps: HTMLAttributes<HTMLElement>,
  /** Props for the group element */
  groupProps: HTMLAttributes<HTMLElement>
}

declare function useCheckboxGroup(props: CheckboxGroupProps): CheckboxGroupAria;

We may also want to support additional Spectrum-specific options like layout orientation, emphasized visual state, labeling visual options, etc.

Example:

<CheckboxGroup label="Favorite sports">
  <Checkbox value="running">Running</Checkbox>
  <Checkbox value="swimming">Swimming</Checkbox>
  <Checkbox value="soccer">Soccer</Checkbox>
</CheckboxGroup>

HTML structure:

<div role="group" aria-labelledby="label-id">
  <div id="label-id">Favorite Sports</div>
  <!-- insert checkboxes here -->
</div>

In terms of CSS, we should be able to use the Spectrum FieldGroup CSS for this (same as used in RadioGroup).

🧢 Your Company/Team

RSP

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions