diff --git a/src/components/inspector/index.js b/src/components/inspector/index.js index 55392528b..619ba5fdc 100644 --- a/src/components/inspector/index.js +++ b/src/components/inspector/index.js @@ -6,4 +6,5 @@ export { default as OptionsList } from './options-list'; export { default as ColumnSetup } from './column-setup'; export { default as PageSelect } from './page-select'; export { default as ValidationSelect } from './validation-select'; -export { default as ScreenSelector } from './screen-selector'; \ No newline at end of file +export { default as ScreenSelector } from './screen-selector'; +export { default as LoopInspector } from './loop'; \ No newline at end of file diff --git a/src/components/inspector/loop.vue b/src/components/inspector/loop.vue new file mode 100644 index 000000000..00469b974 --- /dev/null +++ b/src/components/inspector/loop.vue @@ -0,0 +1,90 @@ + + + + + diff --git a/src/components/renderer/form-loop.vue b/src/components/renderer/form-loop.vue index 4eae8cb2f..c599e98b4 100644 --- a/src/components/renderer/form-loop.vue +++ b/src/components/renderer/form-loop.vue @@ -13,6 +13,13 @@ @update="setMatrixValue(loopIndex, $event)" /> + + + + + + + @@ -22,11 +29,12 @@ import Mustache from 'mustache'; export default { name: 'FormLoop', mixins: [], - props: ['value', 'name', 'config', 'transientData'], + props: ['value', 'config', 'transientData', 'name'], data() { return { matrix: [], items: [], + additionalItems: 0, }; }, computed: { @@ -38,17 +46,19 @@ export default { }]; }, times() { - // If there is existing data, set the length to what ever it is - const itemsFromData = _.get(this.transientData, this.name, null); - if (Array.isArray(itemsFromData)) { - return [...itemsFromData.keys()]; + if (!this.config || !this.config.settings) { + return []; } - if (!this.config) { - return []; + if (this.config.settings.type === 'existing') { + const itemsFromData = _.get(this.transientData, this.name, null); + if (!itemsFromData) { + return []; + } + return [...itemsFromData.keys()]; } - let times = this.config.times; + let times = this.config.settings.times; try { times = Mustache.render(times, this.transientData); @@ -64,6 +74,8 @@ export default { times = 100; } + times += this.additionalItems; + return [...Array(times).keys()]; }, }, @@ -94,8 +106,27 @@ export default { times() { this.setupMatrix(); }, + 'config.settings.times': { + handler() { + this.additionalItems = 0; + } + } }, methods: { + add() { + if (this.config.settings.type === 'existing') { + this.setMatrixValue(this.matrix.length, {}); + } else { + this.additionalItems++; + } + }, + remove() { + if (this.config.settings.type === 'existing') { + this.$delete(this.matrix, this.matrix.length - 1); + } else { + this.additionalItems--; + } + }, setMatrixValue(i, v) { this.$set(this.matrix, i, v); }, @@ -109,7 +140,7 @@ export default { setupMatrix() { for (const i of this.times) { if (typeof this.matrix[i] === 'undefined') { - this.$set(this.matrix, i, {}); + this.setMatrixValue(i, {}); } else { } } diff --git a/src/components/vue-form-builder.vue b/src/components/vue-form-builder.vue index 55ce68353..f91b0a14d 100644 --- a/src/components/vue-form-builder.vue +++ b/src/components/vue-form-builder.vue @@ -199,6 +199,7 @@ v-bind="item.config" v-model="inspection.config[item.field]" @focusout.native="updateState" + @setName="inspection.config.name = $event" /> diff --git a/src/form-builder-controls.js b/src/form-builder-controls.js index 546d14389..04a9f406f 100644 --- a/src/form-builder-controls.js +++ b/src/form-builder-controls.js @@ -364,17 +364,20 @@ export default [ // Default items container items: [], config: { + name: '', icon: 'fas fa-redo', - times: '2', + settings: { + type: 'new', + varname: '', + times: '3', + add: false, + }, }, inspector: [ - keyNameProperty, { - type: 'FormInput', - field: 'times', + type: 'LoopInspector', + field: 'settings', config: { - label: 'Default Number Of Times', - helper: 'Enter the number of times to repeat if the data does not exist.', }, }, ],