You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
12 KiB

1 month ago
  1. {"ast":null,"code":"import { inject, computed, ref } from 'vue';\nimport { useNamespace } from '../../../hooks/use-namespace/index.mjs';\nimport { useId } from '../../../hooks/use-id/index.mjs';\nimport { addClass } from '../../../utils/dom/style.mjs';\nimport { EVENT_CODE } from '../../../constants/aria.mjs';\nconst useDropdown = () => {\n const elDropdown = inject(\"elDropdown\", {});\n const _elDropdownSize = computed(() => elDropdown == null ? void 0 : elDropdown.dropdownSize);\n return {\n elDropdown,\n _elDropdownSize\n };\n};\nconst initDropdownDomEvent = (dropdownChildren, triggerElm, _instance) => {\n const ns = useNamespace(\"dropdown\");\n const menuItems = ref(null);\n const menuItemsArray = ref(null);\n const dropdownElm = ref(null);\n const listId = useId();\n dropdownElm.value = dropdownChildren == null ? void 0 : dropdownChildren.subTree.el;\n function removeTabindex() {\n var _a;\n triggerElm.setAttribute(\"tabindex\", \"-1\");\n (_a = menuItemsArray.value) == null ? void 0 : _a.forEach(item => item.setAttribute(\"tabindex\", \"-1\"));\n }\n function resetTabindex(ele) {\n removeTabindex();\n ele == null ? void 0 : ele.setAttribute(\"tabindex\", \"0\");\n }\n function handleTriggerKeyDown(ev) {\n const code = ev.code;\n if ([EVENT_CODE.up, EVENT_CODE.down].includes(code)) {\n removeTabindex();\n resetTabindex(menuItems.value[0]);\n menuItems.value[0].focus();\n ev.preventDefault();\n ev.stopPropagation();\n } else if ([EVENT_CODE.enter, EVENT_CODE.numpadEnter].includes(code)) {\n _instance.handleClick();\n } else if ([EVENT_CODE.tab, EVENT_CODE.esc].includes(code)) {\n _instance.hide();\n }\n }\n function handleItemKeyDown(ev) {\n const code = ev.code;\n const target = ev.target;\n const currentIndex = menuItemsArray.value.indexOf(target);\n const max = menuItemsArray.value.length - 1;\n let nextIndex;\n if ([EVENT_CODE.up, EVENT_CODE.down].includes(code)) {\n if (code === EVENT_CODE.up) {\n nextIndex = currentIndex !== 0 ? currentIndex - 1 : 0;\n } else {\n nextIndex = currentIndex < max ? currentIndex + 1 : max;\n }\n removeTabindex();\n resetTabindex(menuItems.value[nextIndex]);\n menuItems.value[nextIndex].focus();\n ev.preventDefault();\n ev.stopPropagation();\n } else if (code === EVENT_CODE.enter) {\n triggerElmFocus();\n target.click();\n if (_instance.props.hideOnClick) {\n _instance.hide();\n }\n } else if ([EVENT_CODE.tab, EVENT_CODE.esc].includes(code)) {\n _instance.hide();\n triggerElmFocus();\n }\n }\n function initAria() {\n dropdownElm.value.setAttribute(\"id\", listId.value);\n triggerElm.setAttribute(\"aria-haspopup\", \"list\");\n triggerElm.setAttribute(\"aria-controls\", listId.value);\n if (!_instance.props.splitButton) {\n triggerElm.setAttribute(\"role\", \"button\");\n triggerElm.setAttribute(\"tabindex\", _instance.props.tabindex);\n addClass(triggerElm, ns.b(\"selfdefine\"));\n }\n }\n function initEvent() {\n var _a;\n triggerElm == null ? void 0 : triggerElm.addEventListener(\"keydown\", handleTriggerKeyDown);\n (_a = dropdownElm.value) == null ? void 0 : _a.addEventListener(\"keydown\", handleItemKeyDown, true);\n }\n function initDomOperation() {\n menuItems.value = dropdownElm.value.querySelectorAll(\"[tabindex='-1']\");\n menuItemsArray.value = Array.from(menuItems.value);\n initEvent();\n initAria();\n }\n function triggerElmFocus() {\n triggerElm.focus();\n }\n initDomOperation();\n};\nexport { initDropdownDomEvent, useDropdown };","map":{"version":3,"names":["useDropdown","elDropdown","inject","_elDropdownSize","computed","dropdownSize","initDropdownDomEvent","dropdownChildren","triggerElm","_instance","ns","useNamespace","menuItems","ref","menuItemsArray","dropdownElm","listId","useId","value","subTree","el","removeTabindex","_a","setAttribute","forEach","item","resetTabindex","ele","handleTriggerKeyD