{"version":3,"file":"select.mjs","names":[],"sources":["../../../../../../packages/components/tree-select/src/select.ts"],"sourcesContent":["// @ts-nocheck\nimport { computed, nextTick, onMounted, toRefs, watch } from 'vue'\nimport { useEventListener } from '@vueuse/core'\nimport { pick } from 'lodash-unified'\nimport ElSelect from '@element-plus/components/select'\nimport { useNamespace } from '@element-plus/hooks'\nimport { EVENT_CODE, UPDATE_MODEL_EVENT } from '@element-plus/constants'\nimport { getEventCode } from '@element-plus/utils'\n\nimport type { Ref } from 'vue'\nimport type { SelectInstance } from '@element-plus/components/select'\nimport type { TreeInstance } from '@element-plus/components/tree'\n\nexport const useSelect = (\n  props,\n  { attrs, emit },\n  {\n    select,\n    tree,\n    key,\n  }: {\n    select: Ref<SelectInstance | undefined>\n    tree: Ref<TreeInstance | undefined>\n    key: Ref<string>\n  }\n) => {\n  const ns = useNamespace('tree-select')\n\n  // update tree data when use filterMethod/remoteMethod\n  watch(\n    () => props.data,\n    () => {\n      if (props.filterable) {\n        nextTick(() => {\n          // let tree node expand only, same with tree filter\n          tree.value?.filter(select.value?.states.inputValue)\n        })\n      }\n    },\n    { flush: 'post' }\n  )\n\n  const focusLastNode = (listNode) => {\n    const lastNode = listNode.at(-1)\n    if (lastNode.expanded && lastNode.childNodes.at(-1)) {\n      focusLastNode([lastNode.childNodes.at(-1)])\n    } else {\n      const el = tree.value.el$?.querySelector(\n        `[data-key=\"${listNode.at(-1).key}\"]`\n      )\n      el?.focus({ preventScroll: true })\n      return\n    }\n  }\n\n  onMounted(() => {\n    useEventListener(\n      () => select.value?.$el,\n      'keydown',\n      async (evt) => {\n        const code = getEventCode(evt)\n        const { dropdownMenuVisible } = select.value!\n        if (\n          [EVENT_CODE.down, EVENT_CODE.up].includes(code) &&\n          dropdownMenuVisible\n        ) {\n          await nextTick()\n          // wait navigateOption to finish\n          setTimeout(() => {\n            if (EVENT_CODE.up === code) {\n              const listNode = tree.value.store.root.childNodes\n              focusLastNode(listNode)\n              return\n            }\n            // el-select-dropdown__item => el-tree-node__content => el-tree-node__content\n            select.value.optionsArray[\n              select.value.states.hoveringIndex\n            ].$el?.parentNode?.parentNode?.focus({ preventScroll: true })\n          })\n        }\n      },\n      {\n        capture: true,\n      }\n    )\n  })\n\n  const result = {\n    ...pick(toRefs(props), Object.keys(ElSelect.props)),\n    ...attrs,\n    class: computed(() => attrs.class),\n    style: computed(() => attrs.style),\n    // attrs is not reactive, when v-model binding source changes,\n    // this listener is still old, see the bug(or test 'v-model source change'):\n    // https://github.com/element-plus/element-plus/issues/14204\n    'onUpdate:modelValue': (value) => emit(UPDATE_MODEL_EVENT, value),\n    valueKey: key,\n    popperClass: computed(() => {\n      const classes = [ns.e('popper')]\n      if (props.popperClass) classes.push(props.popperClass)\n      return classes.join(' ')\n    }),\n    filterMethod: (keyword = '') => {\n      if (props.filterMethod) {\n        props.filterMethod(keyword)\n      } else if (props.remoteMethod) {\n        props.remoteMethod(keyword)\n      } else {\n        // let tree node expand only, same with tree filter\n        tree.value?.filter(keyword)\n      }\n    },\n  }\n\n  return result\n}\n"],"mappings":";;;;;;;;;;AAaA,MAAa,aACX,OACA,EAAE,OAAO,QACT,EACE,QACA,MACA,UAMC;CACH,MAAM,KAAK,aAAa,cAAc;AAGtC,aACQ,MAAM,YACN;AACJ,MAAI,MAAM,WACR,gBAAe;AAEb,QAAK,OAAO,OAAO,OAAO,OAAO,OAAO,WAAW;IACnD;IAGN,EAAE,OAAO,QAAQ,CAClB;CAED,MAAM,iBAAiB,aAAa;EAClC,MAAM,WAAW,SAAS,GAAG,GAAG;AAChC,MAAI,SAAS,YAAY,SAAS,WAAW,GAAG,GAAG,CACjD,eAAc,CAAC,SAAS,WAAW,GAAG,GAAG,CAAC,CAAC;OACtC;AAIL,IAHW,KAAK,MAAM,KAAK,cACzB,cAAc,SAAS,GAAG,GAAG,CAAC,IAAI,IACnC,GACG,MAAM,EAAE,eAAe,MAAM,CAAC;AAClC;;;AAIJ,iBAAgB;AACd,yBACQ,OAAO,OAAO,KACpB,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