React Checkbox Pro

Indeterminate State

How to use the indeterminate state in React Checkbox Pro

Indeterminate State

Learn how to use the indeterminate state for partial selection in checkbox groups.

Example

Select or deselect all items

Usage

Basic Indeterminate State

<Checkbox
  indeterminate={true}
  onChange={handleChange}
>
  Indeterminate Checkbox
</Checkbox>

With Custom Icon

<Checkbox
  indeterminate={true}
  indeterminateIcon={<MinusIcon />}
  onChange={handleChange}
>
  Custom Indeterminate Icon
</Checkbox>

Parent-Child Relationship

function ParentChildExample() {
  const [items, setItems] = useState([
    { id: 1, checked: false },
    { id: 2, checked: false }
  ]);
 
  const allChecked = items.every(item => item.checked);
  const someChecked = items.some(item => item.checked);
  const isIndeterminate = someChecked && !allChecked;
 
  return (
    <div>
      <Checkbox
        checked={allChecked}
        indeterminate={isIndeterminate}
        onChange={handleParentChange}
      >
        Parent
      </Checkbox>
      {items.map(item => (
        <Checkbox
          key={item.id}
          checked={item.checked}
          onChange={(checked) => handleItemChange(item.id, checked)}
        >
          Item {item.id}
        </Checkbox>
      ))}
    </div>
  );
}

Best Practices

  1. Use Cases:

    • Parent-child selection relationships
    • Bulk selection interfaces
    • Complex form states
  2. State Management:

    • Track individual item states
    • Calculate parent state based on children
    • Handle state changes efficiently
  3. Accessibility:

    • Use appropriate ARIA attributes
    • Provide clear visual indication
    • Include descriptive labels
  4. UX Considerations:

    • Clear visual feedback
    • Consistent behavior
    • Intuitive parent-child relationships

On this page