Skip to content

Commit

Permalink
fix: do not remove valid empty userData from field data struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnetau committed Feb 8, 2024
1 parent cf006c1 commit 1e64c69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/js/form-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class FormRender {
* @return {Object} sanitized field object
*/
sanitizeField(field, instanceIndex) {
const sanitizedField = Object.assign({}, field)
let sanitizedField = Object.assign({}, field)
if (instanceIndex) {
sanitizedField.id = field.id && `${field.id}-${instanceIndex}`
sanitizedField.name = field.name && `${field.name}-${instanceIndex}`
Expand All @@ -158,7 +158,11 @@ class FormRender {
if (field.values) {
field.values = field.values.map(option => utils.trimObj(option))
}
return utils.trimObj(sanitizedField)
sanitizedField = utils.trimObj(sanitizedField)
if (Array.isArray(field.userData) && field.userData.length === 0) {
sanitizedField.userData = [] //Special handler for allowing userData to be empty
}
return sanitizedField
}

/**
Expand Down
27 changes: 26 additions & 1 deletion tests/form-render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Form Rendering', () => {
expect(textarea).not.toHaveLength(0)
})

test('check userData when no value set', () => {
test('check userData returned when no default value set', () => {
const container = $('<div>')
const formData = [
{type: 'textarea', name: 'input-textarea'},
Expand All @@ -139,4 +139,29 @@ describe('Form Rendering', () => {
expect(userData['input-select']).toStrictEqual([])
expect(userData['input-select-multiple']).toStrictEqual([])
})

test('check default values can be overridden by no value userData', () => {
const container = $('<div>')
const formData = [
{type: 'textarea', name: 'input-textarea', value: 'default', userData: ['']},
{type: 'text', name: 'input-text', value: 'default', userData: ['']},
{type: 'checkbox-group', name: 'input-checkbox-group', 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': true }, ], userData: []},
//@Note Radio-buttons cannot be deselected, userData: [] should only be seen when no radio selected
{type: 'select', name: 'input-select', placeholder: 'Select...', 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': true }, ], userData: []},
{type: 'select', name: 'input-select-multiple', placeholder: 'Select...', multiple: true, 'values': [ { 'label': 'O', 'value': 'option-1', 'selected': true }, ], userData: []},
]
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-select']).toStrictEqual([])
expect(userData['input-select-multiple']).toStrictEqual([])
})
})

0 comments on commit 1e64c69

Please sign in to comment.