Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formRender removing a valid empty userData array #1515

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 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 @@ -156,9 +156,13 @@ class FormRender {
: field.className || field.class || null
delete sanitizedField.class
if (field.values) {
field.values = field.values.map(option => utils.trimObj(option))
sanitizedField.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
}
Comment on lines +162 to +164
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return sanitizedField
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ window.fbEditors = {
}

/**
* Remove null or undefined values from an object, original object is not modified
* Remove null, undefined, empty string or empty array values from an object, original object is not modified
* @param {Object} obj {attrName: attrValue}
* @param {boolean} [removeFalse=false] Remove values === false
* @return {Object} Object trimmed of null or undefined values
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([])
})
})
Loading