feat: migrate Menu and Dropdown with selection and submenus
Port HeroUI menu family and Dropdown from ce90ea3: Reka DropdownMenu base, MenuKey[] selection with section owners, 17 official demos, dropdown docs and B01 tests. Long-press stays demo-level; submenu close is level-scoped.
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
provide,
|
||||
ref,
|
||||
useAttrs,
|
||||
watch,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { DropdownMenuGroup as RekaMenuGroup } from "reka-ui";
|
||||
import { menuSectionVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
menuContextKey,
|
||||
menuSectionContextKey,
|
||||
type MenuKey,
|
||||
type MenuSelectionMode,
|
||||
} from "../menu/context";
|
||||
import { useMenuSelection } from "../menu/useMenuSelection";
|
||||
|
||||
export default defineComponent({
|
||||
name: "MenuSection",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "div" },
|
||||
/** Section-level selection mode. Omit to inherit the menu's selection. */
|
||||
selectionMode: {
|
||||
type: String as PropType<MenuSelectionMode>,
|
||||
default: undefined,
|
||||
},
|
||||
selectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultSelectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
disabledKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
},
|
||||
emits: {
|
||||
"update:selectedKeys": (_keys: MenuKey[]) => true,
|
||||
selectionChange: (_keys: MenuKey[]) => true,
|
||||
},
|
||||
setup(props, { slots, emit }) {
|
||||
const attrs = useAttrs();
|
||||
const menuCtx = inject(menuContextKey, null);
|
||||
const ownsSelection = computed(
|
||||
() =>
|
||||
props.selectionMode !== undefined || props.selectedKeys !== undefined,
|
||||
);
|
||||
|
||||
const internal = ref<MenuKey[]>([...(props.defaultSelectedKeys ?? [])]);
|
||||
const isControlled = computed(() => props.selectedKeys !== undefined);
|
||||
const acceptedKeys = computed(() =>
|
||||
isControlled.value ? (props.selectedKeys ?? []) : internal.value,
|
||||
);
|
||||
const effectiveMode = computed<MenuSelectionMode>(() => {
|
||||
if (props.selectionMode) return props.selectionMode;
|
||||
if (ownsSelection.value) return "multiple";
|
||||
return menuCtx?.selectionMode.value ?? "none";
|
||||
});
|
||||
|
||||
const sectionSelection = useMenuSelection({
|
||||
selectionMode: effectiveMode,
|
||||
selectedKeys: acceptedKeys,
|
||||
disabledKeys: computed(() => props.disabledKeys ?? []),
|
||||
onSelectionChange(next) {
|
||||
if (!isControlled.value) internal.value = next;
|
||||
emit("update:selectedKeys", next);
|
||||
emit("selectionChange", next);
|
||||
},
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.selectedKeys,
|
||||
(next) => {
|
||||
if (next) internal.value = [...next];
|
||||
},
|
||||
);
|
||||
|
||||
provide(menuSectionContextKey, {
|
||||
selection: {
|
||||
get selectionMode() {
|
||||
return ownsSelection.value
|
||||
? sectionSelection.selectionMode
|
||||
: (menuCtx?.selection.selectionMode ??
|
||||
sectionSelection.selectionMode);
|
||||
},
|
||||
get selectedKeys() {
|
||||
return ownsSelection.value
|
||||
? sectionSelection.selectedKeys
|
||||
: (menuCtx?.selection.selectedKeys ??
|
||||
sectionSelection.selectedKeys);
|
||||
},
|
||||
proposeSelection: (key: MenuKey) =>
|
||||
(ownsSelection.value
|
||||
? sectionSelection
|
||||
: (menuCtx?.selection ?? sectionSelection)
|
||||
).proposeSelection(key),
|
||||
isItemSelected: (key: MenuKey) =>
|
||||
(ownsSelection.value
|
||||
? sectionSelection
|
||||
: (menuCtx?.selection ?? sectionSelection)
|
||||
).isItemSelected(key),
|
||||
isItemDisabled: (key: MenuKey) =>
|
||||
(ownsSelection.value
|
||||
? sectionSelection
|
||||
: (menuCtx?.selection ?? sectionSelection)
|
||||
).isItemDisabled(key),
|
||||
},
|
||||
});
|
||||
|
||||
return () =>
|
||||
h(
|
||||
RekaMenuGroup as never,
|
||||
{
|
||||
...attrs,
|
||||
class: [menuSectionVariants(), attrs.class],
|
||||
"data-slot": "menu-section",
|
||||
role: "group",
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user