feat: add Fieldset and SearchField infrastructure
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
getCurrentInstance,
|
||||
nextTick,
|
||||
onMounted,
|
||||
provide,
|
||||
ref,
|
||||
} from "vue";
|
||||
import { searchFieldVariants } from "@heroui-vue/styles";
|
||||
import { textFieldProps, useTextField } from "../field/useTextField";
|
||||
import { searchFieldContextKey } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "SearchField",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
...textFieldProps,
|
||||
type: { type: String, default: "search" },
|
||||
autoFocus: Boolean,
|
||||
},
|
||||
emits: {
|
||||
"update:modelValue": (_value: string) => true,
|
||||
submit: (_value: string) => true,
|
||||
clear: () => true,
|
||||
},
|
||||
setup(props, context) {
|
||||
const instance = getCurrentInstance();
|
||||
const hovered = ref(false),
|
||||
focused = ref(false),
|
||||
focusVisible = ref(false);
|
||||
const slots = computed(() =>
|
||||
searchFieldVariants({
|
||||
variant: props.variant,
|
||||
fullWidth: props.fullWidth,
|
||||
}),
|
||||
);
|
||||
const { field, element, render } = useTextField(props, context, {
|
||||
rootSlot: "search-field",
|
||||
rootClass: () => slots.value.base(),
|
||||
rootAttrs: () => ({
|
||||
onKeydown: keydown,
|
||||
"data-empty": !field.value.value || undefined,
|
||||
"data-focus-within": focused.value || undefined,
|
||||
"data-focus-visible": focusVisible.value || undefined,
|
||||
"data-hovered": (!field.disabled.value && hovered.value) || undefined,
|
||||
}),
|
||||
slotState: () => ({
|
||||
isEmpty: !field.value.value,
|
||||
isHovered: !field.disabled.value && hovered.value,
|
||||
isFocusWithin: focused.value,
|
||||
isFocusVisible: focusVisible.value,
|
||||
}),
|
||||
});
|
||||
function clear(event?: Event) {
|
||||
const input = element.value;
|
||||
if (
|
||||
event?.defaultPrevented ||
|
||||
!input ||
|
||||
field.disabled.value ||
|
||||
field.readOnly.value ||
|
||||
!field.value.value
|
||||
)
|
||||
return;
|
||||
// The native input path also updates an explicitly controlled leaf and
|
||||
// restores rejected values through the same useTextControl adapter.
|
||||
input.value = "";
|
||||
input.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
context.emit("clear");
|
||||
void nextTick(() => input.focus());
|
||||
}
|
||||
function keydown(event: KeyboardEvent) {
|
||||
if (event.target !== element.value) return;
|
||||
if (
|
||||
event.defaultPrevented ||
|
||||
event.isComposing ||
|
||||
event.keyCode === 229 ||
|
||||
event.repeat ||
|
||||
field.disabled.value
|
||||
)
|
||||
return;
|
||||
if (
|
||||
event.key === "Escape" &&
|
||||
!field.readOnly.value &&
|
||||
field.value.value
|
||||
) {
|
||||
event.preventDefault();
|
||||
clear();
|
||||
} else if (
|
||||
event.key === "Enter" &&
|
||||
(instance?.vnode.props?.onSubmit || instance?.vnode.props?.onSubmitOnce)
|
||||
) {
|
||||
event.preventDefault();
|
||||
if (
|
||||
field.validationBehavior.value === "native" &&
|
||||
element.value &&
|
||||
!element.value.reportValidity()
|
||||
)
|
||||
return;
|
||||
context.emit("submit", field.value.value);
|
||||
}
|
||||
// Without @submit, let the browser perform the enclosing form's normal
|
||||
// implicit submission. Never synthesize a second form submission.
|
||||
}
|
||||
provide(searchFieldContextKey, {
|
||||
field,
|
||||
slots,
|
||||
hovered,
|
||||
focused,
|
||||
focusVisible,
|
||||
clear,
|
||||
keydown,
|
||||
});
|
||||
onMounted(() => {
|
||||
if (props.autoFocus && !field.disabled.value) element.value?.focus();
|
||||
});
|
||||
return render;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { inject } from "vue";
|
||||
import {
|
||||
searchFieldVariants,
|
||||
type CloseButtonVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import CloseButton from "../close-button/CloseButton.vue";
|
||||
import { searchFieldContextKey } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
defineProps<{
|
||||
variant?: CloseButtonVariants["variant"];
|
||||
isDisabled?: boolean;
|
||||
disabled?: boolean;
|
||||
}>();
|
||||
const context = inject(searchFieldContextKey, undefined);
|
||||
const fallback = searchFieldVariants();
|
||||
</script>
|
||||
<template>
|
||||
<CloseButton
|
||||
v-bind="$attrs"
|
||||
:class="[
|
||||
context?.slots.value.clearButton() ?? fallback.clearButton(),
|
||||
$attrs.class,
|
||||
]"
|
||||
:variant="variant"
|
||||
:is-disabled="
|
||||
isDisabled ||
|
||||
disabled ||
|
||||
context?.field.disabled.value ||
|
||||
context?.field.readOnly.value
|
||||
"
|
||||
:aria-label="$attrs['aria-label'] ?? 'Clear'"
|
||||
:aria-hidden="!context?.field.value.value || undefined"
|
||||
:tabindex="
|
||||
!context?.field.value.value ? -1 : ($attrs.tabindex as number | undefined)
|
||||
"
|
||||
data-slot="search-field-clear-button"
|
||||
slot="clear"
|
||||
@click="context?.clear"
|
||||
>
|
||||
<template v-if="$slots.default" #default="state">
|
||||
<slot v-bind="state" />
|
||||
</template>
|
||||
</CloseButton>
|
||||
</template>
|
||||
@@ -0,0 +1,50 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, watch } from "vue";
|
||||
import { searchFieldVariants } from "@heroui-vue/styles";
|
||||
import { searchFieldContextKey } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const context = inject(searchFieldContextKey, undefined);
|
||||
const fallback = searchFieldVariants();
|
||||
watch(
|
||||
() => context?.field.disabled.value,
|
||||
(disabled) => {
|
||||
if (disabled && context) {
|
||||
context.hovered.value = false;
|
||||
context.focused.value = false;
|
||||
context.focusVisible.value = false;
|
||||
}
|
||||
},
|
||||
);
|
||||
function blur(event: FocusEvent) {
|
||||
if (
|
||||
context &&
|
||||
!(event.currentTarget as HTMLElement).contains(
|
||||
event.relatedTarget as Node | null,
|
||||
)
|
||||
) {
|
||||
context.focused.value = false;
|
||||
context.focusVisible.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
:class="[context?.slots.value.group() ?? fallback.group(), $attrs.class]"
|
||||
:role="typeof $attrs.role === 'string' ? $attrs.role : 'group'"
|
||||
data-slot="search-field-group"
|
||||
:data-disabled="context?.field.disabled.value || undefined"
|
||||
:data-invalid="context?.field.invalid.value || undefined"
|
||||
:data-hovered="context?.hovered.value || undefined"
|
||||
:data-focus-within="context?.focused.value || undefined"
|
||||
:aria-disabled="context?.field.disabled.value || undefined"
|
||||
@pointerenter="
|
||||
context && (context.hovered.value = !context.field.disabled.value)
|
||||
"
|
||||
@pointerleave="context && (context.hovered.value = false)"
|
||||
@focusin="context && (context.focused.value = true)"
|
||||
@focusout="blur"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,51 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { useTextControl } from "../field/useTextControl";
|
||||
import { searchFieldContextKey } 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(searchFieldContextKey, 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="search-field-input"
|
||||
@input="control.update"
|
||||
@invalid="control.onInvalid"
|
||||
@focus="
|
||||
group &&
|
||||
(group.focusVisible.value = ($event.target as HTMLElement).matches(
|
||||
':focus-visible',
|
||||
))
|
||||
"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,48 @@
|
||||
<script setup lang="ts">
|
||||
import { inject } from "vue";
|
||||
import { Primitive } from "reka-ui";
|
||||
import { searchFieldVariants } from "@heroui-vue/styles";
|
||||
import { searchFieldContextKey } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const context = inject(searchFieldContextKey, undefined);
|
||||
const fallback = searchFieldVariants();
|
||||
</script>
|
||||
<template>
|
||||
<Primitive
|
||||
v-if="$slots.default"
|
||||
as-child
|
||||
v-bind="$attrs"
|
||||
:class="[
|
||||
context?.slots.value.searchIcon() ?? fallback.searchIcon(),
|
||||
$attrs.class,
|
||||
]"
|
||||
aria-hidden="true"
|
||||
data-slot="search-field-search-icon"
|
||||
>
|
||||
<slot />
|
||||
</Primitive>
|
||||
<!-- Default IconSearch ported from the fixed HeroUI icons.tsx baseline; see HEROUI-LICENSE. -->
|
||||
<svg
|
||||
v-else
|
||||
v-bind="$attrs"
|
||||
:class="[
|
||||
context?.slots.value.searchIcon() ?? fallback.searchIcon(),
|
||||
$attrs.class,
|
||||
]"
|
||||
aria-hidden="true"
|
||||
fill="none"
|
||||
height="16"
|
||||
role="presentation"
|
||||
viewBox="0 0 16 16"
|
||||
width="16"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
data-slot="search-field-search-icon"
|
||||
>
|
||||
<path
|
||||
clip-rule="evenodd"
|
||||
d="M11.5 7a4.5 4.5 0 1 1-9 0a4.5 4.5 0 0 1 9 0m-.82 4.74a6 6 0 1 1 1.06-1.06l2.79 2.79a.75.75 0 1 1-1.06 1.06z"
|
||||
fill="currentColor"
|
||||
fill-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</template>
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { ComputedRef, InjectionKey, Ref } from "vue";
|
||||
import type { searchFieldVariants } from "@heroui-vue/styles";
|
||||
import type { TextFieldContext } from "../textfield/context";
|
||||
export interface SearchFieldContext {
|
||||
field: TextFieldContext;
|
||||
slots: ComputedRef<ReturnType<typeof searchFieldVariants>>;
|
||||
hovered: Ref<boolean>;
|
||||
focused: Ref<boolean>;
|
||||
focusVisible: Ref<boolean>;
|
||||
clear: (event?: Event) => void;
|
||||
keydown: (event: KeyboardEvent) => void;
|
||||
}
|
||||
export const searchFieldContextKey: InjectionKey<SearchFieldContext> = Symbol(
|
||||
"HeroUIVue.SearchField",
|
||||
);
|
||||
Reference in New Issue
Block a user