-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathTextField.vue
101 lines (97 loc) · 2.3 KB
/
TextField.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
<template>
<div v-if="!inline" class="form-control w-full">
<label class="label">
<span class="label-text"> {{ label }} </span>
<span
:class="{
'text-red-600':
typeof value === 'string' &&
((maxLength !== -1 && value.length > maxLength) || (minLength !== -1 && value.length < minLength)),
}"
>
{{ typeof value === "string" && (maxLength !== -1 || minLength !== -1) ? `${value.length}/${maxLength}` : "" }}
</span>
</label>
<input
ref="input"
v-model="value"
:placeholder="placeholder"
:type="type"
:required="required"
class="input input-bordered w-full"
/>
</div>
<div v-else class="sm:grid sm:grid-cols-4 sm:items-start sm:gap-4">
<label class="label">
<span class="label-text"> {{ label }} </span>
<span
:class="{
'text-red-600':
typeof value === 'string' &&
((maxLength !== -1 && value.length > maxLength) || (minLength !== -1 && value.length < minLength)),
}"
>
{{ typeof value === "string" && (maxLength !== -1 || minLength !== -1) ? `${value.length}/${maxLength}` : "" }}
</span>
</label>
<input
v-model="value"
:placeholder="placeholder"
:type="type"
:required="required"
class="input input-bordered col-span-3 mt-2 w-full"
/>
</div>
</template>
<script lang="ts" setup>
const props = defineProps({
label: {
type: String,
default: "",
},
modelValue: {
type: [String, Number],
default: null,
},
required: {
type: [Boolean],
default: null,
},
type: {
type: String,
default: "text",
},
triggerFocus: {
type: Boolean,
default: null,
},
inline: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: "",
},
maxLength: {
type: Number,
default: -1,
required: false,
},
minLength: {
type: Number,
default: -1,
required: false,
},
});
const input = ref<HTMLElement | null>(null);
whenever(
() => props.triggerFocus,
() => {
if (input.value) {
input.value.focus();
}
}
);
const value = useVModel(props, "modelValue");
</script>