-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.vue
146 lines (132 loc) · 3.92 KB
/
index.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
<template>
<div>
<div class="flex flex-wrap gap-card-sm items-center">
<label
v-if="!noLabel && label != ''"
class="text-body-2 text-body font-body block mb-2"
:for="id ?? label"
>
{{ t(label) }}
</label>
<p
v-if="hint"
class="pb-2 text-xs text-body relative z-0"
:class="noLabel ? 'text-left' : 'text-right'"
>
{{ t(hint) }}
</p>
</div>
<input
:disabled="disabled"
class="block w-full px-4 py-3 text-base rounded-md relative z-10 transition ease-out duration-500 focus:outline-none appearance-none ring-2 focus:ring-offset-2"
:class="[
{
'invalid:ring-error valid:ring-accent': (touched && input) || error,
'cursor-not-allowed': disabled,
},
light
? 'text-subheading bg-white ring-subheading focus:ring-offset-subheading focus:ring-subheading'
: 'text-white bg-secondary ring-tertiary focus:ring-offset-tertiary focus:ring-accent',
]"
ref="DOM_INPUT"
:placeholder="noLabel ? t(label) : t(placeholder)"
:type="type"
:name="name != '' ? name : label"
:id="id != '' ? id : label"
v-model="input"
@blur="touched = true"
:autocomplete="autocomplete"
:min="min"
/>
<p
v-if="error"
class="pt-2 text-xs relative z-0 transition ease-out duration-500 text-error"
:class="
error ? 'translate-y-0 opacity-100' : 'translate-y-[-100%] opacity-0'
"
>
{{ error }}.
</p>
</div>
</template>
<script lang="ts">
import { useI18n } from "vue-i18n";
export default defineComponent({
props: {
min: { type: String || Number, default: "" },
autocomplete: { type: String, default: "true" },
hint: { type: String, default: "" },
name: { type: String, default: "" },
id: { type: String, default: "" },
type: { type: String, default: "text" },
label: { type: String, default: "" },
noLabel: { type: Boolean, default: false },
noTrim: { type: Boolean, default: false },
light: { type: Boolean, default: false },
placeholder: { type: String, default: "" },
rules: { type: Array, default: [] },
modelValue: { default: "" },
disabled: { type: Boolean, default: false },
focusThis: { type: Boolean, default: false },
},
emits: ["update:modelValue", "valid"],
setup(props, { emit }) {
const { t } = useI18n();
const input = computed({
get() {
return props.modelValue;
},
set(value: string) {
let finalValue: string | number;
if (
props.noTrim ||
props.type == "password" ||
props.type == "number"
) {
finalValue = value;
} else {
finalValue = value.trim();
}
emit("update:modelValue", finalValue);
},
});
const touched = ref(!!props.modelValue);
const DOM_INPUT = ref<HTMLInputElement | null>(null);
const error = computed(() => {
if (!!!DOM_INPUT.value || (!touched.value && !input.value)) return "";
let msg: string = "";
props.rules
.slice()
.reverse()
.forEach((rule: any) => {
if (rule(input.value) != true) {
const [string, placeholder] = rule(input.value).split("_");
if (!!placeholder) {
msg = t(string, {
placeholder: t(placeholder),
});
} else if (!!string) {
msg = t(string);
} else {
msg = t(rule(input.value));
}
}
});
DOM_INPUT.value.setCustomValidity(msg);
emit("valid", !!!msg);
return msg;
});
onMounted(() => {
if (props.focusThis == true) {
DOM_INPUT.value?.focus();
}
});
return { t, input, error, DOM_INPUT, touched };
},
});
</script>
<style scoped>
::-webkit-calendar-picker-indicator {
filter: invert(1);
}
</style>