Skip to content

Commit 5196624

Browse files
chore: refactor and improve fix
1 parent 0886ae8 commit 5196624

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

example/src/forms/Contact/forms.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,25 @@
187187
"disableOthers": true
188188
}
189189
]
190-
}
190+
},
191+
"dependentQuestions": [
192+
{
193+
"condition": "others",
194+
"question": {
195+
"name": "test",
196+
"type": "input",
197+
"label": "Confirm password",
198+
"placeholder": "Confirm password",
199+
"errorMessages": {
200+
"required": "This field is required",
201+
"doesNotMatch": "The passwords do not match"
202+
},
203+
"registerConfig": {
204+
"required": true
205+
}
206+
}
207+
}
208+
]
191209
},
192210
{
193211
"name": "age",

src/Questions/MultipleCheckboxes/index.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,6 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => {
5656
>
5757
{question.config &&
5858
question.config.options.map((option) => {
59-
const checkBoxConfig = {
60-
'data-testid': 'question-singleCheckbox',
61-
id: option.name,
62-
'aria-describedby': 'error_message_' + question.name,
63-
name: question.name,
64-
value: option.value,
65-
defaultChecked: question.defaultCheckedValues?.find(
66-
(defaultValue) => defaultValue === option.value
67-
),
68-
...register(question.name, {
69-
...question.registerConfig,
70-
validate: {
71-
minimumLen: question.registerConfig.minimumLen
72-
? () =>
73-
getValues()[question.name] &&
74-
getValues()[question.name].length >=
75-
question.registerConfig.minimumLen
76-
: () => true,
77-
maximumLen: question.registerConfig.maximumLen
78-
? () =>
79-
getValues()[question.name] &&
80-
getValues()[question.name].length <=
81-
question.registerConfig.maximumLen
82-
: () => true
83-
}
84-
})
85-
}
86-
8759
return (
8860
<div
8961
sx={{
@@ -98,10 +70,34 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => {
9870
alignItems: 'center'
9971
}}
10072
>
101-
{option.disableOthers ?
102-
<Checkbox {...checkBoxConfig} onChange={disableOthers} /> :
103-
<Checkbox {...checkBoxConfig} />
104-
}
73+
<Checkbox
74+
data-testid="question-singleCheckbox"
75+
id={option.name}
76+
aria-describedby={'error_message_' + question.name}
77+
name={question.name}
78+
value={option.value}
79+
defaultChecked={question.defaultCheckedValues?.find(
80+
(defaultValue) => defaultValue === option.value
81+
)}
82+
{...register(question.name, {
83+
...question.registerConfig,
84+
onChange: option.disableOthers && disableOthers,
85+
validate: {
86+
minimumLen: question.registerConfig.minimumLen
87+
? () =>
88+
getValues()[question.name] &&
89+
getValues()[question.name].length >=
90+
question.registerConfig.minimumLen
91+
: () => true,
92+
maximumLen: question.registerConfig.maximumLen
93+
? () =>
94+
getValues()[question.name] &&
95+
getValues()[question.name].length <=
96+
question.registerConfig.maximumLen
97+
: () => true
98+
}
99+
})}
100+
/>
105101
{option.src ? (
106102
<img src={option.src} />
107103
) : (

0 commit comments

Comments
 (0)