feat: add Disclosure DisclosureGroup and Accordion
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
provide,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { disclosureGroupVariants } from "@heroui-vue/styles";
|
||||
import { useInheritedDisabled } from "../field/disabled";
|
||||
import {
|
||||
disclosureGroupContextKey,
|
||||
useDisclosureGroupState,
|
||||
type DisclosureKey,
|
||||
} from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DisclosureGroup",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
expandedKeys: {
|
||||
type: Array as PropType<DisclosureKey[] | undefined>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultExpandedKeys: {
|
||||
type: Array as PropType<DisclosureKey[]>,
|
||||
default: () => [],
|
||||
},
|
||||
allowsMultipleExpanded: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
as: { type: String, default: "div" },
|
||||
},
|
||||
emits: ["update:expandedKeys", "expandedChange"],
|
||||
setup(props, { slots, emit, attrs }) {
|
||||
const inheritedDisabled = useInheritedDisabled(() => props.isDisabled);
|
||||
const state = useDisclosureGroupState({
|
||||
get expandedKeys() {
|
||||
return props.expandedKeys;
|
||||
},
|
||||
get defaultExpandedKeys() {
|
||||
return props.defaultExpandedKeys;
|
||||
},
|
||||
get allowsMultipleExpanded() {
|
||||
return props.allowsMultipleExpanded;
|
||||
},
|
||||
get isDisabled() {
|
||||
return inheritedDisabled.value;
|
||||
},
|
||||
onExpandedChange(keys) {
|
||||
emit("update:expandedKeys", keys);
|
||||
emit("expandedChange", keys);
|
||||
},
|
||||
});
|
||||
|
||||
provide(disclosureGroupContextKey, {
|
||||
expandedKeys: state.expandedKeys,
|
||||
allowsMultipleExpanded: state.allowsMultipleExpanded,
|
||||
isDisabled: computed(() => inheritedDisabled.value),
|
||||
isExpanded: state.isExpanded,
|
||||
toggle: state.toggle,
|
||||
registerItem: state.registerItem,
|
||||
itemKeys: state.itemKeys,
|
||||
});
|
||||
|
||||
const slotClasses = disclosureGroupVariants({});
|
||||
|
||||
return () => {
|
||||
const raw =
|
||||
slots.default?.({
|
||||
expandedKeys: state.expandedKeys.value,
|
||||
isDisabled: inheritedDisabled.value,
|
||||
}) ?? [];
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
class: [slotClasses.base(), attrs.class],
|
||||
"data-slot": "disclosure-group",
|
||||
"data-disabled": inheritedDisabled.value ? "true" : undefined,
|
||||
"aria-disabled": inheritedDisabled.value ? true : undefined,
|
||||
},
|
||||
raw,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,164 @@
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
provide,
|
||||
ref,
|
||||
watch,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
|
||||
export type DisclosureKey = string | number;
|
||||
|
||||
export interface DisclosureSlots {
|
||||
[slot: string]: string;
|
||||
}
|
||||
|
||||
export interface DisclosureItemContext {
|
||||
/** Effective expanded state (group-aware). */
|
||||
isExpanded: ComputedRef<boolean>;
|
||||
isDisabled: ComputedRef<boolean>;
|
||||
toggle: () => void;
|
||||
/** Stable id used for trigger/panel ARIA association. */
|
||||
contentId: ComputedRef<string>;
|
||||
triggerId: ComputedRef<string>;
|
||||
}
|
||||
|
||||
export const disclosureItemContextKey: InjectionKey<DisclosureItemContext> =
|
||||
Symbol("hv-disclosure-item");
|
||||
|
||||
export interface DisclosureGroupContext {
|
||||
expandedKeys: ComputedRef<DisclosureKey[]>;
|
||||
allowsMultipleExpanded: ComputedRef<boolean>;
|
||||
isDisabled: ComputedRef<boolean>;
|
||||
isExpanded: (key: DisclosureKey) => boolean;
|
||||
toggle: (key: DisclosureKey) => void;
|
||||
/** Register a member key for navigation helpers; returns unregister. */
|
||||
registerItem: (key: DisclosureKey) => () => void;
|
||||
itemKeys: ComputedRef<DisclosureKey[]>;
|
||||
}
|
||||
|
||||
export const disclosureGroupContextKey: InjectionKey<DisclosureGroupContext> =
|
||||
Symbol("hv-disclosure-group");
|
||||
|
||||
/**
|
||||
* Key identity uses SameValueZero via Array.includes / index search,
|
||||
* so numeric 1 and string "1" never collide.
|
||||
*/
|
||||
export function keyIndexOf(keys: DisclosureKey[], key: DisclosureKey): number {
|
||||
for (let i = 0; i < keys.length; i += 1) {
|
||||
if (Object.is(keys[i], key) || keys[i] === key) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export function keysEqual(a: DisclosureKey[], b: DisclosureKey[]): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
for (let i = 0; i < a.length; i += 1) {
|
||||
if (keyIndexOf(b, a[i]!) === -1) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export interface UseDisclosureGroupStateOptions {
|
||||
expandedKeys?: DisclosureKey[] | undefined;
|
||||
defaultExpandedKeys?: DisclosureKey[] | undefined;
|
||||
allowsMultipleExpanded?: boolean;
|
||||
isDisabled?: boolean;
|
||||
onExpandedChange?: (keys: DisclosureKey[]) => void;
|
||||
}
|
||||
|
||||
export function useDisclosureGroupState(
|
||||
options: UseDisclosureGroupStateOptions,
|
||||
emitUpdate?: (keys: DisclosureKey[]) => void,
|
||||
) {
|
||||
const internal = ref<DisclosureKey[]>([
|
||||
...(options.defaultExpandedKeys ?? []),
|
||||
]);
|
||||
const itemKeys = ref<DisclosureKey[]>([]);
|
||||
|
||||
const isControlled = computed(() => options.expandedKeys !== undefined);
|
||||
|
||||
const expandedKeys = computed<DisclosureKey[]>(() =>
|
||||
isControlled.value ? [...(options.expandedKeys ?? [])] : internal.value,
|
||||
);
|
||||
|
||||
const allowsMultipleExpanded = computed(
|
||||
() => options.allowsMultipleExpanded === true,
|
||||
);
|
||||
const isDisabled = computed(() => options.isDisabled === true);
|
||||
|
||||
function commit(next: DisclosureKey[]) {
|
||||
// Never mutate the caller array.
|
||||
const copy = [...next];
|
||||
if (!isControlled.value) {
|
||||
internal.value = copy;
|
||||
}
|
||||
options.onExpandedChange?.(copy);
|
||||
emitUpdate?.(copy);
|
||||
}
|
||||
|
||||
function isExpanded(key: DisclosureKey): boolean {
|
||||
return keyIndexOf(expandedKeys.value, key) !== -1;
|
||||
}
|
||||
|
||||
function toggle(key: DisclosureKey) {
|
||||
if (isDisabled.value) return;
|
||||
const current = expandedKeys.value;
|
||||
const open = keyIndexOf(current, key) !== -1;
|
||||
if (allowsMultipleExpanded.value) {
|
||||
const next = open
|
||||
? current.filter((k) => keyIndexOf([key], k) === -1)
|
||||
: [...current, key];
|
||||
commit(next);
|
||||
return;
|
||||
}
|
||||
// Single: replace; clicking open item closes it (allowsEmptySelection-style).
|
||||
commit(open ? [] : [key]);
|
||||
}
|
||||
|
||||
function registerItem(key: DisclosureKey): () => void {
|
||||
if (keyIndexOf(itemKeys.value, key) !== -1) {
|
||||
// Keep both instances mounted, but make the collision visible.
|
||||
console.warn(
|
||||
`[heroui-vue] duplicate disclosure key ${JSON.stringify(key)}; group expand state will be shared.`,
|
||||
);
|
||||
}
|
||||
itemKeys.value = [...itemKeys.value, key];
|
||||
return () => {
|
||||
itemKeys.value = itemKeys.value.filter(
|
||||
(k) => keyIndexOf([key], k) === -1,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
// Keep internal state in sync when controlled prop is removed.
|
||||
watch(
|
||||
() => options.expandedKeys,
|
||||
(value, prev) => {
|
||||
if (value === undefined && prev !== undefined) {
|
||||
internal.value = [...(prev ?? [])];
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
expandedKeys,
|
||||
allowsMultipleExpanded,
|
||||
isDisabled,
|
||||
isExpanded,
|
||||
toggle,
|
||||
registerItem,
|
||||
itemKeys: computed(() => itemKeys.value),
|
||||
commit,
|
||||
};
|
||||
}
|
||||
|
||||
export function injectDisclosureGroup(): DisclosureGroupContext | undefined {
|
||||
return inject(disclosureGroupContextKey, undefined);
|
||||
}
|
||||
|
||||
export function provideDisclosureGroup(ctx: DisclosureGroupContext): void {
|
||||
provide(disclosureGroupContextKey, ctx);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
||||
import { keyIndexOf, type DisclosureKey } from "../disclosure-group/context";
|
||||
|
||||
export interface UseDisclosureGroupNavigationProps {
|
||||
expandedKeys: MaybeRefOrGetter<DisclosureKey[]>;
|
||||
itemIds: MaybeRefOrGetter<DisclosureKey[]>;
|
||||
onExpandedChange: (keys: DisclosureKey[]) => void;
|
||||
allowsMultipleExpanded?: boolean;
|
||||
}
|
||||
|
||||
export interface UseDisclosureGroupNavigationReturn {
|
||||
currentIndex: number;
|
||||
isPrevDisabled: boolean;
|
||||
isNextDisabled: boolean;
|
||||
onPrevious: () => void;
|
||||
onNext: () => void;
|
||||
}
|
||||
|
||||
const EMPTY_ITEM_IDS: DisclosureKey[] = [];
|
||||
|
||||
/**
|
||||
* Vue port of HeroUI useDisclosureGroupNavigation.
|
||||
* `expandedKeys` / `onExpandedChange` use arrays instead of Set;
|
||||
* the source collection is never mutated.
|
||||
*/
|
||||
export function useDisclosureGroupNavigation({
|
||||
allowsMultipleExpanded = false,
|
||||
expandedKeys,
|
||||
itemIds = EMPTY_ITEM_IDS,
|
||||
onExpandedChange,
|
||||
}: UseDisclosureGroupNavigationProps): UseDisclosureGroupNavigationReturn {
|
||||
const currentIndex = computed(() => {
|
||||
const ids = toValue(itemIds) ?? EMPTY_ITEM_IDS;
|
||||
const keys = toValue(expandedKeys) ?? [];
|
||||
const expandedItems = ids.filter((id) => keyIndexOf(keys, id) !== -1);
|
||||
const currentItem = expandedItems.length > 0 ? expandedItems[0] : ids[0];
|
||||
|
||||
if (currentItem === undefined) return -1;
|
||||
|
||||
return keyIndexOf(ids, currentItem);
|
||||
});
|
||||
|
||||
function handlePrevious() {
|
||||
const ids = toValue(itemIds) ?? EMPTY_ITEM_IDS;
|
||||
const keys = toValue(expandedKeys) ?? [];
|
||||
const index = currentIndex.value;
|
||||
if (index <= 0) return;
|
||||
|
||||
const prevItem = ids[index - 1];
|
||||
|
||||
if (prevItem === undefined) return;
|
||||
|
||||
if (allowsMultipleExpanded) {
|
||||
if (keyIndexOf(keys, prevItem) === -1) {
|
||||
onExpandedChange([...keys, prevItem]);
|
||||
}
|
||||
} else {
|
||||
onExpandedChange([prevItem]);
|
||||
}
|
||||
}
|
||||
|
||||
function handleNext() {
|
||||
const ids = toValue(itemIds) ?? EMPTY_ITEM_IDS;
|
||||
const keys = toValue(expandedKeys) ?? [];
|
||||
const index = currentIndex.value;
|
||||
if (index >= ids.length - 1) return;
|
||||
|
||||
const nextItem = ids[index + 1];
|
||||
|
||||
if (nextItem === undefined) return;
|
||||
|
||||
if (allowsMultipleExpanded) {
|
||||
if (keyIndexOf(keys, nextItem) === -1) {
|
||||
onExpandedChange([...keys, nextItem]);
|
||||
}
|
||||
} else {
|
||||
onExpandedChange([nextItem]);
|
||||
}
|
||||
}
|
||||
|
||||
const isPrevDisabled = computed(() => currentIndex.value <= 0);
|
||||
const isNextDisabled = computed(() => {
|
||||
const ids = toValue(itemIds) ?? EMPTY_ITEM_IDS;
|
||||
return currentIndex.value >= ids.length - 1;
|
||||
});
|
||||
|
||||
return {
|
||||
get currentIndex() {
|
||||
return currentIndex.value;
|
||||
},
|
||||
get isPrevDisabled() {
|
||||
return isPrevDisabled.value;
|
||||
},
|
||||
get isNextDisabled() {
|
||||
return isNextDisabled.value;
|
||||
},
|
||||
onPrevious: handlePrevious,
|
||||
onNext: handleNext,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user