Skip to content

Commit 7395c1a

Browse files
committed
feat: enhance Toggle component to support controlled state management
1 parent e609822 commit 7395c1a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Common/Toggle/Toggle.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ const Toggle = ({
3333
shouldToggleValueOnLabelClick = false,
3434
isLoading = false,
3535
value = CHECKBOX_VALUE.CHECKED,
36+
isControlled = false,
3637
...props
3738
}) => {
3839
const [active, setActive] = React.useState(selected)
3940

4041
useEffectAfterMount(() => {
41-
if (typeof onSelect === 'function') {
42+
if (typeof onSelect === 'function' && !isControlled) {
4243
if (active !== selected) {
4344
onSelect(active)
4445
}
@@ -51,7 +52,11 @@ const Toggle = ({
5152

5253
function handleClick() {
5354
if (!disabled) {
54-
setActive((active) => !active)
55+
if (isControlled) {
56+
onSelect(!active)
57+
} else {
58+
setActive((active) => !active)
59+
}
5560
}
5661
}
5762

0 commit comments

Comments
 (0)