Skip to content

Commit 919a129

Browse files
committed
feat: update CheckboxInput to manage checked state and improve form validation integration
1 parent 91b6e1c commit 919a129

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/components/form/CheckboxInput.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,33 @@ export type CheckboxInputProps =
1414

1515
export const CheckboxInput: React.FC<CheckboxInputProps> = (props) => {
1616

17-
const [checked, setChecked] = React.useState<CheckedState>(props.initialValue ?? "indeterminate");
18-
1917
const {
2018
title,
2119
description,
2220
formValidation = {
2321
valid: true,
2422
notValidMessage: null,
25-
setValue: (value: CheckedState) => {
26-
},
23+
setValue: (value: CheckedState) => undefined,
2724
},
2825
...rest
2926
} = props
3027

28+
const [checked, setChecked] = React.useState<CheckedState>(props.initialValue ?? "indeterminate");
29+
30+
React.useEffect(() => {
31+
formValidation.setValue(checked)
32+
}, [checked])
33+
3134
return <>
3235
{!!title ? <InputLabel children={title}/> : null}
3336
{!!description ? <InputDescription children={description}/> : null}
3437

35-
<div {...mergeCode0Props(`input ${!formValidation?.valid ? "input--not-valid" : ""} checkbox-input`, {})}>
38+
<div {...mergeCode0Props(`input ${!formValidation?.valid ? "input--not-valid" : ""} checkbox-input`, {})} onClick={() => setChecked(prevState => !prevState)}>
3639

37-
<Checkbox defaultChecked={checked} {...mergeCode0Props("checkbox-input__button", {
38-
...rest, onCheckedChange: (value: CheckedState) => {
39-
if (rest.onCheckedChange) rest.onCheckedChange!!(value)
40-
setChecked(value)
41-
formValidation.setValue(value)
42-
}
43-
})}>
40+
<Checkbox checked={checked} defaultChecked={checked} {...mergeCode0Props("checkbox-input__button", rest)}>
4441
<CheckboxIndicator className={"checkbox-input__indicator"}>
45-
{checked === "indeterminate" && <IconMinus size={10}/>}
46-
{checked === true && <IconCheck size={10}/>}
42+
{checked === "indeterminate" && <IconMinus size={16}/>}
43+
{checked === true && <IconCheck size={16}/>}
4744
</CheckboxIndicator>
4845
</Checkbox>
4946

src/components/form/Input.style.scss

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,23 @@
302302
&__button {
303303

304304
aspect-ratio: 1/1;
305-
width: 17px;
306-
height: 17px;
305+
width: variables.$xl;
306+
height: variables.$xl;
307307
display: flex;
308308
align-items: center;
309309
justify-content: center;
310+
border-radius: variables.$xxs;
311+
cursor: pointer;
310312

311313
& {
312314
@include helpers.fontStyle();
313315
@include box.boxActive();
314316
@include box.box();
315317
@include helpers.disabled();
316-
@include helpers.borderRadius();
318+
}
319+
320+
&[data-state="checked"] {
321+
@include box.boxActiveStyle(variables.$info);
317322
}
318323

319324
}

0 commit comments

Comments
 (0)