Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 5128e25

Browse files
committed
Refactored textbox
1 parent ee0eb9d commit 5128e25

File tree

1 file changed

+75
-72
lines changed

1 file changed

+75
-72
lines changed

src/components/Textbox/main.vue

Lines changed: 75 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@
1313
:name="name"
1414
:id="id"
1515
:tabindex="tabindex"
16-
:max="maxValue"
16+
:max="max"
1717
:maxlength="maxlength ? maxlength : null"
18-
:min="minValue"
18+
:min="min"
1919
:number="type === 'number' ? true : null"
20-
:step="stepValue"
20+
:step="step"
2121
:type="type"
22-
:value="value"
2322

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"
3027

3128
ref="input"
3229
class="textbox__input"
@@ -45,14 +42,11 @@
4542
:id="id"
4643
:rows="rows"
4744
:tabindex="tabindex"
48-
:value="value"
4945

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"
5650

5751
ref="textarea"
5852
class="textbox__textarea"
@@ -109,11 +103,17 @@ export default {
109103
type: Number,
110104
default: 2,
111105
},
112-
min: Number,
113-
max: Number,
106+
min: {
107+
type: Number,
108+
default: -Infinity,
109+
},
110+
max: {
111+
type: Number,
112+
default: Infinity,
113+
},
114114
step: {
115-
type: String,
116-
default: 'any',
115+
type: Number,
116+
default: 1,
117117
},
118118
maxlength: Number,
119119
readonly: {
@@ -146,21 +146,6 @@ export default {
146146
},
147147
},
148148
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-
},
164149
classes() {
165150
return [
166151
`textbox--size-${this.size}`,
@@ -170,58 +155,76 @@ export default {
170155
{ 'textbox--error': this.error },
171156
];
172157
},
158+
nativeInputValue() {
159+
return this.value === null || this.value === undefined ? '' : String(this.value);
160+
},
173161
},
174162
data() {
175163
return {
176164
isTouched: false,
165+
isFocused: false,
177166
initialValue: this.value,
178167
};
179168
},
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();
186171
},
187172
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;
193175
},
194-
onFocus(e) {
195-
this.$emit('focus', e);
176+
focus() {
177+
this.getInput().focus();
196178
},
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();
203181
},
204-
onKeydown(e) {
205-
this.$emit('keydown', e);
182+
select() {
183+
this.getInput().select();
206184
},
207-
onKeydownEnter(e) {
208-
this.$emit('keydown-enter', e);
185+
clear() {
186+
this.$emit('input', '');
187+
this.$emit('change', '');
188+
this.$emit('clear');
209189
},
210190
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');
222194
},
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+
});
225228
},
226229
},
227230
};

0 commit comments

Comments
 (0)