13
13
:name =" name"
14
14
:id =" id"
15
15
:tabindex =" tabindex"
16
- :max =" maxValue "
16
+ :max =" max "
17
17
:maxlength =" maxlength ? maxlength : null"
18
- :min =" minValue "
18
+ :min =" min "
19
19
:number =" type === 'number' ? true : null"
20
- :step =" stepValue "
20
+ :step =" step "
21
21
:type =" type"
22
- :value =" value"
23
22
24
- @blur =" onBlur"
25
- @change =" onChange"
26
- @focus =" onFocus"
27
- @input =" updateValue($event.target.value)"
28
- @keydown.enter =" onKeydownEnter"
29
- @keydown =" onKeydown"
23
+ @blur =" handleBlur"
24
+ @change =" handleChange"
25
+ @focus =" handleFocus"
26
+ @input =" handleInput"
30
27
31
28
ref =" input"
32
29
class =" textbox__input"
45
42
:id =" id"
46
43
:rows =" rows"
47
44
:tabindex =" tabindex"
48
- :value =" value"
49
45
50
- @blur =" onBlur"
51
- @change =" onChange"
52
- @focus =" onFocus"
53
- @input =" updateValue($event.target.value)"
54
- @keydown.enter =" onKeydownEnter"
55
- @keydown =" onKeydown"
46
+ @blur =" handleBlur"
47
+ @change =" handleChange"
48
+ @focus =" handleFocus"
49
+ @input =" handleInput"
56
50
57
51
ref =" textarea"
58
52
class =" textbox__textarea"
@@ -109,11 +103,17 @@ export default {
109
103
type: Number ,
110
104
default: 2 ,
111
105
},
112
- min: Number ,
113
- max: Number ,
106
+ min: {
107
+ type: Number ,
108
+ default: - Infinity ,
109
+ },
110
+ max: {
111
+ type: Number ,
112
+ default: Infinity ,
113
+ },
114
114
step: {
115
- type: String ,
116
- default: ' any ' ,
115
+ type: Number ,
116
+ default: 1 ,
117
117
},
118
118
maxlength: Number ,
119
119
readonly: {
@@ -146,21 +146,6 @@ export default {
146
146
},
147
147
},
148
148
computed: {
149
- minValue () {
150
- if (this .type === ' number' && this .min !== undefined ) {
151
- return this .min ;
152
- }
153
- return null ;
154
- },
155
- maxValue () {
156
- if (this .type === ' number' && this .max !== undefined ) {
157
- return this .max ;
158
- }
159
- return null ;
160
- },
161
- stepValue () {
162
- return this .type === ' number' ? this .step : null ;
163
- },
164
149
classes () {
165
150
return [
166
151
` textbox--size-${ this .size } ` ,
@@ -170,58 +155,76 @@ export default {
170
155
{ ' textbox--error' : this .error },
171
156
];
172
157
},
158
+ nativeInputValue () {
159
+ return this .value === null || this .value === undefined ? ' ' : String (this .value );
160
+ },
173
161
},
174
162
data () {
175
163
return {
176
164
isTouched: false ,
165
+ isFocused: false ,
177
166
initialValue: this .value ,
178
167
};
179
168
},
180
- created () {
181
- // If value is null, set it to empty string
182
- if (this .value === null ) {
183
- this .initialValue = ' ' ;
184
- this .updateValue (' ' );
185
- }
169
+ mounted () {
170
+ this .setNativeInputValue ();
186
171
},
187
172
methods: {
188
- updateValue (value ) {
189
- this .$emit (' input' , value);
190
- },
191
- onChange (e ) {
192
- this .$emit (' change' , this .value , e);
173
+ getInput () {
174
+ return this .$refs .input || this .$refs .textarea ;
193
175
},
194
- onFocus ( e ) {
195
- this .$emit ( ' focus' , e );
176
+ focus ( ) {
177
+ this .getInput (). focus ( );
196
178
},
197
- onBlur (e ) {
198
- this .$emit (' blur' , e);
199
- if (! this .isTouched ) {
200
- this .isTouched = true ;
201
- this .$emit (' touch' );
202
- }
179
+ blur () {
180
+ this .getInput ().blur ();
203
181
},
204
- onKeydown ( e ) {
205
- this .$emit ( ' keydown ' , e );
182
+ select ( ) {
183
+ this .getInput (). select ( );
206
184
},
207
- onKeydownEnter (e ) {
208
- this .$emit (' keydown-enter' , e);
185
+ clear () {
186
+ this .$emit (' input' , ' ' );
187
+ this .$emit (' change' , ' ' );
188
+ this .$emit (' clear' );
209
189
},
210
190
reset () {
211
- if (
212
- document .activeElement === this .$refs .input
213
- || document .activeElement === this .$refs .textarea
214
- ) {
215
- document .activeElement .blur ();
216
- }
217
- this .updateValue (this .initialValue );
218
- this .resetTouched ();
219
- },
220
- resetTouched (options = { touched: false }) {
221
- this .isTouched = options .touched ;
191
+ this .$emit (' input' , this .initialValue );
192
+ this .$emit (' change' , this .initialValue );
193
+ this .$emit (' reset' );
222
194
},
223
- focus () {
224
- (this .$refs .input || this .$refs .textarea ).focus ();
195
+ handleBlur (event ) {
196
+ this .isFocused = false ;
197
+ this .$emit (' blur' , event );
198
+ },
199
+ handleFocus (event ) {
200
+ this .isFocused = true ;
201
+ this .$emit (' focus' , event );
202
+ },
203
+ handleInput (event ) {
204
+ if (event .target .value === this .nativeInputValue ) return ;
205
+
206
+ this .$emit (' input' , event .target .value );
207
+
208
+ this .$nextTick (this .setNativeInputValue );
209
+ },
210
+ handleChange (event ) {
211
+ this .$emit (' change' , event .target .value );
212
+ },
213
+ setNativeInputValue () {
214
+ const input = this .getInput ();
215
+ if (! input) return ;
216
+ if (input .value === this .nativeInputValue ) return ;
217
+ input .value = this .nativeInputValue ;
218
+ },
219
+ },
220
+ watch: {
221
+ nativeInputValue () {
222
+ this .setNativeInputValue ();
223
+ },
224
+ type () {
225
+ this .$nextTick (() => {
226
+ this .setNativeInputValue ();
227
+ });
225
228
},
226
229
},
227
230
};
0 commit comments