diff --git a/src/js/form-builder.js b/src/js/form-builder.js index 1a03e425e..43575a211 100644 --- a/src/js/form-builder.js +++ b/src/js/form-builder.js @@ -33,7 +33,7 @@ import { safename, forceNumber, getContentType, - generateSelectorClassNames, + generateSelectorClassNames, firstNumberOrUndefined, } from './utils' import { attributeWillClobber, setElementContent, setSanitizerConfig } from './sanitizer' import fontConfig from '../fonts/config.json' @@ -682,8 +682,16 @@ function FormBuilder(opts, element, $) { if (attrValType !== 'undefined') { const orig = mi18n.get(attribute) const tUA = typeUserAttr[attribute] - const origValue = attrValType === 'boolean' ? tUA.value : (tUA.value || '') - tUA.value = values[attribute] || origValue + let origValue = tUA.value + if (attrValType === 'boolean') { + origValue = tUA.value + tUA[attribute] ??= tUA.value + } else if (attrValType === 'number') { + tUA[attribute] ??= firstNumberOrUndefined(values[attribute], origValue) + } else { + origValue ??= '' + tUA[attribute] ??= values[attribute] || origValue + } if (tUA.label) { i18n[attribute] = Array.isArray(tUA.label) ? mi18n.get(...tUA.label) || tUA.label[0] : tUA.label @@ -871,7 +879,7 @@ function FormBuilder(opts, element, $) { */ const numberAttribute = (attribute, values) => { const { class: classname, className, ...attrs } = values - const attrVal = (isNaN(attrs[attribute])) ? undefined : attrs[attribute] + const attrVal = (Number.isNaN(attrs[attribute])) ? undefined : attrs[attribute] const attrLabel = mi18n.get(attribute) || attribute const placeholder = mi18n.get(`placeholder.${attribute}`) diff --git a/src/js/utils.js b/src/js/utils.js index 20f19f31b..48ffa56cc 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -702,6 +702,10 @@ export function titleCase(str) { ) } +export function firstNumberOrUndefined(...options) { + return options.find(x => typeof x === 'number') +} + const utils = { addEventListeners, attrString, @@ -733,6 +737,7 @@ const utils = { unique, validAttr, titleCase, + firstNumberOrUndefined, } /** diff --git a/tests/form-builder.test.js b/tests/form-builder.test.js index af753bb83..a0addd853 100644 --- a/tests/form-builder.test.js +++ b/tests/form-builder.test.js @@ -199,6 +199,38 @@ describe('FormBuilder stage names translated', () => { }) }) +describe('FormBuilder attribute setup', () => { + test('number control number attributes do not inherit field value when not set', async() => { + const config = {} + const fbWrap = $('