feat: add InputGroup and verify shared field integration
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, provide, ref, useAttrs } from "vue";
|
||||
import {
|
||||
inputGroupVariants,
|
||||
type InputGroupVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import { textFieldContextKey } from "../textfield/context";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
fullWidth?: boolean;
|
||||
variant?: InputGroupVariants["variant"];
|
||||
role?: "group" | "region" | "presentation";
|
||||
}>(),
|
||||
{
|
||||
fullWidth: false,
|
||||
role: "group",
|
||||
},
|
||||
);
|
||||
|
||||
const attrs = useAttrs();
|
||||
const textField = inject(textFieldContextKey, undefined);
|
||||
|
||||
const groupEl = ref<HTMLElement>();
|
||||
const hovered = ref(false);
|
||||
const focusWithin = ref(false);
|
||||
|
||||
/** Explicit variant wins over TextField inheritance. */
|
||||
const resolvedVariant = computed<InputGroupVariants["variant"]>(
|
||||
() => props.variant ?? textField?.variant.value,
|
||||
);
|
||||
|
||||
const slots = computed(() =>
|
||||
inputGroupVariants({
|
||||
fullWidth: props.fullWidth,
|
||||
variant: resolvedVariant.value,
|
||||
}),
|
||||
);
|
||||
|
||||
provide(inputGroupContextKey, { slots });
|
||||
|
||||
const invalid = computed(() => Boolean(textField?.invalid.value));
|
||||
const disabled = computed(() => Boolean(textField?.disabled.value));
|
||||
|
||||
function focusControl(e: MouseEvent) {
|
||||
const group = groupEl.value;
|
||||
if (!group) return;
|
||||
const target = e.target as HTMLElement | null;
|
||||
const control = group.querySelector<HTMLElement>(
|
||||
'input[data-slot="input-group-input"], textarea[data-slot="input-group-textarea"]',
|
||||
);
|
||||
if (!control || control === target || control.contains(target)) return;
|
||||
// Keep real focus on the actual input/textarea so value and caret stay intact.
|
||||
if (
|
||||
target?.closest(
|
||||
'button, a, input, textarea, select, [role="button"], [tabindex]',
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
control.focus();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
ref="groupEl"
|
||||
v-bind="attrs"
|
||||
:class="[slots.base(), attrs.class]"
|
||||
:role="role"
|
||||
:data-hovered="hovered || undefined"
|
||||
:data-focus-within="focusWithin || undefined"
|
||||
:data-invalid="invalid || undefined"
|
||||
:data-disabled="disabled || undefined"
|
||||
:aria-disabled="disabled || undefined"
|
||||
data-slot="input-group"
|
||||
@click="focusControl"
|
||||
@pointerenter="hovered = true"
|
||||
@pointerleave="hovered = false"
|
||||
@focusin="focusWithin = true"
|
||||
@focusout="focusWithin = false"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
invalid?: boolean;
|
||||
type?: string;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
const control = useTextControl(props, attrs, emit);
|
||||
const { element, value } = control;
|
||||
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<input
|
||||
ref="element"
|
||||
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="[group?.slots.value.input(), attrs.class]"
|
||||
: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-group-input"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-bind="attrs"
|
||||
:class="[group?.slots.value.prefix(), attrs.class]"
|
||||
data-slot="input-group-prefix"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-bind="attrs"
|
||||
:class="[group?.slots.value.suffix(), attrs.class]"
|
||||
data-slot="input-group-suffix"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
import { inputGroupContextKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue?: string;
|
||||
defaultValue?: string;
|
||||
invalid?: boolean;
|
||||
rows?: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
|
||||
const attrs = useAttrs();
|
||||
const group = inject(inputGroupContextKey, undefined);
|
||||
const control = useTextControl(props, attrs, emit);
|
||||
const { element, value } = control;
|
||||
|
||||
defineExpose({ focus: () => element.value?.focus(), element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<textarea
|
||||
ref="element"
|
||||
v-bind="{ ...control.inheritedAttrs(), ...attrs }"
|
||||
:value="value"
|
||||
:id="control.id()"
|
||||
:name="control.name()"
|
||||
:rows="rows"
|
||||
:disabled="control.disabled()"
|
||||
:readonly="control.readOnly()"
|
||||
:required="control.required()"
|
||||
:class="[group?.slots.value.input(), attrs.class]"
|
||||
: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-group-textarea"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { ComputedRef, InjectionKey } from "vue";
|
||||
import type { inputGroupVariants } from "@heroui-vue/styles";
|
||||
|
||||
export type InputGroupSlots = ReturnType<typeof inputGroupVariants>;
|
||||
|
||||
export interface InputGroupContextValue {
|
||||
slots: ComputedRef<InputGroupSlots>;
|
||||
}
|
||||
|
||||
export const inputGroupContextKey: InjectionKey<InputGroupContextValue> =
|
||||
Symbol("HeroUIVue.InputGroup");
|
||||
@@ -213,7 +213,12 @@ export default defineComponent({
|
||||
const position = `${path}-${index}`;
|
||||
const type = node.type as { name?: string; __name?: string };
|
||||
const name = type?.name ?? type?.__name;
|
||||
if (name === "Input" || name === "TextArea")
|
||||
if (
|
||||
name === "Input" ||
|
||||
name === "TextArea" ||
|
||||
name === "InputGroupInput" ||
|
||||
name === "InputGroupTextArea"
|
||||
)
|
||||
found.controlId = node.props?.id;
|
||||
if (name === "Description" || name === "FieldError") {
|
||||
const kind = name === "Description" ? "description" : "error";
|
||||
@@ -221,6 +226,22 @@ export default defineComponent({
|
||||
found[kind].push(String(id));
|
||||
return cloneVNode(node, { id });
|
||||
}
|
||||
// InputGroup is a library-owned transparent field wrapper. Prepare its
|
||||
// default slot once, preserving the current render's values and keys.
|
||||
// Arbitrary consumer components remain opaque to SSR planning.
|
||||
if (
|
||||
name === "InputGroup" &&
|
||||
node.children &&
|
||||
typeof node.children === "object"
|
||||
) {
|
||||
const groupSlots = node.children as { default?: () => VNodeChild[] };
|
||||
if (typeof groupSlots.default === "function") {
|
||||
const prepared = prepare(groupSlots.default(), found, position);
|
||||
const clone = cloneVNode(node);
|
||||
clone.children = { ...groupSlots, default: () => prepared };
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
if (
|
||||
(typeof node.type === "string" || node.type === Fragment) &&
|
||||
Array.isArray(node.children)
|
||||
|
||||
Reference in New Issue
Block a user