Files
heroui-vue/packages/vue/src/components/input-otp/InputOTPSlot.vue
T

197 lines
6.2 KiB
Vue

<script lang="ts">
import { computed, defineComponent, h, inject, ref, useAttrs } from "vue";
import { PinInputInput } from "reka-ui";
import { inputOTPContextKey } from "./context";
type Handler = (event: Event) => void;
function toHandlerArray(value: unknown): Handler[] {
if (!value) return [];
if (Array.isArray(value)) {
return value.filter((item): item is Handler => typeof item === "function");
}
return typeof value === "function" ? [value as Handler] : [];
}
export default defineComponent({
name: "InputOTPSlot",
inheritAttrs: false,
props: {
index: {
type: Number,
required: true,
},
as: {
type: String,
default: "div",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const context = inject(inputOTPContextKey, null);
const focused = ref(false);
const char = computed(() => context?.chars.value[props.index] ?? "");
const isDisabled = computed(() => Boolean(context?.isDisabled.value));
const isInvalid = computed(() => Boolean(context?.isInvalid.value));
const isReadOnly = computed(() => Boolean(context?.isReadOnly.value));
const isFilled = computed(() => char.value.length > 0);
const isActive = computed(() => focused.value && !isDisabled.value);
const mask = computed(() => Boolean(context?.mask.value));
const displayChar = computed(() => (mask.value ? "•" : char.value));
function onInputFocus() {
focused.value = true;
}
function onInputBlur() {
focused.value = false;
}
/** Block edit keys before Reka handlers when readonly/disabled. */
function guardEdit(event: Event) {
if (!isReadOnly.value && !isDisabled.value) return;
const key = (event as KeyboardEvent).key;
if (
key === "Tab" ||
key === "Escape" ||
key === "ArrowLeft" ||
key === "ArrowRight" ||
key === "ArrowUp" ||
key === "ArrowDown" ||
key === "Home" ||
key === "End"
) {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
}
function guardPaste(event: Event) {
if (!isReadOnly.value && !isDisabled.value) return;
event.preventDefault();
event.stopImmediatePropagation();
}
/** After user handlers: preventDefault means cancel the edit. */
function stopIfPrevented(event: Event) {
if (event.defaultPrevented) event.stopImmediatePropagation();
}
function render() {
const slotClass = context?.slots.value.slot();
const valueClass = context?.slots.value.slotValue();
const caretClass = context?.slots.value.caret();
const inputClass = [
context?.slots.value.input(),
context?.inputClassName.value,
];
const {
class: cls,
onKeydown: userKeydown,
onKeydownCapture: userKeydownCapture,
onPaste: userPaste,
onPasteCapture: userPasteCapture,
onInputCapture: userInputCapture,
...rest
} = attrs as Record<string, unknown>;
// Vue native handler arrays honor stopImmediatePropagation between
// items. Bubble-phase keydown/paste are promoted into the capture
// list so user preventDefault can cancel Reka's edit handlers.
const keydownCapture: Handler[] = [
guardEdit,
...toHandlerArray(userKeydownCapture),
...toHandlerArray(userKeydown),
stopIfPrevented,
];
const pasteCapture: Handler[] = [
guardPaste,
...toHandlerArray(userPasteCapture),
...toHandlerArray(userPaste),
stopIfPrevented,
(event) => context?.handlePaste(event as ClipboardEvent, props.index),
];
return h(
props.as,
{
...rest,
class: [slotClass, cls],
onInputCapture: [
...toHandlerArray(userInputCapture),
(event: InputEvent) => context?.guardInput(event, props.index),
],
onKeydownCapture: keydownCapture,
onPasteCapture: pasteCapture,
"data-active": isActive.value || undefined,
"data-disabled": isDisabled.value || undefined,
"data-filled": isFilled.value || undefined,
"data-invalid": isInvalid.value || undefined,
"data-readonly": isReadOnly.value || undefined,
"data-slot": "input-otp-slot",
},
[
isFilled.value
? h(
"div",
{
class: valueClass,
"data-slot": "input-otp-slot-value",
},
displayChar.value,
)
: null,
isActive.value
? h("div", {
class: caretClass,
"data-slot": "input-otp-caret",
})
: null,
h(
PinInputInput,
{
asChild: true,
index: props.index,
disabled: isDisabled.value,
readonly: isReadOnly.value || undefined,
class: inputClass,
style: { textAlign: context?.textAlign.value },
id: context?.inputId(props.index),
"data-slot": "input-otp-input",
autocomplete: "one-time-code",
"aria-describedby": context?.describedBy.value,
"aria-invalid": isInvalid.value ? "true" : undefined,
onFocus: onInputFocus,
onBlur: onInputBlur,
},
{
// Own the native ARIA so the primitive's default label cannot
// override the composed field name. Retain its input handlers.
default: () =>
h("input", {
"aria-labelledby":
attrs["aria-labelledby"] ??
(attrs["aria-label"]
? undefined
: context?.labelledBy.value),
"aria-label":
attrs["aria-label"] ??
context?.accessibleLabel.value ??
(context?.labelledBy.value
? undefined
: `pin input ${props.index + 1} of ${context?.maxLength.value ?? 0}`),
}),
},
),
slots.default?.(),
],
);
}
return render;
},
});
</script>