|
| 1 | +import without from 'lodash/without' |
| 2 | +import last from 'lodash/last' |
| 3 | +import ConnectedControlMixin from '../mixins/ConnectedControl' |
| 4 | + |
| 5 | +export default { |
| 6 | + props: { |
| 7 | + name: { |
| 8 | + type: String, |
| 9 | + required: true, |
| 10 | + }, |
| 11 | + |
| 12 | + value: { |
| 13 | + type: Array, |
| 14 | + default: () => [], |
| 15 | + }, |
| 16 | + |
| 17 | + validators: Array, |
| 18 | + asyncValidators: Array, |
| 19 | + |
| 20 | + renderField: { |
| 21 | + type: Function, |
| 22 | + required: true, |
| 23 | + }, |
| 24 | + }, |
| 25 | + |
| 26 | + mixins: [ConnectedControlMixin], |
| 27 | + |
| 28 | + computed: { |
| 29 | + controlValue() { |
| 30 | + return this.state[0] |
| 31 | + }, |
| 32 | + |
| 33 | + setValue() { |
| 34 | + return this.state[1] |
| 35 | + }, |
| 36 | + |
| 37 | + fields() { |
| 38 | + return { |
| 39 | + forEach: this.forEach, |
| 40 | + get: this.get, |
| 41 | + getAll: this.getAll, |
| 42 | + insert: this.insert, |
| 43 | + length: this.length, |
| 44 | + map: this.map, |
| 45 | + move: this.move, |
| 46 | + pop: this.pop, |
| 47 | + push: this.push, |
| 48 | + remove: this.remove, |
| 49 | + removeAll: this.removeAll, |
| 50 | + shift: this.shift, |
| 51 | + swap: this.swap, |
| 52 | + unshift: this.unshift, |
| 53 | + } |
| 54 | + }, |
| 55 | + }, |
| 56 | + |
| 57 | + methods: { |
| 58 | + forEach(callback) { |
| 59 | + return this.controlValue.forEach(callback) |
| 60 | + }, |
| 61 | + |
| 62 | + get(index) { |
| 63 | + return this.controlValue[index] |
| 64 | + }, |
| 65 | + |
| 66 | + getAll() { |
| 67 | + return this.controlValue |
| 68 | + }, |
| 69 | + |
| 70 | + insert(index, value) { |
| 71 | + const nextArray = [ |
| 72 | + ...this.controlValue.slice(0, index), |
| 73 | + value, |
| 74 | + ...this.controlValue.slice(index + 1), |
| 75 | + ] |
| 76 | + |
| 77 | + this.setValue(nextArray) |
| 78 | + |
| 79 | + return nextArray |
| 80 | + }, |
| 81 | + |
| 82 | + length() { |
| 83 | + return this.controlValue.length |
| 84 | + }, |
| 85 | + |
| 86 | + map(callback) { |
| 87 | + return this.controlValue.map(callback) |
| 88 | + }, |
| 89 | + |
| 90 | + move(fromIndex, toIndex) { |
| 91 | + const elementToMove = this.controlValue[fromIndex] |
| 92 | + const arrayWithout = without(this.controlValue, elementToMove) |
| 93 | + const nextArray = [ |
| 94 | + ...arrayWithout.slice(0, toIndex), |
| 95 | + elementToMove, |
| 96 | + ...arrayWithout.slice(toIndex + 1), |
| 97 | + ] |
| 98 | + |
| 99 | + this.setValue(nextArray) |
| 100 | + |
| 101 | + return nextArray |
| 102 | + }, |
| 103 | + |
| 104 | + pop() { |
| 105 | + const elementToRemove = last(this.controlValue) |
| 106 | + const nextArray = without(this.controlValue, elementToRemove) |
| 107 | + |
| 108 | + this.setValue(nextArray) |
| 109 | + |
| 110 | + return nextArray |
| 111 | + }, |
| 112 | + |
| 113 | + push(value) { |
| 114 | + const nextArray = this.controlValue.concat(value) |
| 115 | + |
| 116 | + this.setValue(nextArray) |
| 117 | + |
| 118 | + return nextArray |
| 119 | + }, |
| 120 | + |
| 121 | + remove(index) { |
| 122 | + const nextArray = without(this.controlValue, this.controlValue[index]) |
| 123 | + |
| 124 | + this.setValue(nextArray) |
| 125 | + |
| 126 | + return nextArray |
| 127 | + }, |
| 128 | + |
| 129 | + removeAll() { |
| 130 | + const nextArray = [] |
| 131 | + |
| 132 | + this.setValue(nextArray) |
| 133 | + |
| 134 | + return nextArray |
| 135 | + }, |
| 136 | + |
| 137 | + shift() { |
| 138 | + const nextArray = without(this.controlValue, this.controlValue[0]) |
| 139 | + |
| 140 | + this.setValue(nextArray) |
| 141 | + |
| 142 | + return nextArray |
| 143 | + }, |
| 144 | + |
| 145 | + swap(firstIndex, secondIndex) { |
| 146 | + const [smallerIndex, biggerIndex] = [firstIndex, secondIndex].sort() |
| 147 | + |
| 148 | + const firstElement = this.controlValue[smallerIndex] |
| 149 | + const secondElement = this.controlValue[biggerIndex] |
| 150 | + |
| 151 | + const nextArray = [ |
| 152 | + ...this.controlValue.slice(0, smallerIndex), |
| 153 | + secondElement, |
| 154 | + ...this.controlValue.slice(smallerIndex + 1, biggerIndex), |
| 155 | + firstElement, |
| 156 | + ...this.controlValue.slice(biggerIndex + 1), |
| 157 | + ] |
| 158 | + |
| 159 | + this.setValue(nextArray) |
| 160 | + |
| 161 | + return nextArray |
| 162 | + }, |
| 163 | + |
| 164 | + unshift(value) { |
| 165 | + const nextArray = [value].concat(this.controlValue) |
| 166 | + |
| 167 | + this.setValue(nextArray) |
| 168 | + |
| 169 | + return nextArray |
| 170 | + }, |
| 171 | + |
| 172 | + renderComponent(value) { |
| 173 | + return this.renderField({ data: value, fields: this.fields }) |
| 174 | + }, |
| 175 | + }, |
| 176 | +} |
0 commit comments