Skip to content

Commit

Permalink
fix: onAddOption default
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchappell committed Aug 24, 2020
1 parent cfb6536 commit 55f4a8f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/demo/js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ jQuery(function($) {
onAddField: fieldId => {
setCurrentFieldIdValues(fieldId)
},
onAddOption: (optionTemplate, {index}) => {
optionTemplate.label = optionTemplate.label || `Option ${index + 1}`
optionTemplate.value = optionTemplate.value || `option-${index + 1}`
// onAddOption: (optionTemplate, {index}) => {
// optionTemplate.label = optionTemplate.label || `Option ${index + 1}`
// optionTemplate.value = optionTemplate.value || `option-${index + 1}`

return optionTemplate
},
// return optionTemplate
// },
onClearAll: () => window.sessionStorage.removeItem('formData'),
stickyControls: {
enable: true,
Expand Down
2 changes: 1 addition & 1 deletion src/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const defaultOptions = {
},
},
onAddField: (fieldId, fieldData) => fieldData,
onAddOption: noop,
onAddOption: obj => obj,
onClearAll: noop,
onCloseFieldEdit: noop,
onOpenFieldEdit: noop,
Expand Down
15 changes: 9 additions & 6 deletions src/js/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,21 @@ const FormBuilder = function(opts, element, $) {
const optionActions = [m('a', mi18n.get('addOption'), { className: 'add add-opt' })]
const fieldOptions = [m('label', mi18n.get('selectOptions'), { className: 'false-label' })]
const isMultiple = fieldData.multiple || type === 'checkbox-group'
const optionDataTemplate = label => ({
selected: false,
label,
value: hyphenCase(label),
})
const optionDataTemplate = count => {
const label = mi18n.get('optionCount', count)
return {
selected: false,
label,
value: hyphenCase(label)
}
}

if (!values || !values.length) {
let defaultOptCount = [1, 2, 3]
if (['checkbox-group', 'checkbox'].includes(type)) {
defaultOptCount = [1]
}
fieldValues = defaultOptCount.map(index => optionDataTemplate(`${mi18n.get('optionCount', index)}`))
fieldValues = defaultOptCount.map(optionDataTemplate)

const firstOption = fieldValues[0]
if (firstOption.hasOwnProperty('selected') && type !== 'radio-group') {
Expand Down

0 comments on commit 55f4a8f

Please sign in to comment.