From 62cc1d471348f1ff1e4537e35bb4a72bf943c8c3 Mon Sep 17 00:00:00 2001 From: Jesus Sabroso Date: Fri, 24 Jan 2025 10:24:32 +0100 Subject: [PATCH] fix: improve multi-checkbox to disabled when selected option with a true disableOthers (#278) * fix: send option on multicheckbox to disable only when selected * chore: change to ternary --------- Co-authored-by: Jesus Sabroso --- src/Questions/MultipleCheckboxes/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Questions/MultipleCheckboxes/index.js b/src/Questions/MultipleCheckboxes/index.js index 0d3dfe4f..b890d82a 100644 --- a/src/Questions/MultipleCheckboxes/index.js +++ b/src/Questions/MultipleCheckboxes/index.js @@ -8,9 +8,9 @@ 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 @@ -18,7 +18,7 @@ const disableOthers = (e) => { 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 } @@ -71,7 +71,7 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => { }} > { )} {...register(question.name, { ...question.registerConfig, - onChange: option.disableOthers && disableOthers, + onChange: option.disableOthers + ? disableOthers(option) + : undefined, validate: { minimumLen: question.registerConfig.minimumLen ? () =>