feat: migrate Select with field, collection and SSR contracts
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts" generic="T extends ListBoxDataItem = ListBoxDataItem">
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
getCurrentInstance,
|
||||
mergeProps,
|
||||
nextTick,
|
||||
@@ -17,6 +18,7 @@ import { useConfig } from "../../providers/context";
|
||||
import { useInheritedDisabled, disabledContextKey } from "../field/disabled";
|
||||
import { useCollectionSelection } from "../collection/selection";
|
||||
import { useCollectionRegistry } from "../collection/registry";
|
||||
import { collectionOwnerKey } from "../collection/owner";
|
||||
import { assertKey } from "../collection/keys";
|
||||
import {
|
||||
listBoxKey,
|
||||
@@ -36,6 +38,21 @@ const props = withDefaults(defineProps<ListBoxOptions<T>>(), {
|
||||
rowHeight: 50,
|
||||
overscan: 5,
|
||||
});
|
||||
const owner = inject(collectionOwnerKey, undefined);
|
||||
provide(collectionOwnerKey, undefined);
|
||||
const mode = computed(() => owner?.mode.value ?? props.selectionMode);
|
||||
const behavior = computed(() =>
|
||||
owner
|
||||
? mode.value === "single"
|
||||
? "replace"
|
||||
: "toggle"
|
||||
: props.selectionBehavior,
|
||||
);
|
||||
const readOnly = computed(() => props.isReadOnly || !!owner?.readOnly.value);
|
||||
const disabledKeys = computed(() => [
|
||||
...(props.disabledKeys ?? []),
|
||||
...(owner?.disabledKeys.value ?? []),
|
||||
]);
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [keys: ListBoxKey[]];
|
||||
selectionChange: [keys: ListBoxKey[]];
|
||||
@@ -62,13 +79,32 @@ onBeforeUpdate(() => {
|
||||
controlled.value = hasModel.value || selectedPresence();
|
||||
});
|
||||
const config = useConfig();
|
||||
const disabled = useInheritedDisabled(() => props.isDisabled);
|
||||
const disabled = useInheritedDisabled(
|
||||
() => props.isDisabled || owner?.disabled.value,
|
||||
);
|
||||
provide(disabledContextKey, disabled);
|
||||
const dir = computed(() => props.dir ?? config?.dir.value ?? "ltr");
|
||||
const registry = useCollectionRegistry<ListBoxMember>();
|
||||
const mounted = ref(false);
|
||||
let disconnectOwner: (() => void) | undefined;
|
||||
onMounted(() => {
|
||||
mounted.value = true;
|
||||
if (owner)
|
||||
disconnectOwner = owner.connect({
|
||||
items: known,
|
||||
focus: async (edge) => {
|
||||
const available = eligible();
|
||||
const key =
|
||||
edge === "last"
|
||||
? available.at(-1)?.key
|
||||
: edge === "first"
|
||||
? available[0]?.key
|
||||
: (available.find((item) => selection.selected(item.key))?.key ??
|
||||
available[0]?.key);
|
||||
if (key !== undefined) await focus(key);
|
||||
else root.value?.focus();
|
||||
},
|
||||
});
|
||||
});
|
||||
const data = computed(() => {
|
||||
const seen = new Set<ListBoxKey>();
|
||||
@@ -79,14 +115,14 @@ const data = computed(() => {
|
||||
seen.add(item.id);
|
||||
return {
|
||||
key: item.id,
|
||||
disabled: !!item.isDisabled || !!props.disabledKeys?.includes(item.id),
|
||||
disabled: !!item.isDisabled || !!disabledKeys.value.includes(item.id),
|
||||
text: item.textValue ?? "",
|
||||
};
|
||||
});
|
||||
});
|
||||
const keyDisabled = (key: ListBoxKey) =>
|
||||
disabled.value ||
|
||||
!!props.disabledKeys?.includes(key) ||
|
||||
!!disabledKeys.value.includes(key) ||
|
||||
!!data.value?.find((item) => item.key === key)?.disabled;
|
||||
const known = () => {
|
||||
const members = registry.ordered();
|
||||
@@ -115,15 +151,18 @@ const blockedKeys = computed(() =>
|
||||
.map((item) => item.key),
|
||||
);
|
||||
const selection = useCollectionSelection({
|
||||
mode: () => props.selectionMode,
|
||||
controlled: () => controlled.value,
|
||||
value: () => (hasModel.value ? props.modelValue : props.selectedKeys),
|
||||
mode: () => mode.value,
|
||||
controlled: () => !!owner || controlled.value,
|
||||
value: () =>
|
||||
owner?.keys.value ??
|
||||
(hasModel.value ? props.modelValue : props.selectedKeys),
|
||||
defaultValue: props.defaultValue ?? props.defaultSelectedKeys,
|
||||
disabled: () => disabled.value,
|
||||
readOnly: () => props.isReadOnly,
|
||||
readOnly: () => readOnly.value,
|
||||
disabledKeys: () => blockedKeys.value,
|
||||
disallowEmptySelection: () => props.disallowEmptySelection,
|
||||
onChange: (keys) => {
|
||||
owner?.request(keys);
|
||||
emit("update:modelValue", [...keys]);
|
||||
emit("selectionChange", [...keys]);
|
||||
},
|
||||
@@ -137,9 +176,8 @@ const active = computed(() => {
|
||||
const items = eligible();
|
||||
return (
|
||||
items.find((item) => item.key === lastFocused.value)?.key ??
|
||||
items.find(
|
||||
(item) => props.selectionMode !== "none" && selection.selected(item.key),
|
||||
)?.key ??
|
||||
items.find((item) => mode.value !== "none" && selection.selected(item.key))
|
||||
?.key ??
|
||||
items[0]?.key
|
||||
);
|
||||
});
|
||||
@@ -165,6 +203,7 @@ function restoreFocus() {
|
||||
let focusRequest = 0;
|
||||
let disposing = false;
|
||||
onBeforeUnmount(() => {
|
||||
disconnectOwner?.();
|
||||
disposing = true;
|
||||
focusRequest++;
|
||||
});
|
||||
@@ -203,22 +242,19 @@ function activate(key: ListBoxKey, event: MouseEvent | KeyboardEvent) {
|
||||
if (
|
||||
event.defaultPrevented ||
|
||||
disabled.value ||
|
||||
props.isReadOnly ||
|
||||
readOnly.value ||
|
||||
!item ||
|
||||
item.disabled
|
||||
)
|
||||
return;
|
||||
if (props.selectionMode === "multiple" && event.shiftKey) {
|
||||
if (mode.value === "multiple" && event.shiftKey) {
|
||||
anchor ??= selection.keys.value[0] ?? active.value ?? key;
|
||||
selectRange(anchor, key);
|
||||
} else if (
|
||||
props.selectionBehavior === "replace" &&
|
||||
!event.ctrlKey &&
|
||||
!event.metaKey
|
||||
)
|
||||
} else if (behavior.value === "replace" && !event.ctrlKey && !event.metaKey)
|
||||
selection.replace(key);
|
||||
else selection.toggle(key);
|
||||
if (!event.shiftKey) anchor = key;
|
||||
owner?.action(key);
|
||||
emit("action", key);
|
||||
}
|
||||
function selectRange(startKey: ListBoxKey, endKey: ListBoxKey) {
|
||||
@@ -245,13 +281,23 @@ function keydown(event: KeyboardEvent) {
|
||||
(event.target as Element).closest("[data-hv-listbox]") !== root.value
|
||||
)
|
||||
return;
|
||||
if (
|
||||
owner &&
|
||||
event.key === "Tab" &&
|
||||
!event.altKey &&
|
||||
!event.ctrlKey &&
|
||||
!event.metaKey
|
||||
) {
|
||||
owner.leave();
|
||||
return;
|
||||
}
|
||||
keyboard.value = true;
|
||||
const items = eligible();
|
||||
if (!items.length) return;
|
||||
if (
|
||||
(event.ctrlKey || event.metaKey) &&
|
||||
event.key.toLowerCase() === "a" &&
|
||||
props.selectionMode === "multiple"
|
||||
mode.value === "multiple"
|
||||
) {
|
||||
event.preventDefault();
|
||||
selection.selectAll(items.map((item) => item.key));
|
||||
@@ -296,11 +342,11 @@ function keydown(event: KeyboardEvent) {
|
||||
? (target + items.length) % items.length
|
||||
: Math.max(0, Math.min(target, items.length - 1));
|
||||
const key = items[target].key;
|
||||
if (event.shiftKey && props.selectionMode === "multiple") {
|
||||
if (event.shiftKey && mode.value === "multiple") {
|
||||
anchor ??= active.value;
|
||||
if (anchor !== undefined) selectRange(anchor, key);
|
||||
} else if (
|
||||
props.selectionBehavior === "replace" &&
|
||||
behavior.value === "replace" &&
|
||||
!event.ctrlKey &&
|
||||
!event.metaKey
|
||||
) {
|
||||
@@ -318,7 +364,7 @@ function keydown(event: KeyboardEvent) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (props.isReadOnly) {
|
||||
if (readOnly.value) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
@@ -380,8 +426,8 @@ function contextOwnsEvent(event: KeyboardEvent) {
|
||||
return (event.target as Element).closest("[data-hv-listbox]") === root.value;
|
||||
}
|
||||
provide(listBoxKey, {
|
||||
mode: computed(() => props.selectionMode),
|
||||
readOnly: computed(() => props.isReadOnly),
|
||||
mode: computed(() => mode.value),
|
||||
readOnly: computed(() => readOnly.value),
|
||||
keyboard,
|
||||
focused,
|
||||
noteFocus: (key) => {
|
||||
@@ -390,7 +436,7 @@ provide(listBoxKey, {
|
||||
},
|
||||
pressed,
|
||||
active,
|
||||
selected: (key) => props.selectionMode !== "none" && selection.selected(key),
|
||||
selected: (key) => mode.value !== "none" && selection.selected(key),
|
||||
disabled: keyDisabled,
|
||||
register: registry.register,
|
||||
activate,
|
||||
@@ -406,7 +452,7 @@ provide(listBoxKey, {
|
||||
: undefined,
|
||||
});
|
||||
function rootAttrs() {
|
||||
if (props.selectionMode === "single" && selection.keys.value.length > 1)
|
||||
if (mode.value === "single" && selection.keys.value.length > 1)
|
||||
throw new TypeError("Single ListBox accepts at most one selected key.");
|
||||
if (
|
||||
props.virtualized &&
|
||||
@@ -418,6 +464,13 @@ function rootAttrs() {
|
||||
// Force duplicate validation even when a scoped item renderer is absent.
|
||||
void data.value;
|
||||
return mergeProps(attrs, {
|
||||
id: owner?.id ?? attrs.id,
|
||||
"aria-label": attrs["aria-label"] ?? owner?.ariaLabel(),
|
||||
"aria-labelledby":
|
||||
attrs["aria-labelledby"] ??
|
||||
((attrs["aria-label"] ?? owner?.ariaLabel())
|
||||
? undefined
|
||||
: owner?.labelledBy()),
|
||||
class: listboxVariants({ variant: props.variant }),
|
||||
role: "listbox",
|
||||
dir: dir.value,
|
||||
@@ -425,12 +478,10 @@ function rootAttrs() {
|
||||
"data-hv-listbox": "",
|
||||
"data-disabled": disabled.value || undefined,
|
||||
"aria-disabled": disabled.value || undefined,
|
||||
"aria-readonly": props.isReadOnly || undefined,
|
||||
"aria-readonly": readOnly.value || undefined,
|
||||
"aria-orientation": props.orientation,
|
||||
"aria-multiselectable":
|
||||
props.selectionMode === "none"
|
||||
? undefined
|
||||
: props.selectionMode === "multiple",
|
||||
mode.value === "none" ? undefined : mode.value === "multiple",
|
||||
tabindex:
|
||||
disabled.value ||
|
||||
(mounted.value &&
|
||||
|
||||
Reference in New Issue
Block a user