feat: establish shared field and form foundation
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { useAttrs } from "vue";
|
||||
import { inputVariants, type InputVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
computed,
|
||||
ref,
|
||||
onMounted,
|
||||
onBeforeUnmount,
|
||||
nextTick,
|
||||
useAttrs,
|
||||
} from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
@@ -17,57 +11,31 @@ const props = defineProps<{
|
||||
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));
|
||||
const attrs = useAttrs();
|
||||
const control = useTextControl(props, attrs, emit);
|
||||
const { element, value, variant } = control;
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
<template>
|
||||
<input
|
||||
ref="element"
|
||||
v-bind="$attrs"
|
||||
v-bind="{ ...control.inheritedAttrs(), ...attrs }"
|
||||
:value="value"
|
||||
:id="control.id()"
|
||||
:name="control.name()"
|
||||
:type="control.type()"
|
||||
:disabled="control.disabled()"
|
||||
:readonly="control.readOnly()"
|
||||
:required="control.required()"
|
||||
: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-invalid="control.invalid() || undefined"
|
||||
:aria-invalid="control.invalid() || undefined"
|
||||
:aria-readonly="control.readOnly() || undefined"
|
||||
:aria-describedby="control.describedBy()"
|
||||
:data-validation-behavior="control.validationBehavior()"
|
||||
data-slot="input"
|
||||
@input="update"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
/>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user