Frontendx
README.md
SOLUTION.md
main.tsxentry
App.tsx
App.css
index.html
vite.config.ts
Installed
react@^19.2.0
react-dom@^19.2.0

Compound Components Pattern

Description

Implement a ToggleGroup component using the compound components pattern. The parent manages state and provides context, while child components access the shared state.

Example 1:

<ToggleGroup value={selected} onChange={setSelected}>
  <ToggleGroup.Item value="a">Option A</ToggleGroup.Item>
  <ToggleGroup.Item value="b">Option B</ToggleGroup.Item>
  <ToggleGroup.Item value="c">Option C</ToggleGroup.Item>
</ToggleGroup>

Example 2:

// Parent ToggleGroup provides context
// Children ToggleGroup.Item consume context to know if they're selected
// Clicking an item updates the parent's value

Constraints

  • Create a ToggleGroup that accepts value and onChange props
  • Create ToggleGroup.Item that shows as selected when its value matches the group's value
  • Use React.createContext to share state between ToggleGroup and its children
  • Items should be clickable and update the group's value
  • The component should look like a button group where one can be selected
Use React.createContext to share state between parent and children
Test Results

Compound Components Pattern

Implement the compound components pattern to create a flexible toggle group.