Skip to content

Commit

Permalink
fix: convert raw HTML strings received from control.build() into HTML…
Browse files Browse the repository at this point in the history
…Element(s) to ensure processClassName() can strip bootstrap classes
  • Loading branch information
lucasnetau committed Oct 24, 2023
1 parent cde865a commit 03e657f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/js/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ export default class layout {
field = { field: field }
}

// If field is a raw HTML string, convert to HTMLElement(s)
if (typeof field.field === 'string') {
const tmpField = this.markup('div', field.field, {})
if (tmpField.childElementCount === 1) {
field.field = tmpField.children.item(0)
} else {
field.field = Array.from(tmpField.children)
}
}

// build the label & help text
const label = this.label()
const help = this.help()
Expand Down
67 changes: 66 additions & 1 deletion tests/control/custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,71 @@ describe('Test Custom Control', () => {
}
fb.actions.addField(field)

expect(fbWrap.find('.stage-wrap li')).toHaveLength(1)
expect(fbWrap.find('.stage-wrap li[type="customText"]')).toHaveLength(1)
})

test('test add custom field defined as html string and row/col set', async () => {
const fbWrap = $('<div>')

const fields = [{
label: 'Custom with string field',
attrs: {
type: 'customString'
},
icon: '🌟'
}]
const templates = {
customString: function(fieldData) {
return {
field: `<span class="${fieldData.className}">Placeholder</span>`,
}
}
}

const fb = await $(fbWrap).formBuilder({fields, templates}).promise
const field = {
type: 'customString',
className: 'form-control row-1 col-md-12'
}
fb.actions.addField(field)

expect(fbWrap.find('.stage-wrap li[type="customString"]')).toHaveLength(1)
const renderedCtl = $(fbWrap).find('.prev-holder span')
expect(renderedCtl.attr('class')).toBe('form-control')
expect(renderedCtl.text()).toBe('Placeholder')
})

test('test add custom field defined as complex html string and row/col set', async () => {
const fbWrap = $('<div>')

const fields = [{
label: 'Custom with string field',
attrs: {
type: 'customString'
},
icon: '🌟'
}]
const templates = {
customString: function(fieldData) {
return {
field: `<div><span class="${fieldData.className}">Placeholder</span></div><input />`,
}
}
}

const fb = await $(fbWrap).formBuilder({fields, templates}).promise
const field = {
type: 'customString',
className: 'form-control row-1 col-md-12'
}
fb.actions.addField(field)

expect(fbWrap.find('.stage-wrap li[type="customString"]')).toHaveLength(1)
expect($(fbWrap).find('.prev-holder div')).toHaveLength(2)
expect($(fbWrap).find('.prev-holder input')).toHaveLength(1)
expect($(fbWrap).find('.prev-holder span')).toHaveLength(1)
const renderedCtl = $(fbWrap).find('.prev-holder span')
expect(renderedCtl.attr('class')).toBe('form-control')
expect(renderedCtl.text()).toBe('Placeholder')
})
})

0 comments on commit 03e657f

Please sign in to comment.