diff --git a/packages/design-system/src/components/Form/ToggleSwitch/Primitive/ToggleSwitchPrimitive.tsx b/packages/design-system/src/components/Form/ToggleSwitch/Primitive/ToggleSwitchPrimitive.tsx index 2bff188ffe..8c8b3c4e15 100644 --- a/packages/design-system/src/components/Form/ToggleSwitch/Primitive/ToggleSwitchPrimitive.tsx +++ b/packages/design-system/src/components/Form/ToggleSwitch/Primitive/ToggleSwitchPrimitive.tsx @@ -5,6 +5,7 @@ import classnames from 'classnames'; import { useId } from '../../../../useId'; import { CheckboxPrimitiveType } from '../../Checkbox/Primitive/CheckboxPrimitive'; +import { useControl } from '../../../../useControl'; import styles from './ToggleSwitchPrimitive.module.scss'; @@ -13,8 +14,8 @@ export type ToggleSwitchPrimitiveType = Omit }; const ToggleSwitchPrimitive = forwardRef( - ( - { + (props: Omit, ref: Ref) => { + const { id, label, defaultChecked, @@ -26,10 +27,15 @@ const ToggleSwitchPrimitive = forwardRef( isInline, onChange, ...rest - }: Omit, - ref: Ref, - ) => { + } = props; const switchId = useId(id, 'switch-'); + const controlled = useControl(props, { + onChangeKey: 'onChange', + valueKey: 'checked', + defaultValueKey: 'defaultChecked', + selector: e => e.target.checked, + defaultValue: false, + }); return ( - // If readonly, we return current check status ; Else we return opposite status as new status - onChange?.(Boolean(readOnly ? checked : !checked)) - } + checked={controlled.value} + onChange={e => controlled.onChange(e)} {...rest} ref={ref} /> diff --git a/packages/design-system/src/components/Form/ToggleSwitch/ToggleSwitch.tsx b/packages/design-system/src/components/Form/ToggleSwitch/ToggleSwitch.tsx index 4cc953c565..fc3f13bd12 100644 --- a/packages/design-system/src/components/Form/ToggleSwitch/ToggleSwitch.tsx +++ b/packages/design-system/src/components/Form/ToggleSwitch/ToggleSwitch.tsx @@ -1,19 +1,13 @@ import { forwardRef, Ref } from 'react'; -import { Mandatory } from '../../../types'; import ToggleSwitchPrimitive, { ToggleSwitchPrimitiveType, } from './Primitive/ToggleSwitchPrimitive'; -export type ToggleSwitchProps = Mandatory< - Omit, - 'onChange' ->; +export type ToggleSwitchProps = Omit; -const ToggleSwitch = forwardRef((props: ToggleSwitchProps, ref: Ref) => { +export const ToggleSwitch = forwardRef((props: ToggleSwitchProps, ref: Ref) => { return ; }); ToggleSwitch.displayName = 'ToggleSwitch'; - -export default ToggleSwitch; diff --git a/packages/design-system/src/components/Form/ToggleSwitch/UncontrolledToggleSwitch.tsx b/packages/design-system/src/components/Form/ToggleSwitch/UncontrolledToggleSwitch.tsx deleted file mode 100644 index b978f2f642..0000000000 --- a/packages/design-system/src/components/Form/ToggleSwitch/UncontrolledToggleSwitch.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import { Ref, forwardRef } from 'react'; - -import { useUncontrolled } from 'uncontrollable'; - -import ToggleSwitchPrimitive, { - ToggleSwitchPrimitiveType, -} from './Primitive/ToggleSwitchPrimitive'; - -export type UncontrolledToggleSwitchProps = Omit; - -const UncontrolledToggleSwitch = forwardRef( - (props: UncontrolledToggleSwitchProps, ref: Ref) => { - const controlledProps = useUncontrolled(props, { - checked: 'onChange', - }); - - return ; - }, -); - -UncontrolledToggleSwitch.displayName = 'UncontrolledToggleSwitch'; - -export default UncontrolledToggleSwitch; diff --git a/packages/design-system/src/components/Form/ToggleSwitch/index.ts b/packages/design-system/src/components/Form/ToggleSwitch/index.ts index e293a72934..9af62e8b0c 100644 --- a/packages/design-system/src/components/Form/ToggleSwitch/index.ts +++ b/packages/design-system/src/components/Form/ToggleSwitch/index.ts @@ -1,4 +1 @@ -import ToggleSwitch from './ToggleSwitch'; -import UncontrolledToggleSwitch from './UncontrolledToggleSwitch'; - -export { ToggleSwitch, UncontrolledToggleSwitch }; +export { ToggleSwitch } from './ToggleSwitch'; diff --git a/packages/design-system/src/components/Form/index.ts b/packages/design-system/src/components/Form/index.ts index 04676895b1..51e781af92 100644 --- a/packages/design-system/src/components/Form/index.ts +++ b/packages/design-system/src/components/Form/index.ts @@ -1,4 +1,4 @@ -import { ToggleSwitch, UncontrolledToggleSwitch } from './ToggleSwitch'; +import { ToggleSwitch } from './ToggleSwitch'; import Buttons from './Buttons'; import Datalist from './Field/Datalist'; import Input from './Field/Input'; diff --git a/packages/design-system/src/components/Switch/Switch.tsx b/packages/design-system/src/components/Switch/Switch.tsx index 3e05e0c507..7ab5dfa3ae 100644 --- a/packages/design-system/src/components/Switch/Switch.tsx +++ b/packages/design-system/src/components/Switch/Switch.tsx @@ -1,4 +1,4 @@ -import { useLayoutEffect, useRef, useState, useEffect } from 'react'; +import { MouseEvent, useLayoutEffect, useRef, useState, useEffect } from 'react'; import type { PropsWithChildren, HTMLAttributes } from 'react'; import classnames from 'classnames'; @@ -18,7 +18,7 @@ export type SwitchProps = PropsWithChildren, disabled?: boolean; readOnly?: boolean; // Redefine onChange prop - onChange?: (selectedValue: string) => void; + onChange?: (event: MouseEvent, selectedValue: string) => void; }; const Switch = ({ @@ -90,9 +90,9 @@ const Switch = ({ tabIndex={-1} role="radio" aria-checked={isChecked} - onClick={() => { + onClick={(e: MouseEvent) => { setRadio(v); - onChange?.(v); + onChange?.(e, v); }} value={v} key={i} diff --git a/packages/design-system/src/stories/form/Fieldset/Fieldset.stories.tsx b/packages/design-system/src/stories/form/Fieldset/Fieldset.stories.tsx index 41c3b1b621..75286114d1 100644 --- a/packages/design-system/src/stories/form/Fieldset/Fieldset.stories.tsx +++ b/packages/design-system/src/stories/form/Fieldset/Fieldset.stories.tsx @@ -7,7 +7,6 @@ import { Form, InlineMessageDestructive, InlineMessageInformation, - UncontrolledToggleSwitch, } from '../../../'; export default { @@ -178,11 +177,7 @@ export const ConditionalFieldset = () => { ref={register({ required: 'This field is required' })} /> - + {withUser && ( diff --git a/packages/design-system/src/stories/form/Form.stories.tsx b/packages/design-system/src/stories/form/Form.stories.tsx index 1ec35b1851..fc549cdc56 100644 --- a/packages/design-system/src/stories/form/Form.stories.tsx +++ b/packages/design-system/src/stories/form/Form.stories.tsx @@ -108,7 +108,7 @@ export const Default = () => { - { label="Checkbox" /> - + Reset diff --git a/packages/design-system/src/stories/form/ToggleSwitch/ToggleSwitch.stories.tsx b/packages/design-system/src/stories/form/ToggleSwitch/ToggleSwitch.stories.tsx index 935b2f365e..52d827d58e 100644 --- a/packages/design-system/src/stories/form/ToggleSwitch/ToggleSwitch.stories.tsx +++ b/packages/design-system/src/stories/form/ToggleSwitch/ToggleSwitch.stories.tsx @@ -5,7 +5,6 @@ import { InlineMessageInformation, StackVertical, ToggleSwitch, - UncontrolledToggleSwitch, } from '../../../../'; import { useForm } from 'react-hook-form'; @@ -16,45 +15,30 @@ export default { export const ToggleSwitchStates = () => ( - - + - - - - + + + ); export const ToggleSwitchInline = () => ( <> - + - ( export const ToggleSwitchWithLongLabel = () => (
- { return (
- + { /> )} - - + + - - + + - - +