import React, { useCallback, type ComponentPropsWithoutRef, type ReactNode, } from 'react'; import { SegmentedControlItem, type SegmentedControlItemProps, } from './SegmentedControlItem'; import { segmentedControlsTheme, type SegmentedControlsVariants, } from './SegmentedControls.theme'; /** * Represents an option for a segmented control. */ export interface SegmentedControlsOption extends Omit { /** * The label of the item. */ label: ReactNode; /** * The value of the item. * */ value: string; } /** * Represents the props for the SegmentedControls component. */ export interface SegmentedControlsProps extends Omit, 'onChange'>, SegmentedControlsVariants { /** * An array of options for a segmented control component. */ options: SegmentedControlsOption[]; /** * An optional string value. */ value?: T; /** * Optional callback function to handle changes in state. */ onChange?: (v: T) => void; } /** * A component that renders segmented controls with customizable options. */ export function SegmentedControls({ className, options, value, type, size, onChange, ...props }: SegmentedControlsProps) { const { parent } = segmentedControlsTheme({ size, type }); /** * Handles the change event for a given option. */ const handleChange = useCallback( (option: T) => { if (!option) return; onChange?.(option); }, [onChange], ); return (
{options.map((option, index) => ( handleChange(option.value as T)} {...option} > {option.label} ))}
); }