From 7a9a8db402a533efbf5b74cde9e63db475c302f2 Mon Sep 17 00:00:00 2001 From: Gbacc Date: Wed, 19 Jul 2023 14:26:52 +0200 Subject: [PATCH] feat(TDOPS-4687): checkbox widget can disable a single value in list (#4809) --- .changeset/orange-lobsters-poke.md | 5 ++++ .../fields/CheckBox/CheckBoxes.component.js | 8 ++--- .../UIForm/fields/CheckBox/CheckBoxes.test.js | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 .changeset/orange-lobsters-poke.md diff --git a/.changeset/orange-lobsters-poke.md b/.changeset/orange-lobsters-poke.md new file mode 100644 index 00000000000..9de2884c3bd --- /dev/null +++ b/.changeset/orange-lobsters-poke.md @@ -0,0 +1,5 @@ +--- +'@talend/react-forms': minor +--- + +Forms - Allow to disable a single checkbox for checkbox widget list diff --git a/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.component.js b/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.component.js index 70008e3fcb6..b5516bae000 100644 --- a/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.component.js +++ b/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.component.js @@ -3,7 +3,7 @@ import SimpleCheckBox from './SimpleCheckBox.component'; import FieldTemplate from '../FieldTemplate'; import { generateDescriptionId, generateErrorId } from '../../Message/generateId'; -function getValues(value = [], itemValue, checked) { +function getValues(itemValue, checked, value = []) { if (checked) { return value.concat(itemValue); } @@ -35,7 +35,7 @@ export default function CheckBoxes(props) { {titleMap.map((item, index) => ( onChange(event, { schema: payload.schema, - value: getValues(value, item.value, payload.value), + value: getValues(item.value, payload.value, value), }) } onFinish={(event, payload) => onFinish(event, { schema: payload.schema, - value: getValues(value, item.value, payload.value), + value: getValues(item.value, payload.value, value), }) } schema={schema} diff --git a/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.test.js b/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.test.js index 36f4ef1ff23..ec39142e4da 100644 --- a/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.test.js +++ b/packages/forms/src/UIForm/fields/CheckBox/CheckBoxes.test.js @@ -88,6 +88,36 @@ describe('CheckBoxes field', () => { expect(screen.getByRole('checkbox', { name: 'My lol title' })).toBeDisabled(); }); + it('should render single disabled checkboxe', () => { + // given + const singleDisabledSchema = { + ...schema, + titleMap: [ + { name: 'My foo title', value: 'foo' }, + { name: 'My bar title', value: 'bar', disabled: true }, + { name: 'My lol title', value: 'lol' }, + ], + }; + + // when + render( + , + ); + + // then + expect(screen.getAllByRole('checkbox')).toHaveLength(3); + expect(screen.getByRole('checkbox', { name: 'My foo title' })).toBeEnabled(); + expect(screen.getByRole('checkbox', { name: 'My bar title' })).toBeDisabled(); + expect(screen.getByRole('checkbox', { name: 'My lol title' })).toBeEnabled(); + }); + describe('#onChange', () => { it('should trigger callback, adding a value to existing values', async () => { // given