Skip to content

Commit

Permalink
feat(multiplecheckbox): add disable others option (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloelisseo committed May 19, 2022
1 parent 2e20eed commit d0c18c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions example/src/forms.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@
"label": "Smart home function(TV links with IoT devices and sensors so you can turn on the lights, check fridge, run the robot etc. )",
"value": "smart_home"
},
{
"label": "None",
"value": "none",
"disableOthers": true
},
{
"label": "Other, please specify",
"value": "other"
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/Questions/MultipleCheckboxes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import Label from '../../Fields/Label'

import ReactMarkdown from 'react-markdown'

const disableOthers = (e) => {
Object.entries(e.target.form).forEach(([, v]) => {
if (e.target.checked === true) {
if (v.type === 'checkbox' && v.name === e.target.name) {
v.checked = false
v.disabled = true
}
e.target.disabled = false
e.target.checked = true
}
if (e.target.checked === false) {
if (v.type === 'checkbox' && v.name === e.target.name) {
v.disabled = false
}
}
})
}

const QuestionMultipleCheckboxes = ({ component, form, question, useForm }) => {
const { getValues, errors, register } = useForm
return (
Expand Down Expand Up @@ -70,6 +88,7 @@ const QuestionMultipleCheckboxes = ({ component, form, question, useForm }) => {
: () => true
}
})}
onChange={option.disableOthers && disableOthers}
/>
{option.src ? (
<img src={option.src} />
Expand Down

0 comments on commit d0c18c8

Please sign in to comment.