import { type FC, type ChangeEventHandler, type ReactNode, type ReactElement, forwardRef, type ForwardRefRenderFunction, } from 'react'; import { Fieldset, type FieldsetProps, type LabelProps, type RadioProps, Label, Radio, } from '../../../atoms'; import type { TooltipProps } from '../../modals'; import styles from './switch.module.scss'; type SwitchItemProps = Omit & Pick & { /** * The item id. */ id: string; /** * Is the item selected? */ isSelected?: boolean; /** * The label used to describe the switch item. */ label: ReactNode; /** * The event handler on value change. */ onSwitch: ChangeEventHandler; /** * The item value. */ value: string; }; /** * SwitchItem component. */ const SwitchItem: FC = ({ className = '', id, isDisabled = false, isSelected = false, label, name, onSwitch, value, ...props }) => { const itemClass = `${styles.item} ${className}`; return ( ); }; export type SwitchOption = Pick; export type SwitchProps = Omit & { /** * The switch items. */ items: [SwitchOption, SwitchOption]; /** * The switch group name. */ name: string; /** * A function to handle selection change. */ onSwitch: ChangeEventHandler; /** * A tooltip to display before switch options. */ tooltip?: ReactElement; /** * The selected item value. */ value: SwitchOption['value']; }; const SwitchWithRef: ForwardRefRenderFunction< HTMLFieldSetElement, SwitchProps > = ( { className = '', isDisabled = false, items, name, onSwitch, tooltip, value, ...props }, ref ) => { const fieldsetClass = `${styles.fieldset} ${className}`; return (
{tooltip}
{items.map((item) => ( ))}
); }; /** * Switch component. */ export const Switch = forwardRef(SwitchWithRef);