/
UiInput.vue
164 lines (135 loc) · 3.75 KB
/
UiInput.vue
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
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import type { PropType } from 'vue';
import type { TypeClassList } from '~/assets/utils/types';
import { TYPES_DICT } from '~/assets/utils/constants/input';
// Composable
import { classNameProps, useClassName } from '~/composables/useClassName';
import { sizeProps, useSize } from '~/composables/useSize';
import { colorProps, useColor } from '~/composables/useColor';
import { stateProps, useState } from '~/composables/useState';
import { styleProps, useStyle } from '~/composables/useStyle';
type TypeValue = string | number | null;
const props = defineProps({
...classNameProps,
...sizeProps,
...colorProps,
...stateProps,
...styleProps,
/**
* Input value
*/
modelValue: {
type: [String, Number] as PropType<TypeValue>,
default: '',
},
/**
* Type value for some validations
*/
valueType: {
type: String,
default: TYPES_DICT.STRING,
validator: (val: string) => Object
.values(TYPES_DICT)
.includes(val),
},
});
const { getClassName } = useClassName(props);
const { sizeClassList } = useSize(props);
const { colorClassList } = useColor(props);
const { stateClassList } = useState(props);
const { styleClassList } = useStyle(props);
const classList = computed((): TypeClassList => [
sizeClassList.value,
colorClassList.value,
stateClassList.value,
styleClassList.value,
]);
// Value Logic
const actualValue = ref<TypeValue>('');
const $emit = defineEmits<{
'update:modelValue': [value: TypeValue]
change: [value: TypeValue]
blur: [value: TypeValue]
focus: [value: Event]
}>();
watch(() => props.modelValue, (val: TypeValue) => {
actualValue.value = val !== null ? String(val) : val;
}, {
immediate: true,
});
function setValue(value: TypeValue) {
let newVal: TypeValue = value;
switch (typeof newVal) {
case 'object':
newVal = null;
break;
case 'number':
newVal = newVal.toString();
break;
default:
newVal = !newVal.trim().length ? null : newVal;
break;
}
if (props.valueType === TYPES_DICT.NUMERIC) {
if (newVal) {
newVal = Number(newVal);
}
}
actualValue.value = newVal;
}
function onInput(event: Event) {
const target = event.target as HTMLInputElement;
setValue(target.value);
setTimeout(() => {
$emit('update:modelValue', actualValue.value);
}, event.composed ? 1 : 0);
}
function onChange(event: Event) {
const target = event.target as HTMLInputElement;
setValue(target.value);
$emit('change', actualValue.value);
$emit('blur', actualValue.value);
}
function onEnter(e: Event) {
const target = e.target as HTMLInputElement;
setTimeout(() => {
target.blur();
}, 100);
}
function onBlur(e: Event) {
const target = e.target as HTMLInputElement;
setValue(target.value);
}
function onFocus(e: Event) {
$emit('focus', e);
}
</script>
<!-- todo: Добавить внутрь этого компонента обработки всех типов данных: phone, custom mask, splitThousand -->
<template>
<input
v-bind="$attrs"
:value="actualValue"
:class="[getClassName('Input'), classList]"
:disabled="disabled"
@input="onInput"
@paste="onInput"
@change="onChange"
@keydown.enter="onEnter"
@blur="onBlur"
@focus="onFocus"
/>
</template>
<style lang="scss">
.UiInput {
outline: none;
transition: all .3s ease;
&:focus {
outline: none;
}
&.--is-disabled {
pointer-events: none;
user-select: none;
}
}
</style>