-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
index.ts
252 lines (233 loc) Β· 6.45 KB
/
index.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
// Mixins
import Colorable from '../colorable'
import Themeable from '../themeable'
import { inject as RegistrableInject } from '../registrable'
// Utilities
import { deepEqual } from '../../util/helpers'
import { consoleError } from '../../util/console'
import mixins from '../../util/mixins'
// Types
import { PropType } from 'vue'
import { InputMessage, InputValidationRules } from 'types'
/* @vue/component */
export default mixins(
Colorable,
RegistrableInject('form'),
Themeable
).extend({
name: 'validatable',
props: {
disabled: Boolean,
error: Boolean,
errorCount: {
type: [Number, String],
default: 1,
},
errorMessages: {
type: [String, Array] as PropType<InputMessage>,
default: () => [],
},
messages: {
type: [String, Array] as PropType<InputMessage>,
default: () => [],
},
readonly: Boolean,
rules: {
type: Array as PropType<InputValidationRules>,
default: () => [],
},
success: Boolean,
successMessages: {
type: [String, Array] as PropType<InputMessage>,
default: () => [],
},
validateOnBlur: Boolean,
value: { required: false },
},
data () {
return {
errorBucket: [] as string[],
hasColor: false,
hasFocused: false,
hasInput: false,
isFocused: false,
isResetting: false,
lazyValue: this.value,
valid: false,
}
},
computed: {
computedColor (): string | undefined {
if (this.disabled) return undefined
if (this.color) return this.color
// It's assumed that if the input is on a
// dark background, the user will want to
// have a white color. If the entire app
// is setup to be dark, then they will
// like want to use their primary color
if (this.isDark && !this.appIsDark) return 'white'
else return 'primary'
},
hasError (): boolean {
return (
this.internalErrorMessages.length > 0 ||
this.errorBucket.length > 0 ||
this.error
)
},
// TODO: Add logic that allows the user to enable based
// upon a good validation
hasSuccess (): boolean {
return (
this.internalSuccessMessages.length > 0 ||
this.success
)
},
externalError (): boolean {
return this.internalErrorMessages.length > 0 || this.error
},
hasMessages (): boolean {
return this.validationTarget.length > 0
},
hasState (): boolean {
if (this.disabled) return false
return (
this.hasSuccess ||
(this.shouldValidate && this.hasError)
)
},
internalErrorMessages (): InputValidationRules {
return this.genInternalMessages(this.errorMessages)
},
internalMessages (): InputValidationRules {
return this.genInternalMessages(this.messages)
},
internalSuccessMessages (): InputValidationRules {
return this.genInternalMessages(this.successMessages)
},
internalValue: {
get (): unknown {
return this.lazyValue
},
set (val: any) {
this.lazyValue = val
this.$emit('input', val)
},
},
shouldValidate (): boolean {
if (this.externalError) return true
if (this.isResetting) return false
return this.validateOnBlur
? this.hasFocused && !this.isFocused
: (this.hasInput || this.hasFocused)
},
validations (): InputValidationRules {
return this.validationTarget.slice(0, Number(this.errorCount))
},
validationState (): string | undefined {
if (this.disabled) return undefined
if (this.hasError && this.shouldValidate) return 'error'
if (this.hasSuccess) return 'success'
if (this.hasColor) return this.computedColor
return undefined
},
validationTarget (): InputValidationRules {
if (this.internalErrorMessages.length > 0) {
return this.internalErrorMessages
} else if (this.successMessages.length > 0) {
return this.internalSuccessMessages
} else if (this.messages.length > 0) {
return this.internalMessages
} else if (this.shouldValidate) {
return this.errorBucket
} else return []
},
},
watch: {
rules: {
handler (newVal, oldVal) {
if (deepEqual(newVal, oldVal)) return
this.validate()
},
deep: true,
},
internalValue () {
// If it's the first time we're setting input,
// mark it with hasInput
this.hasInput = true
this.validateOnBlur || this.$nextTick(this.validate)
},
isFocused (val) {
// Should not check validation
// if disabled
if (
!val &&
!this.disabled
) {
this.hasFocused = true
this.validateOnBlur && this.$nextTick(this.validate)
}
},
isResetting () {
setTimeout(() => {
this.hasInput = false
this.hasFocused = false
this.isResetting = false
this.validate()
}, 0)
},
hasError (val) {
if (this.shouldValidate) {
this.$emit('update:error', val)
}
},
value (val) {
this.lazyValue = val
},
},
beforeMount () {
this.validate()
},
created () {
this.form && this.form.register(this)
},
beforeDestroy () {
this.form && this.form.unregister(this)
},
methods: {
genInternalMessages (messages: InputMessage): InputValidationRules {
if (!messages) return []
else if (Array.isArray(messages)) return messages
else return [messages]
},
/** @public */
reset () {
this.isResetting = true
this.internalValue = Array.isArray(this.internalValue)
? []
: undefined
},
/** @public */
resetValidation () {
this.isResetting = true
},
/** @public */
validate (force = false, value?: any): boolean {
const errorBucket = []
value = value || this.internalValue
if (force) this.hasInput = this.hasFocused = true
for (let index = 0; index < this.rules.length; index++) {
const rule = this.rules[index]
const valid = typeof rule === 'function' ? rule(value) : rule
if (valid === false || typeof valid === 'string') {
errorBucket.push(valid || '')
} else if (typeof valid !== 'boolean') {
consoleError(`Rules should return a string or boolean, received '${typeof valid}' instead`, this)
}
}
this.errorBucket = errorBucket
this.valid = errorBucket.length === 0
return this.valid
},
},
})