{"version":3,"file":"useKeydown.js","sources":["../../../../../../../packages/components/tree/src/model/useKeydown.ts"],"sourcesContent":["// @ts-nocheck\nimport { onMounted, onUpdated, shallowRef, watch } from 'vue'\nimport { useEventListener } from '@vueuse/core'\nimport { EVENT_CODE } from '@element-plus/constants'\nimport { useNamespace } from '@element-plus/hooks'\nimport type TreeStore from './tree-store'\n\nimport type { Ref } from 'vue'\nimport type { Nullable } from '@element-plus/utils'\n\ninterface UseKeydownOption {\n el$: Ref\n}\nexport function useKeydown({ el$ }: UseKeydownOption, store: Ref) {\n const ns = useNamespace('tree')\n\n const treeItems = shallowRef[]>([])\n const checkboxItems = shallowRef[]>([])\n\n onMounted(() => {\n initTabIndex()\n })\n\n onUpdated(() => {\n treeItems.value = Array.from(el$.value.querySelectorAll('[role=treeitem]'))\n checkboxItems.value = Array.from(\n el$.value.querySelectorAll('input[type=checkbox]')\n )\n })\n\n watch(checkboxItems, (val) => {\n val.forEach((checkbox) => {\n checkbox.setAttribute('tabindex', '-1')\n })\n })\n\n const handleKeydown = (ev: KeyboardEvent): void => {\n const currentItem = ev.target as HTMLElement\n if (!currentItem.className.includes(ns.b('node'))) return\n const code = ev.code\n treeItems.value = Array.from(\n el$.value.querySelectorAll(`.${ns.is('focusable')}[role=treeitem]`)\n )\n const currentIndex = treeItems.value.indexOf(currentItem)\n let nextIndex\n if ([EVENT_CODE.up, EVENT_CODE.down].includes(code)) {\n ev.preventDefault()\n if (code === EVENT_CODE.up) {\n nextIndex =\n currentIndex === -1\n ? 0\n : currentIndex !== 0\n ? currentIndex - 1\n : treeItems.value.length - 1\n const startIndex = nextIndex\n while (true) {\n if (\n store.value.getNode(treeItems.value[nextIndex].dataset.key).canFocus\n )\n break\n nextIndex--\n if (nextIndex === startIndex) {\n nextIndex = -1\n break\n }\n if (nextIndex < 0) {\n nextIndex = treeItems.value.length - 1\n }\n }\n } else {\n nextIndex =\n currentIndex === -1\n ? 0\n : currentIndex < treeItems.value.length - 1\n ? currentIndex + 1\n : 0\n const startIndex = nextIndex\n while (true) {\n if (\n store.value.getNode(treeItems.value[nextIndex].dataset.key).canFocus\n )\n break\n nextIndex++\n if (nextIndex === startIndex) {\n nextIndex = -1\n break\n }\n if (nextIndex >= treeItems.value.length) {\n nextIndex = 0\n }\n }\n }\n nextIndex !== -1 && treeItems.value[nextIndex].focus()\n }\n if ([EVENT_CODE.left, EVENT_CODE.right].includes(code)) {\n ev.preventDefault()\n currentItem.click()\n }\n const hasInput = currentItem.querySelector(\n '[type=\"checkbox\"]'\n ) as Nullable\n if ([EVENT_CODE.enter, EVENT_CODE.space].includes(code) && hasInput) {\n ev.preventDefault()\n hasInput.click()\n }\n }\n\n useEventListener(el$, 'keydown', handleKeydown)\n\n const initTabIndex = (): void => {\n treeItems.value = Array.from(\n el$.value.querySelectorAll(`.${ns.is('focusable')}[role=treeitem]`)\n )\n checkboxItems.value = Array.from(\n el$.value.querySelectorAll('input[type=checkbox]')\n )\n const checkedItem = el$.value.querySelectorAll(\n `.${ns.is('checked')}[role=treeitem]`\n )\n if (checkedItem.length) {\n checkedItem[0].setAttribute('tabindex', '0')\n return\n }\n treeItems.value[0]?.setAttribute('tabindex', '0')\n }\n}\n"],"names":["useNamespace","shallowRef","onMounted","onUpdated","watch","EVENT_CODE","useEventListener"],"mappings":";;;;;;;;;;;AAIO,SAAS,UAAU,CAAC,EAAE,GAAG,EAAE,EAAE,KAAK,EAAE;AAC3C,EAAE,MAAM,EAAE,GAAGA,kBAAY,CAAC,MAAM,CAAC,CAAC;AAClC,EAAE,MAAM,SAAS,GAAGC,cAAU,CAAC,EAAE,CAAC,CAAC;AACnC,EAAE,MAAM,aAAa,GAAGA,cAAU,CAAC,EAAE,CAAC,CAAC;AACvC,EAAEC,aAAS,CAAC,MAAM;AAClB,IAAI,YAAY,EAAE,CAAC;AACnB,GAAG,CAAC,CAAC;AACL,EAAEC,aAAS,CAAC,MAAM;AAClB,IAAI,SAAS,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CA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