import React, { ChangeEvent, InputHTMLAttributes, Ref } from 'react'; import classnames from 'classnames'; import { FormElement, FormElementProps } from './FormElement'; import { useEventCallback } from './hooks'; import { createFC } from './common'; /** * */ export type ToggleProps = { label?: string; required?: boolean; error?: FormElementProps['error']; cols?: number; name?: string; elementRef?: Ref; inputRef?: Ref; onValueChange?: (checked: boolean) => void; } & InputHTMLAttributes; /** * */ export const Toggle = createFC( (props) => { const { id, className, label, required, error, cols, elementRef, inputRef, onChange: onChange_, onValueChange, ...rprops } = props; const onChange = useEventCallback((e: ChangeEvent) => { onChange_?.(e); onValueChange?.(e.target.checked); }); const toggleClassNames = classnames( className, 'slds-checkbox_toggle slds-grid' ); const toggle = ( ); const formElemProps = { controlId: id, label, required, error, cols, elementRef, }; return {toggle}; }, { isFormElement: true } );