The ToggleGroup component provides a group of toggle buttons that work together. It's designed for selecting between multiple options, filtering, and any interface where users need to choose from a set of related options.
import { ToggleGroup } from '@dashflowx/core'export default function ToggleGroupExample() {return (<ToggleGroupitems={[{ id: '1', title: 'Option 1', value: 'option1' },{ id: '2', title: 'Option 2', value: 'option2' },{ id: '3', title: 'Option 3', value: 'option3' }]}defaultValue="option1"/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | '' | Additional CSS classes |
disabled | boolean | false | Whether the toggle group is disabled |
fullwidth | boolean | false | Whether the toggle group takes full width |
lable | string | 'Toggle Group' | Label for the toggle group |
required | boolean | false | Whether the field is required |
items | Array | [] | Array of toggle items |
defaultValue | string | '' | Default selected value |
prefixElement | JSX.Element | - | Element to display before the toggle group |
sufixElement | JSX.Element | - | Element to display after the toggle group |
sucessMsg | string | - | Success message to display |
errorMsg | string | - | Error message to display |
import { ToggleGroup } from '@dashflowx/core'export default function BasicToggleGroup() {return (<ToggleGroupitems={[{ id: '1', title: 'Option 1', value: 'option1' },{ id: '2', title: 'Option 2', value: 'option2' },{ id: '3', title: 'Option 3', value: 'option3' }]}defaultValue="option1"/>)}
Preview:
import { ToggleGroup } from '@dashflowx/core'export default function CustomStyledToggleGroup() {return (<ToggleGroupclassName="border border-blue-500 rounded-lg p-4"items={[{ id: '1', title: 'Option 1', value: 'option1' },{ id: '2', title: 'Option 2', value: 'option2' },{ id: '3', title: 'Option 3', value: 'option3' }]}defaultValue="option1"/>)}
Preview:
import { ToggleGroup } from '@dashflowx/core'export default function ToggleGroupWithLabel() {return (<ToggleGrouplable="Select view"items={[{ id: '1', title: 'Grid', value: 'grid' },{ id: '2', title: 'List', value: 'list' },{ id: '3', title: 'Card', value: 'card' }]}defaultValue="grid"/>)}
Preview:
import { ToggleGroup } from '@dashflowx/core'export default function RequiredToggleGroup() {return (<ToggleGrouplable="Choose option"required={true}items={[{ id: '1', title: 'Yes', value: 'yes' },{ id: '2', title: 'No', value: 'no' }]}defaultValue="yes"/>)}
Preview:
import { ToggleGroup } from '@dashflowx/core'export default function DisabledToggleGroup() {return (<ToggleGrouplable="Disabled options"disabled={true}items={[{ id: '1', title: 'Option 1', value: 'option1' },{ id: '2', title: 'Option 2', value: 'option2' }]}defaultValue="option1"/>)}
Preview:
import { ToggleGroup } from '@dashflowx/core'export default function FullWidthToggleGroup() {return (<ToggleGrouplable="Full width options"fullwidth={true}items={[{ id: '1', title: 'Option 1', value: 'option1' },{ id: '2', title: 'Option 2', value: 'option2' },{ id: '3', title: 'Option 3', value: 'option3' }]}defaultValue="option1"/>)}
Preview:
const viewSelection = (<ToggleGrouplable="View"items={[{ id: '1', title: 'Grid', value: 'grid' },{ id: '2', title: 'List', value: 'list' },{ id: '3', title: 'Card', value: 'card' }]}defaultValue="grid"/>);
const filterOptions = (<ToggleGrouplable="Filter"items={[{ id: '1', title: 'All', value: 'all' },{ id: '2', title: 'Active', value: 'active' },{ id: '3', title: 'Inactive', value: 'inactive' }]}defaultValue="all"/>);
const sizeSelection = (<ToggleGrouplable="Size"items={[{ id: '1', title: 'S', value: 'small' },{ id: '2', title: 'M', value: 'medium' },{ id: '3', title: 'L', value: 'large' }]}defaultValue="medium"/>);
const themeSelection = (<ToggleGrouplable="Theme"items={[{ id: '1', title: 'Light', value: 'light' },{ id: '2', title: 'Dark', value: 'dark' },{ id: '3', title: 'Auto', value: 'auto' }]}defaultValue="light"/>);
Use clear and descriptive labels for toggle groups.
// ✅ Good - Clear labels<ToggleGroup lable="Select view mode" />// ❌ Avoid - Unclear labels<ToggleGroup lable="Options" />
Use appropriate options for different contexts.
// ✅ Good - Appropriate options<ToggleGroup items={[{ id: '1', title: 'Grid', value: 'grid' },{ id: '2', title: 'List', value: 'list' }]} />// ❌ Avoid - Inappropriate options<ToggleGroup items={[{ id: '1', title: 'Maybe', value: 'maybe' },{ id: '2', title: 'Sometimes', value: 'sometimes' }]} />
Maintain consistent styling across toggle groups.
// ✅ Good - Consistent styling<ToggleGroup className="standard-toggle-group-style" />// ❌ Avoid - Inconsistent styling<ToggleGroup className="random-styles" />
Ensure toggle group labels are accessible to all users.
// ✅ Good - Accessible labels<ToggleGroup lable="Select display mode" />// ❌ Avoid - Not accessible<ToggleGroup lable="Options" />
Set appropriate default values for toggle groups.
// ✅ Good - Appropriate default<ToggleGroup defaultValue="grid" />// ❌ Avoid - Inappropriate default<ToggleGroup defaultValue="unknown" />
You can customize the toggle group appearance using CSS classes:
<ToggleGroup className="border border-gray-300 rounded-lg p-4" />
Use different themes for different contexts:
// Light theme<ToggleGroup className="bg-white border-gray-200" />// Dark theme<ToggleGroup className="bg-gray-900 border-gray-700 text-white" />// Brand theme<ToggleGroup className="bg-blue-50 border-blue-200" />
Use different sizes for different emphasis levels:
// Small toggle group<ToggleGroup className="text-sm" />// Standard toggle group<ToggleGroup className="text-base" />// Large toggle group<ToggleGroup className="text-lg" />
Load toggle group options dynamically:
const [options, setOptions] = useState([]);useEffect(() => {// Load options from APIloadOptions().then(setOptions);}, []);
Add validation to toggle group selections:
const [hasError, setHasError] = useState(false);<ToggleGrouplable="Required selection"required={true}errorMsg={hasError ? "Please select an option" : ""}/>
Show success messages for toggle group selections:
const [showSuccess, setShowSuccess] = useState(false);<ToggleGrouplable="Selection made"sucessMsg={showSuccess ? "Selection saved successfully" : ""}/>
- Toggle: For individual toggle buttons
- Button: For button-style toggles
- RadioGroup: For radio button groups
- Form: For form containers
- Label: For form labels
For the complete API reference and advanced usage patterns, see the ToggleGroup API documentation.
