-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
selectable.js
146 lines (124 loc) Β· 3.14 KB
/
selectable.js
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
// Components
import VInput from '../components/VInput'
// Mixins
import Rippleable from './rippleable'
import Comparable from './comparable'
/* @vue/component */
export default VInput.extend({
name: 'selectable',
mixins: [Rippleable, Comparable],
model: {
prop: 'inputValue',
event: 'change'
},
props: {
color: {
type: String,
default: 'accent'
},
id: String,
inputValue: null,
falseValue: null,
trueValue: null,
multiple: {
type: Boolean,
default: null
},
label: String
},
data: vm => ({
lazyValue: vm.inputValue
}),
computed: {
computedColor () {
return this.isActive ? this.color : this.validationState
},
isMultiple () {
return this.multiple === true || (this.multiple === null && Array.isArray(this.internalValue))
},
isActive () {
const value = this.value
const input = this.internalValue
if (this.isMultiple) {
if (!Array.isArray(input)) return false
return input.some(item => this.valueComparator(item, value))
}
if (this.trueValue === undefined || this.falseValue === undefined) {
return value
? this.valueComparator(value, input)
: Boolean(input)
}
return this.valueComparator(input, this.trueValue)
},
isDirty () {
return this.isActive
}
},
watch: {
inputValue (val) {
this.lazyValue = val
}
},
methods: {
genLabel () {
if (!this.hasLabel) return null
const label = VInput.options.methods.genLabel.call(this)
label.data.on = { click: this.onChange }
return label
},
genInput (type, attrs) {
return this.$createElement('input', {
attrs: Object.assign({
'aria-label': this.label,
'aria-checked': this.isActive.toString(),
disabled: this.isDisabled,
id: this.id,
role: type,
type
}, attrs),
domProps: {
value: this.value,
checked: this.isActive
},
on: {
blur: this.onBlur,
change: this.onChange,
focus: this.onFocus,
keydown: this.onKeydown
},
ref: 'input'
})
},
onBlur () {
this.isFocused = false
},
onChange () {
if (this.isDisabled) return
const value = this.value
let input = this.internalValue
if (this.isMultiple) {
if (!Array.isArray(input)) {
input = []
}
const length = input.length
input = input.filter(item => !this.valueComparator(item, value))
if (input.length === length) {
input.push(value)
}
} else if (this.trueValue !== undefined && this.falseValue !== undefined) {
input = this.valueComparator(input, this.trueValue) ? this.falseValue : this.trueValue
} else if (value) {
input = this.valueComparator(input, value) ? null : value
} else {
input = !input
}
this.validate(true, input)
this.internalValue = input
},
onFocus () {
this.isFocused = true
},
/** @abstract */
onKeydown (e) {}
}
})