Skip to content

Commit

Permalink
feat: onAddOption
Browse files Browse the repository at this point in the history
allows you to define an onAddOption method that will transfer the optionTemplate to add custom label or values. resolves #1098
  • Loading branch information
kevinchappell committed Aug 23, 2020
1 parent a091ba9 commit 7824e08
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/demo/js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ jQuery(function($) {
onAddField: fieldId => {
setCurrentFieldIdValues(fieldId)
},
onAddOption: (optionTemplate, optionIndex) => {
optionTemplate.label = `Option ${optionIndex + 1}`
optionTemplate.value = `option-${optionIndex + 1}`
return optionTemplate
},
onClearAll: () => window.sessionStorage.removeItem('formData'),
stickyControls: {
enable: true,
Expand Down
1 change: 1 addition & 0 deletions src/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const defaultOptions = {
},
},
onAddField: (fieldId, fieldData) => fieldData,
onAddOption: noop,
onClearAll: noop,
onCloseFieldEdit: noop,
onOpenFieldEdit: noop,
Expand Down
4 changes: 3 additions & 1 deletion src/js/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,9 @@ const FormBuilder = function(opts, element, $) {

const name = $firstOption.attr('name').replace(/-option$/, '')

$('.sortable-options', $optionWrap).append(selectFieldOptions(name, false, isMultiple))
const optionTemplate = { selected: false, label: '', value: '' }
const optionData = config.opts.onAddOption(optionTemplate, $('.sortable-options', $optionWrap).children().length)
$('.sortable-options', $optionWrap).append(selectFieldOptions(name, optionData, isMultiple))
})

$stage.on('mouseover mouseout', '.remove, .del-button', e =>
Expand Down

0 comments on commit 7824e08

Please sign in to comment.