/
VItemGroup.ts
269 lines (226 loc) Β· 6.52 KB
/
VItemGroup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Styles
import './VItemGroup.sass'
// Mixins
import Comparable from '../../mixins/comparable'
import Groupable from '../../mixins/groupable'
import Proxyable from '../../mixins/proxyable'
import Themeable from '../../mixins/themeable'
// Utilities
import mixins from '../../util/mixins'
import { consoleWarn } from '../../util/console'
// Types
import { VNode } from 'vue/types'
export type GroupableInstance = InstanceType<typeof Groupable> & {
id?: string
to?: any
value?: any
}
export const BaseItemGroup = mixins(
Comparable,
Proxyable,
Themeable
).extend({
name: 'base-item-group',
props: {
activeClass: {
type: String,
default: 'v-item--active',
},
mandatory: Boolean,
max: {
type: [Number, String],
default: null,
},
multiple: Boolean,
tag: {
type: String,
default: 'div',
},
},
data () {
return {
// As long as a value is defined, show it
// Otherwise, check if multiple
// to determine which default to provide
internalLazyValue: this.value !== undefined
? this.value
: this.multiple ? [] : undefined,
items: [] as GroupableInstance[],
}
},
computed: {
classes (): Record<string, boolean> {
return {
'v-item-group': true,
...this.themeClasses,
}
},
selectedIndex (): number {
return (this.selectedItem && this.items.indexOf(this.selectedItem)) || -1
},
selectedItem (): GroupableInstance | undefined {
if (this.multiple) return undefined
return this.selectedItems[0]
},
selectedItems (): GroupableInstance[] {
return this.items.filter((item, index) => {
return this.toggleMethod(this.getValue(item, index))
})
},
selectedValues (): any[] {
if (this.internalValue == null) return []
return Array.isArray(this.internalValue)
? this.internalValue
: [this.internalValue]
},
toggleMethod (): (v: any) => boolean {
if (!this.multiple) {
return (v: any) => this.valueComparator(this.internalValue, v)
}
const internalValue = this.internalValue
if (Array.isArray(internalValue)) {
return (v: any) => internalValue.some(intern => this.valueComparator(intern, v))
}
return () => false
},
},
watch: {
internalValue: 'updateItemsState',
items: 'updateItemsState',
},
created () {
if (this.multiple && !Array.isArray(this.internalValue)) {
consoleWarn('Model must be bound to an array if the multiple property is true.', this)
}
},
methods: {
genData (): object {
return {
class: this.classes,
}
},
getValue (item: GroupableInstance, i: number): unknown {
return item.value === undefined
? i
: item.value
},
onClick (item: GroupableInstance) {
this.updateInternalValue(
this.getValue(item, this.items.indexOf(item))
)
},
register (item: GroupableInstance) {
const index = this.items.push(item) - 1
item.$on('change', () => this.onClick(item))
// If no value provided and mandatory,
// assign first registered item
if (this.mandatory && !this.selectedValues.length) {
this.updateMandatory()
}
this.updateItem(item, index)
},
unregister (item: GroupableInstance) {
if (this._isDestroyed) return
const index = this.items.indexOf(item)
const value = this.getValue(item, index)
this.items.splice(index, 1)
const valueIndex = this.selectedValues.indexOf(value)
// Items is not selected, do nothing
if (valueIndex < 0) return
// If not mandatory, use regular update process
if (!this.mandatory) {
return this.updateInternalValue(value)
}
// Remove the value
if (this.multiple && Array.isArray(this.internalValue)) {
this.internalValue = this.internalValue.filter(v => v !== value)
} else {
this.internalValue = undefined
}
// If mandatory and we have no selection
// add the last item as value
/* istanbul ignore else */
if (!this.selectedItems.length) {
this.updateMandatory(true)
}
},
updateItem (item: GroupableInstance, index: number) {
const value = this.getValue(item, index)
item.isActive = this.toggleMethod(value)
},
// https://github.com/vuetifyjs/vuetify/issues/5352
updateItemsState () {
this.$nextTick(() => {
if (this.mandatory &&
!this.selectedItems.length
) {
return this.updateMandatory()
}
// TODO: Make this smarter so it
// doesn't have to iterate every
// child in an update
this.items.forEach(this.updateItem)
})
},
updateInternalValue (value: any) {
this.multiple
? this.updateMultiple(value)
: this.updateSingle(value)
},
updateMandatory (last?: boolean) {
if (!this.items.length) return
const items = this.items.slice()
if (last) items.reverse()
const item = items.find(item => !item.disabled)
// If no tabs are available
// aborts mandatory value
if (!item) return
const index = this.items.indexOf(item)
this.updateInternalValue(
this.getValue(item, index)
)
},
updateMultiple (value: any) {
const defaultValue = Array.isArray(this.internalValue)
? this.internalValue
: []
const internalValue = defaultValue.slice()
const index = internalValue.findIndex(val => this.valueComparator(val, value))
if (
this.mandatory &&
// Item already exists
index > -1 &&
// value would be reduced below min
internalValue.length - 1 < 1
) return
if (
// Max is set
this.max != null &&
// Item doesn't exist
index < 0 &&
// value would be increased above max
internalValue.length + 1 > this.max
) return
index > -1
? internalValue.splice(index, 1)
: internalValue.push(value)
this.internalValue = internalValue
},
updateSingle (value: any) {
const isSame = this.valueComparator(this.internalValue, value)
if (this.mandatory && isSame) return
this.internalValue = isSame ? undefined : value
},
},
render (h): VNode {
return h(this.tag, this.genData(), this.$slots.default)
},
})
export default BaseItemGroup.extend({
name: 'v-item-group',
provide (): object {
return {
itemGroup: this,
}
},
})