Skip to content

Commit

Permalink
fix: For input[type=checkbox], no selection should result in an empty…
Browse files Browse the repository at this point in the history
… userData otherwise there is no ability to save state when default selected checkboxes are all unchecked
  • Loading branch information
lucasnetau committed Oct 31, 2023
1 parent 477f7ca commit 2744a68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/js/form-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? []
}
})

Expand Down
26 changes: 26 additions & 0 deletions tests/form-render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div>')
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([])
})
})

0 comments on commit 2744a68

Please sign in to comment.