feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<script setup lang="ts">
|
||||
import { inputVariants, type InputVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
computed,
|
||||
ref,
|
||||
onMounted,
|
||||
onBeforeUnmount,
|
||||
nextTick,
|
||||
useAttrs,
|
||||
} from "vue";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
invalid?: boolean;
|
||||
variant?: InputVariants["variant"];
|
||||
fullWidth?: boolean;
|
||||
type?: string;
|
||||
}>();
|
||||
const attrs = useAttrs();
|
||||
const invalidState = computed(
|
||||
() =>
|
||||
props.invalid ||
|
||||
attrs["aria-invalid"] === true ||
|
||||
attrs["aria-invalid"] === "true",
|
||||
);
|
||||
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
|
||||
const internal = ref(props.defaultValue ?? "");
|
||||
const value = computed(() => props.modelValue ?? internal.value);
|
||||
const element = ref<HTMLInputElement>();
|
||||
function update(event: Event) {
|
||||
const next = (event.target as HTMLInputElement).value;
|
||||
internal.value = next;
|
||||
emit("update:modelValue", next);
|
||||
void nextTick(() => {
|
||||
if (element.value && element.value.value !== value.value)
|
||||
element.value.value = value.value;
|
||||
});
|
||||
}
|
||||
function reset(event: Event) {
|
||||
void nextTick(() => {
|
||||
if (event.defaultPrevented) return;
|
||||
internal.value = props.defaultValue ?? "";
|
||||
if (element.value) element.value.value = value.value;
|
||||
});
|
||||
}
|
||||
let form: HTMLFormElement | null = null;
|
||||
onMounted(() => {
|
||||
form = element.value?.form ?? null;
|
||||
form?.addEventListener("reset", reset);
|
||||
});
|
||||
onBeforeUnmount(() => form?.removeEventListener("reset", reset));
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
<template>
|
||||
<input
|
||||
ref="element"
|
||||
v-bind="$attrs"
|
||||
:value="value"
|
||||
:class="inputVariants({ variant, fullWidth })"
|
||||
:type="type ?? 'text'"
|
||||
:data-invalid="invalidState || undefined"
|
||||
:aria-invalid="invalidState || undefined"
|
||||
:aria-readonly="
|
||||
$attrs.readonly === '' ||
|
||||
$attrs.readonly === true ||
|
||||
$attrs.readOnly === true ||
|
||||
undefined
|
||||
"
|
||||
data-slot="input"
|
||||
@input="update"
|
||||
/>
|
||||
</template>
|
||||
Reference in New Issue
Block a user