Skip to content

Commit

Permalink
fix: improve multi-checkbox to disabled when selected option with a t…
Browse files Browse the repository at this point in the history
…rue disableOthers (#278)

* fix: send option on multicheckbox to disable only when selected

* chore: change to ternary

---------

Co-authored-by: Jesus Sabroso <[email protected]>
  • Loading branch information
Xexuline and Jesus Sabroso authored Jan 24, 2025
1 parent 3e9d5f8 commit 62cc1d4
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Questions/MultipleCheckboxes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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) {
const disableOthers = (option) => (e) => {
Object.entries(e?.target?.form || []).forEach(([, v]) => {
if (e.target.checked === true && option.value === e.target.value) {
if (v.type === 'checkbox' && v.name === e.target.name && v !== e.target) {
v.checked = false
v.disabled = true
}
e.target.disabled = false
e.target.checked = true
}
if (e.target.checked === false) {
if (e?.target?.checked === false) {
if (v.type === 'checkbox' && v.name === e.target.name) {
v.disabled = false
}
Expand Down Expand Up @@ -71,7 +71,7 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => {
}}
>
<Checkbox
data-testid="question-singleCheckbox"
data-testid='question-singleCheckbox'
id={option.name}
aria-describedby={'error_message_' + question.name}
name={question.name}
Expand All @@ -81,7 +81,9 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => {
)}
{...register(question.name, {
...question.registerConfig,
onChange: option.disableOthers && disableOthers,
onChange: option.disableOthers
? disableOthers(option)
: undefined,
validate: {
minimumLen: question.registerConfig.minimumLen
? () =>
Expand Down

0 comments on commit 62cc1d4

Please sign in to comment.