Skip to content

Commit 38d7e98

Browse files
committed
add onChangeValue handler on Toggle component
1 parent 3bde8bf commit 38d7e98

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/scripts/Toggle.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { InputHTMLAttributes, Ref } from 'react';
1+
import React, { ChangeEvent, InputHTMLAttributes, Ref } from 'react';
22
import classnames from 'classnames';
33
import { FormElement, FormElementProps } from './FormElement';
4-
import { useFormElementId } from './hooks';
4+
import { useEventCallback, useFormElementId } from './hooks';
55
import { 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' />

stories/Toggle.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const meta: ComponentMeta<typeof Toggle> = {
99
component: Toggle,
1010
argTypes: {
1111
onChange: { action: 'change' },
12+
onValueChange: { action: 'valueChange' },
1213
},
1314
};
1415
export default meta;

0 commit comments

Comments
 (0)