From 2744a6840a47771486fbdfd3bd05ea0432bfa2f6 Mon Sep 17 00:00:00 2001 From: James Lucas Date: Wed, 1 Nov 2023 09:40:23 +1100 Subject: [PATCH] fix: For input[type=checkbox], no selection should result in an empty userData otherwise there is no ability to save state when default selected checkboxes are all unchecked --- src/js/form-render.js | 2 +- tests/form-render.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/js/form-render.js b/src/js/form-render.js index a83d06166..29d492c9c 100644 --- a/src/js/form-render.js +++ b/src/js/form-render.js @@ -303,7 +303,7 @@ class FormRender { // Skip disabled fields -- This will not have user data available if (definedField.disabled) continue - definedField.userData = userDataMap[definedField.name] + definedField.userData = userDataMap[definedField.name] ?? [] } }) diff --git a/tests/form-render.test.js b/tests/form-render.test.js index 7c8133a85..43bb689fe 100644 --- a/tests/form-render.test.js +++ b/tests/form-render.test.js @@ -113,4 +113,30 @@ describe('Form Rendering', () => { textarea = container.find('#textarea-elem') expect(textarea).not.toHaveLength(0) }) + + test('check userData when no value set', () => { + const container = $('
') + const formData = [ + {type: 'textarea', name: 'input-textarea'}, + {type: 'text', name: 'input-text'}, + {type: 'checkbox-group', name: 'input-checkbox-group', 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': false }, ]}, + {type: 'radio-group', name: 'input-radio-group', 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': false }, ]}, + {type: 'select', name: 'input-select', placeholder: 'Select...', 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': false }, ]}, + {type: 'select', name: 'input-select-multiple', placeholder: 'Select...', multiple: true, 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': false }, ]}, + ] + container.formRender({ formData }) + + const userData = {} + + container.formRender('userData').forEach(elem => { + userData[elem.name] = elem.userData + }) + + expect(userData['input-textarea']).toStrictEqual(['']) + expect(userData['input-text']).toStrictEqual(['']) + expect(userData['input-checkbox-group']).toStrictEqual([]) + expect(userData['input-radio-group']).toStrictEqual([]) + expect(userData['input-select']).toStrictEqual([]) + expect(userData['input-select-multiple']).toStrictEqual([]) + }) }) \ No newline at end of file