/
form-input.js
168 lines (165 loc) · 3.94 KB
/
form-input.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import Vue from '../../utils/vue'
import idMixin from '../../mixins/id'
import formMixin from '../../mixins/form'
import formSizeMixin from '../../mixins/form-size'
import formStateMixin from '../../mixins/form-state'
import formTextMixin from '../../mixins/form-text'
import formSelectionMixin from '../../mixins/form-selection'
import formValidityMixin from '../../mixins/form-validity'
import { arrayIncludes } from '../../utils/array'
import { eventOn, eventOff } from '../../utils/dom'
// Valid supported input types
const TYPES = [
'text',
'password',
'email',
'number',
'url',
'tel',
'search',
'range',
'color',
'date',
'time',
'datetime',
'datetime-local',
'month',
'week'
]
// @vue/component
export const BFormInput = /*#__PURE__*/ Vue.extend({
name: 'BFormInput',
mixins: [
idMixin,
formMixin,
formSizeMixin,
formStateMixin,
formTextMixin,
formSelectionMixin,
formValidityMixin
],
props: {
// value prop defined in form-text mixin
// value: { },
type: {
type: String,
default: 'text',
validator: type => arrayIncludes(TYPES, type)
},
noWheel: {
// Disable mousewheel to prevent wheel from changing values (i.e. number/date).
type: Boolean,
default: false
},
min: {
type: [String, Number],
default: null
},
max: {
type: [String, Number],
default: null
},
step: {
type: [String, Number],
default: null
},
list: {
type: String,
default: null
}
},
computed: {
localType() {
// We only allow certain types
return arrayIncludes(TYPES, this.type) ? this.type : 'text'
}
},
watch: {
noWheel(newVal) {
this.setWheelStopper(newVal)
}
},
mounted() {
this.setWheelStopper(this.noWheel)
},
deactivated() {
// Turn off listeners when keep-alive component deactivated
/* istanbul ignore next */
this.setWheelStopper(false)
},
activated() {
// Turn on listeners (if no-wheel) when keep-alive component activated
/* istanbul ignore next */
this.setWheelStopper(this.noWheel)
},
beforeDestroy() {
/* istanbul ignore next */
this.setWheelStopper(false)
},
methods: {
setWheelStopper(on) {
const input = this.$el
// We use native events, so that we don't interfere with propgation
if (on) {
eventOn(input, 'focus', this.onWheelFocus)
eventOn(input, 'blur', this.onWheelBlur)
} else {
eventOff(input, 'focus', this.onWheelFocus)
eventOff(input, 'blur', this.onWheelBlur)
eventOff(document, 'wheel', this.stopWheel)
}
},
onWheelFocus(evt) {
eventOn(document, 'wheel', this.stopWheel)
},
onWheelBlur(evt) {
eventOff(document, 'wheel', this.stopWheel)
},
stopWheel(evt) {
evt.preventDefault()
this.$el.blur()
}
},
render(h) {
var self = this
return h('input', {
ref: 'input',
class: self.computedClass,
directives: [
{
name: 'model',
rawName: 'v-model',
value: self.localValue,
expression: 'localValue'
}
],
attrs: {
id: self.safeId(),
name: self.name,
form: self.form || null,
type: self.localType,
disabled: self.disabled,
placeholder: self.placeholder,
required: self.required,
autocomplete: self.autocomplete || null,
readonly: self.readonly || self.plaintext,
min: self.min,
max: self.max,
step: self.step,
list: self.localType !== 'password' ? self.list : null,
'aria-required': self.required ? 'true' : null,
'aria-invalid': self.computedAriaInvalid
},
domProps: {
value: self.localValue
},
on: {
...self.$listeners,
input: self.onInput,
change: self.onChange,
blur: self.onBlur
}
})
}
})
export default BFormInput