4cd4fd28
郭伟龙
feat: 初始化项目
|
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
|
import { extend } from '../vue'
import {
EVENT_NAME_BLUR,
EVENT_NAME_CHANGE,
EVENT_NAME_INPUT,
EVENT_NAME_UPDATE
} from '../constants/events'
import {
PROP_TYPE_BOOLEAN,
PROP_TYPE_BOOLEAN_STRING,
PROP_TYPE_FUNCTION,
PROP_TYPE_NUMBER_STRING,
PROP_TYPE_STRING
} from '../constants/props'
import { attemptBlur, attemptFocus } from '../utils/dom'
import { stopEvent } from '../utils/events'
import { mathMax } from '../utils/math'
import { makeModelMixin } from '../utils/model'
import { toInteger, toFloat } from '../utils/number'
import { sortKeys } from '../utils/object'
import { hasPropFunction, makeProp, makePropsConfigurable } from '../utils/props'
import { toString } from '../utils/string'
// --- Constants ---
const {
mixin: modelMixin,
props: modelProps,
prop: MODEL_PROP_NAME,
event: MODEL_EVENT_NAME
} = makeModelMixin('value', {
type: PROP_TYPE_NUMBER_STRING,
defaultValue: '',
event: EVENT_NAME_UPDATE
})
export { MODEL_PROP_NAME, MODEL_EVENT_NAME }
// --- Props ---
export const props = makePropsConfigurable(
sortKeys({
...modelProps,
ariaInvalid: makeProp(PROP_TYPE_BOOLEAN_STRING, false),
autocomplete: makeProp(PROP_TYPE_STRING),
// Debounce timeout (in ms). Not applicable with `lazy` prop
debounce: makeProp(PROP_TYPE_NUMBER_STRING, 0),
formatter: makeProp(PROP_TYPE_FUNCTION),
// Only update the `v-model` on blur/change events
lazy: makeProp(PROP_TYPE_BOOLEAN, false),
lazyFormatter: makeProp(PROP_TYPE_BOOLEAN, false),
number: makeProp(PROP_TYPE_BOOLEAN, false),
placeholder: makeProp(PROP_TYPE_STRING),
plaintext: makeProp(PROP_TYPE_BOOLEAN, false),
readonly: makeProp(PROP_TYPE_BOOLEAN, false),
trim: makeProp(PROP_TYPE_BOOLEAN, false)
}),
'formTextControls'
)
// --- Mixin ---
// @vue/component
export const formTextMixin = extend({
mixins: [modelMixin],
props,
data() {
const value = this[MODEL_PROP_NAME]
return {
localValue: toString(value),
vModelValue: this.modifyValue(value)
}
},
computed: {
computedClass() {
const { plaintext, type } = this
const isRange = type === 'range'
const isColor = type === 'color'
return [
{
// Range input needs class `custom-range`
'custom-range': isRange,
// `plaintext` not supported by `type="range"` or `type="color"`
'form-control-plaintext': plaintext && !isRange && !isColor,
// `form-control` not used by `type="range"` or `plaintext`
// Always used by `type="color"`
'form-control': isColor || (!plaintext && !isRange)
},
this.sizeFormClass,
this.stateClass
]
},
computedDebounce() {
// Ensure we have a positive number equal to or greater than 0
return mathMax(toInteger(this.debounce, 0), 0)
},
hasFormatter() {
return hasPropFunction(this.formatter)
}
},
watch: {
[MODEL_PROP_NAME](newValue) {
const stringifyValue = toString(newValue)
const modifiedValue = this.modifyValue(newValue)
if (stringifyValue !== this.localValue || modifiedValue !== this.vModelValue) {
// Clear any pending debounce timeout, as we are overwriting the user input
this.clearDebounce()
// Update the local values
this.localValue = stringifyValue
this.vModelValue = modifiedValue
}
}
},
created() {
// Create private non-reactive props
this.$_inputDebounceTimer = null
},
beforeDestroy() {
this.clearDebounce()
},
methods: {
clearDebounce() {
clearTimeout(this.$_inputDebounceTimer)
this.$_inputDebounceTimer = null
},
formatValue(value, event, force = false) {
value = toString(value)
if (this.hasFormatter && (!this.lazyFormatter || force)) {
value = this.formatter(value, event)
}
return value
},
modifyValue(value) {
value = toString(value)
// Emulate `.trim` modifier behaviour
if (this.trim) {
value = value.trim()
}
// Emulate `.number` modifier behaviour
if (this.number) {
value = toFloat(value, value)
}
return value
},
updateValue(value, force = false) {
const { lazy } = this
if (lazy && !force) {
return
}
// Make sure to always clear the debounce when `updateValue()`
// is called, even when the v-model hasn't changed
this.clearDebounce()
// Define the shared update logic in a method to be able to use
// it for immediate and debounced value changes
const doUpdate = () => {
value = this.modifyValue(value)
if (value !== this.vModelValue) {
this.vModelValue = value
this.$emit(MODEL_EVENT_NAME, value)
} else if (this.hasFormatter) {
// When the `vModelValue` hasn't changed but the actual input value
// is out of sync, make sure to change it to the given one
// Usually caused by browser autocomplete and how it triggers the
// change or input event, or depending on the formatter function
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2657
// https://github.com/bootstrap-vue/bootstrap-vue/issues/3498
/* istanbul ignore next: hard to test */
const $input = this.$refs.input
/* istanbul ignore if: hard to test out of sync value */
if ($input && value !== $input.value) {
$input.value = value
}
}
}
// Only debounce the value update when a value greater than `0`
// is set and we are not in lazy mode or this is a forced update
const debounce = this.computedDebounce
if (debounce > 0 && !lazy && !force) {
this.$_inputDebounceTimer = setTimeout(doUpdate, debounce)
} else {
// Immediately update the v-model
doUpdate()
}
},
onInput(event) {
// `event.target.composing` is set by Vue
// https://github.com/vuejs/vue/blob/dev/src/platforms/web/runtime/directives/model.js
// TODO: Is this needed now with the latest Vue?
/* istanbul ignore if: hard to test composition events */
if (event.target.composing) {
return
}
const { value } = event.target
const formattedValue = this.formatValue(value, event)
// Exit when the `formatter` function strictly returned `false`
// or prevented the input event
/* istanbul ignore next */
if (formattedValue === false || event.defaultPrevented) {
stopEvent(event, { propagation: false })
return
}
this.localValue = formattedValue
this.updateValue(formattedValue)
this.$emit(EVENT_NAME_INPUT, formattedValue)
},
onChange(event) {
const { value } = event.target
const formattedValue = this.formatValue(value, event)
// Exit when the `formatter` function strictly returned `false`
// or prevented the input event
/* istanbul ignore next */
if (formattedValue === false || event.defaultPrevented) {
stopEvent(event, { propagation: false })
return
}
this.localValue = formattedValue
this.updateValue(formattedValue, true)
this.$emit(EVENT_NAME_CHANGE, formattedValue)
},
onBlur(event) {
// Apply the `localValue` on blur to prevent cursor jumps
// on mobile browsers (e.g. caused by autocomplete)
const { value } = event.target
const formattedValue = this.formatValue(value, event, true)
if (formattedValue !== false) {
// We need to use the modified value here to apply the
// `.trim` and `.number` modifiers properly
this.localValue = toString(this.modifyValue(formattedValue))
// We pass the formatted value here since the `updateValue` method
// handles the modifiers itself
this.updateValue(formattedValue, true)
}
// Emit native blur event
this.$emit(EVENT_NAME_BLUR, event)
},
focus() {
// For external handler that may want a focus method
if (!this.disabled) {
attemptFocus(this.$el)
}
},
blur() {
// For external handler that may want a blur method
if (!this.disabled) {
attemptBlur(this.$el)
}
}
}
})
|