Skip to content

Commit

Permalink
fix: take a copy of typeUserAttr when processing
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnetau committed Mar 12, 2024
1 parent f5a361a commit c002163
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/js/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ function FormBuilder(opts, element, $) {
const attrValType = userAttrType(typeUserAttr[attribute])
if (attrValType !== 'undefined') {
const orig = mi18n.get(attribute)
const tUA = typeUserAttr[attribute]
const tUA = Object.assign({}, typeUserAttr[attribute]) //Ensure we work on a copy of the attributes
let origValue = tUA.value
if (attrValType === 'boolean') {
tUA[attribute] ??= tUA.value
Expand All @@ -722,7 +722,6 @@ function FormBuilder(opts, element, $) {
}

i18n[attribute] = orig
tUA.value = origValue
} else if (attrValType === 'undefined' && hasSubType(values, attribute)) {
advField.push(processTypeUserAttrs(typeUserAttr[attribute], values))
} else {
Expand Down
39 changes: 38 additions & 1 deletion tests/form-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ describe('FormBuilder typeUserAttrs detection', () => {
'blue form-control': 'Blue'
},
style: 'border: 1px solid red',
value: 'Red',
value: 'red form-control',
}
},
},
Expand All @@ -543,6 +543,43 @@ describe('FormBuilder typeUserAttrs detection', () => {
expect(input.val()).toEqual('green form-control')
})

test('fix GH-1534', async() => {
const config = {
typeUserAttrs: {
'*': {
className: {
label: 'Class',
multiple: false,
options: {
'red form-control': 'Red',
'green form-control': 'Green',
'blue form-control': 'Blue'
},
style: 'border: 1px solid red',
value: 'red form-control',
}
},
},
}
const fbWrap = $('<div>')
const fb = await fbWrap.formBuilder(config).promise
fb.actions.setData([
{
type: 'text',
className: 'green form-control',
},
{
type: 'text',
className: 'blue form-control',
}
])

const input = fbWrap.find('.text-field .className-wrap select')
expect(input.length).toBe(2)
expect($(input.get(0)).val()).toEqual('green form-control')
expect($(input.get(1)).val()).toEqual('blue form-control')
})

test('can load formData with value for multi select typeUserAttr into stage', async() => {
const config = {
typeUserAttrs: {
Expand Down

0 comments on commit c002163

Please sign in to comment.