Skip to content

Commit 2fb8c7e

Browse files
move label generation to each field type explicitly
1 parent c99f0d9 commit 2fb8c7e

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

assets/src/js/admin/form-editor/field-generator.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ generators.select = function (config) {
5252
)
5353
}
5454

55+
if (config.label && config.showLabel) {
56+
return m('label', [
57+
config.label,
58+
m('select', attributes, options)
59+
])
60+
}
61+
5562
return m('select', attributes, options)
5663
}
5764

@@ -83,24 +90,26 @@ generators['terms-checkbox'] = function (config) {
8390
* @returns {*}
8491
*/
8592
generators.checkbox = function (config) {
86-
return config.choices.map(function (choice) {
87-
const name = config.name + (config.type === 'checkbox' ? '[]' : '')
88-
const required = config.required && config.type === 'radio'
89-
90-
return m('label', [
91-
m('input', {
92-
name,
93-
type: config.type,
94-
value: choice.value,
95-
checked: choice.selected,
96-
required,
97-
oncreate: setAttributes
98-
}),
99-
' ',
100-
m('span', choice.label)
101-
]
102-
)
103-
})
93+
return m('fieldset', [
94+
config.label && config.showLabel ? m('legend', config.label) : null,
95+
config.choices.map(function (choice) {
96+
const name = config.name + (config.type === 'checkbox' ? '[]' : '')
97+
const required = config.required && config.type === 'radio'
98+
99+
return m('label', [
100+
m('input', {
101+
name,
102+
type: config.type,
103+
value: choice.value,
104+
checked: choice.selected,
105+
required,
106+
oncreate: setAttributes
107+
}),
108+
' ',
109+
m('span', choice.label)
110+
])
111+
})
112+
])
104113
}
105114
generators.radio = generators.checkbox
106115

@@ -121,25 +130,32 @@ generators.default = function (config) {
121130
attributes.name = config.name
122131
}
123132

124-
if (config.min) {
133+
if (config.min.length) {
125134
attributes.min = config.min
126135
}
127136

128-
if (config.max) {
137+
if (config.max.length) {
129138
attributes.max = config.max
130139
}
131140

132-
if (config.value.length > 0) {
141+
if (config.value.length) {
133142
attributes.value = config.value
134143
}
135144

136-
if (config.placeholder.length > 0) {
145+
if (config.placeholder.length) {
137146
attributes.placeholder = config.placeholder
138147
}
139148

140149
attributes.required = config.required
141150
attributes.oncreate = setAttributes
142151

152+
if (config.label && config.showLabel) {
153+
return m('label', [
154+
config.label,
155+
m('input', attributes)
156+
])
157+
}
158+
143159
return m('input', attributes)
144160
}
145161

@@ -155,24 +171,11 @@ generators.procaptcha = function (config) {
155171
* @returns {string}
156172
*/
157173
function generate (config) {
158-
const isNested = !['checkbox', 'radio'].includes(config.type)
159-
const field = (generators[config.type] || generators.default)(config)
160-
const hasLabel = config.label.length > 0 && config.showLabel
161-
162-
const content = config.type === 'terms-checkbox'
163-
? field
164-
: isNested
165-
? (hasLabel
166-
? m('label', [config.label, field])
167-
: field)
168-
: m('fieldset', [hasLabel
169-
? m('legend', config.label)
170-
: '', field])
171-
172-
const htmlTemplate = (config.wrap && isNested) ? m('p', content) : content
174+
const content = (generators[config.type] || generators.default)(config)
175+
const wrap = config.wrap && config.type !== 'radio' && config.type !== 'checkbox'
176+
const htmlTemplate = wrap ? m('p', content) : content
173177
const vdom = document.createElement('div')
174178
m.render(vdom, htmlTemplate)
175-
176179
return htmlutil.prettyPrint(vdom.innerHTML) + '\n'
177180
}
178181

0 commit comments

Comments
 (0)