forked from bootstrap-vue/bootstrap-vue
/
form-input.js
162 lines (159 loc) · 3.53 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
import idMixin from '../../mixins/id'
import formMixin from '../../mixins/form'
import formSizeMixin from '../../mixins/form-size'
import formStateMixin from '../../mixins/form-state'
import { arrayIncludes } from '../../utils/array'
// Import styles
import './form-input.css'
// Valid supported input types
const TYPES = [
'text',
'password',
'email',
'number',
'url',
'tel',
'search',
'range',
'color',
`date`,
`time`,
`datetime`,
`datetime-local`,
`month`,
`week`
]
export default {
mixins: [idMixin, formMixin, formSizeMixin, formStateMixin],
render (h) {
return h('input', {
ref: 'input',
class: this.inputClass,
domProps: { value: this.localValue },
attrs: {
id: this.safeId(),
name: this.name,
type: this.localType,
disabled: this.disabled,
required: this.required,
readonly: this.readonly || this.plaintext,
placeholder: this.placeholder,
autocomplete: this.autocomplete || null,
'aria-required': this.required ? 'true' : null,
'aria-invalid': this.computedAriaInvalid
},
on: {
input: this.onInput,
change: this.onChange
}
})
},
data () {
return {
localValue: this.value
}
},
props: {
value: {
default: null
},
type: {
type: String,
default: 'text',
validator: type => arrayIncludes(TYPES, type)
},
ariaInvalid: {
type: [Boolean, String],
default: false
},
readonly: {
type: Boolean,
default: false
},
plaintext: {
type: Boolean,
default: false
},
autocomplete: {
type: String,
default: null
},
placeholder: {
type: String,
default: null
},
formatter: {
type: Function
},
lazyFormatter: {
type: Boolean,
default: false
}
},
computed: {
localType () {
// We only allow certain types
return arrayIncludes(TYPES, this.type) ? this.type : 'text'
},
inputClass () {
return [
this.plaintext ? 'form-control-plaintext' : 'form-control',
this.sizeFormClass,
this.stateClass
]
},
computedAriaInvalid () {
if (!this.ariaInvalid || this.ariaInvalid === 'false') {
// this.ariaInvalid is null or false or 'false'
return this.computedState === false ? 'true' : null
}
if (this.ariaInvalid === true) {
// User wants explicit aria-invalid=true
return 'true'
}
// Most likely a string value (which could be 'true')
return this.ariaInvalid
}
},
watch: {
value (newVal, oldVal) {
if (newVal !== oldVal) {
this.localValue = newVal
}
},
localValue (newVal, oldVal) {
if (newVal !== oldVal) {
this.$emit('input', newVal)
}
}
},
methods: {
format (value, e) {
if (this.formatter) {
const formattedValue = this.formatter(value, e)
if (formattedValue !== value) {
return formattedValue
}
}
return value
},
onInput (evt) {
const value = evt.target.value
if (this.lazyFormatter) {
// Update the model with the current unformated value
this.localValue = value
} else {
this.localValue = this.format(value, evt)
}
},
onChange (evt) {
this.localValue = this.format(evt.target.value, evt)
this.$emit('change', this.localValue)
},
focus () {
if (!this.disabled) {
this.$el.focus()
}
}
}
}