1- import React , { InputHTMLAttributes , Ref } from 'react' ;
1+ import React , { ChangeEvent , InputHTMLAttributes , Ref } from 'react' ;
22import classnames from 'classnames' ;
33import { FormElement , FormElementProps } from './FormElement' ;
4- import { useFormElementId } from './hooks' ;
4+ import { useEventCallback , useFormElementId } from './hooks' ;
55import { createFC } from './common' ;
66
77/**
@@ -15,6 +15,7 @@ export type ToggleProps = {
1515 name ?: string ;
1616 elementRef ?: Ref < HTMLDivElement > ;
1717 inputRef ?: Ref < HTMLInputElement > ;
18+ onValueChange ?: ( checked : boolean ) => void ;
1819} & InputHTMLAttributes < HTMLInputElement > ;
1920
2021/**
@@ -31,9 +32,15 @@ export const Toggle = createFC<ToggleProps, { isFormElement: boolean }>(
3132 cols,
3233 elementRef,
3334 inputRef,
35+ onChange : onChange_ ,
36+ onValueChange,
3437 ...rprops
3538 } = props ;
3639 const id = useFormElementId ( id_ , 'toggle' ) ;
40+ const onChange = useEventCallback ( ( e : ChangeEvent < HTMLInputElement > ) => {
41+ onChange_ ?.( e ) ;
42+ onValueChange ?.( e . target . checked ) ;
43+ } ) ;
3744 const toggleClassNames = classnames (
3845 className ,
3946 'slds-checkbox_toggle slds-grid'
@@ -47,6 +54,7 @@ export const Toggle = createFC<ToggleProps, { isFormElement: boolean }>(
4754 type = 'checkbox'
4855 aria-describedby = 'toggle-desc'
4956 { ...rprops }
57+ onChange = { onChange }
5058 />
5159 < span className = 'slds-checkbox_faux_container' aria-live = 'assertive' >
5260 < span className = 'slds-checkbox_faux' />
0 commit comments