{"version":3,"file":"autocomplete2.mjs","names":[],"sources":["../../../../../../packages/components/autocomplete/src/autocomplete.vue"],"sourcesContent":["<template>\n  <el-tooltip\n    ref=\"popperRef\"\n    :visible=\"suggestionVisible\"\n    :placement=\"placement\"\n    :fallback-placements=\"['bottom-start', 'top-start']\"\n    :popper-class=\"[ns.e('popper'), popperClass!]\"\n    :popper-style=\"popperStyle\"\n    :teleported=\"teleported\"\n    :append-to=\"appendTo\"\n    :gpu-acceleration=\"false\"\n    pure\n    manual-mode\n    effect=\"light\"\n    trigger=\"click\"\n    :transition=\"`${ns.namespace.value}-zoom-in-top`\"\n    persistent\n    role=\"listbox\"\n    @before-show=\"onSuggestionShow\"\n    @hide=\"onHide\"\n  >\n    <div\n      ref=\"listboxRef\"\n      :class=\"[ns.b(), $attrs.class]\"\n      :style=\"styles\"\n      role=\"combobox\"\n      aria-haspopup=\"listbox\"\n      :aria-expanded=\"suggestionVisible\"\n      :aria-owns=\"listboxId\"\n    >\n      <el-input\n        ref=\"inputRef\"\n        v-bind=\"mergeProps(passInputProps, $attrs)\"\n        :model-value=\"modelValue\"\n        :disabled=\"disabled\"\n        @input=\"handleInput\"\n        @change=\"handleChange\"\n        @focus=\"handleFocus\"\n        @blur=\"handleBlur\"\n        @clear=\"handleClear\"\n        @keydown=\"handleKeydown\"\n        @mousedown=\"handleMouseDown\"\n      >\n        <template v-if=\"$slots.prepend\" #prepend>\n          <slot name=\"prepend\" />\n        </template>\n        <template v-if=\"$slots.append\" #append>\n          <slot name=\"append\" />\n        </template>\n        <template v-if=\"$slots.prefix\" #prefix>\n          <slot name=\"prefix\" />\n        </template>\n        <template v-if=\"$slots.suffix\" #suffix>\n          <slot name=\"suffix\" />\n        </template>\n      </el-input>\n    </div>\n    <template #content>\n      <div\n        ref=\"regionRef\"\n        :class=\"[ns.b('suggestion'), ns.is('loading', suggestionLoading)]\"\n        :style=\"{\n          [fitInputWidth ? 'width' : 'minWidth']: dropdownWidth,\n          outline: 'none',\n        }\"\n        role=\"region\"\n      >\n        <div\n          v-if=\"$slots.header\"\n          :class=\"ns.be('suggestion', 'header')\"\n          @click.stop\n        >\n          <slot name=\"header\" />\n        </div>\n        <el-scrollbar\n          :id=\"listboxId\"\n          tag=\"ul\"\n          :wrap-class=\"ns.be('suggestion', 'wrap')\"\n          :view-class=\"ns.be('suggestion', 'list')\"\n          role=\"listbox\"\n        >\n          <li v-if=\"suggestionLoading\">\n            <slot name=\"loading\">\n              <el-icon :class=\"ns.is('loading')\">\n                <Loading />\n              </el-icon>\n            </slot>\n          </li>\n          <template v-else>\n            <li\n              v-for=\"(item, index) in suggestions\"\n              :id=\"`${listboxId}-item-${index}`\"\n              :key=\"index\"\n              :class=\"{ highlighted: highlightedIndex === index }\"\n              role=\"option\"\n              :aria-selected=\"highlightedIndex === index\"\n              @click=\"handleSelect(item)\"\n            >\n              <slot :item=\"item\">{{ item[valueKey] }}</slot>\n            </li>\n          </template>\n        </el-scrollbar>\n        <div\n          v-if=\"$slots.footer\"\n          :class=\"ns.be('suggestion', 'footer')\"\n          @click.stop\n        >\n          <slot name=\"footer\" />\n        </div>\n      </div>\n    </template>\n  </el-tooltip>\n</template>\n\n<script lang=\"ts\" setup>\nimport {\n  computed,\n  mergeProps,\n  onBeforeUnmount,\n  onMounted,\n  ref,\n  useAttrs as useRawAttrs,\n} from 'vue'\nimport { pick } from 'lodash-unified'\nimport { onClickOutside, useDebounceFn } from '@vueuse/core'\nimport { Loading } from '@element-plus/icons-vue'\nimport { useId, useNamespace } from '@element-plus/hooks'\nimport { NOOP, getEventCode, isArray, throwError } from '@element-plus/utils'\nimport {\n  CHANGE_EVENT,\n  EVENT_CODE,\n  INPUT_EVENT,\n  UPDATE_MODEL_EVENT,\n} from '@element-plus/constants'\nimport ElInput, {\n  inputProps,\n  inputPropsDefaults,\n} from '@element-plus/components/input'\nimport ElScrollbar from '@element-plus/components/scrollbar'\nimport ElTooltip from '@element-plus/components/tooltip'\nimport ElIcon from '@element-plus/components/icon'\nimport { useFormDisabled } from '@element-plus/components/form'\nimport { autocompleteEmits } from './autocomplete'\n\nimport type { AutocompleteData, AutocompleteProps } from './autocomplete'\nimport type { StyleValue } from 'vue'\nimport type { TooltipInstance } from '@element-plus/components/tooltip'\nimport type { InputInstance } from '@element-plus/components/input'\n\nconst COMPONENT_NAME = 'ElAutocomplete'\ndefineOptions({\n  name: COMPONENT_NAME,\n  inheritAttrs: false,\n})\n\nconst props = withDefaults(defineProps<AutocompleteProps>(), {\n  ...inputPropsDefaults,\n  valueKey: 'value',\n  modelValue: '',\n  debounce: 300,\n  placement: 'bottom-start',\n  fetchSuggestions: NOOP,\n  triggerOnFocus: true,\n  loopNavigation: true,\n  teleported: true,\n})\nconst emit = defineEmits(autocompleteEmits)\n\nconst passInputProps = computed(() => pick(props, Object.keys(inputProps)))\n\nconst rawAttrs = useRawAttrs()\nconst disabled = useFormDisabled()\nconst ns = useNamespace('autocomplete')\n\nconst inputRef = ref<InputInstance>()\nconst regionRef = ref<HTMLElement>()\nconst popperRef = ref<TooltipInstance>()\nconst listboxRef = ref<HTMLElement>()\n\nlet readonly = false\nlet ignoreFocusEvent = false\nconst suggestions = ref<AutocompleteData>([])\nconst highlightedIndex = ref(-1)\nconst dropdownWidth = ref('')\nconst activated = ref(false)\nconst suggestionDisabled = ref(false)\nconst loading = ref(false)\n\nconst listboxId = useId()\nconst styles = computed(() => rawAttrs.style as StyleValue)\n\nconst suggestionVisible = computed(() => {\n  const isValidData = suggestions.value.length > 0\n  return (isValidData || loading.value) && activated.value\n})\n\nconst suggestionLoading = computed(() => !props.hideLoading && loading.value)\n\nconst refInput = computed<HTMLInputElement[]>(() => {\n  if (inputRef.value) {\n    return Array.from<HTMLInputElement>(\n      inputRef.value.$el.querySelectorAll('input')\n    )\n  }\n  return []\n})\n\nconst onSuggestionShow = () => {\n  if (suggestionVisible.value) {\n    dropdownWidth.value = `${inputRef.value!.$el.offsetWidth}px`\n  }\n}\n\nconst onHide = () => {\n  highlightedIndex.value = -1\n}\n\nconst getData = async (queryString: string) => {\n  if (suggestionDisabled.value) return\n\n  const cb = (suggestionList: AutocompleteData) => {\n    loading.value = false\n    if (suggestionDisabled.value) return\n\n    if (isArray(suggestionList)) {\n      suggestions.value = suggestionList\n      highlightedIndex.value = props.highlightFirstItem ? 0 : -1\n    } else {\n      throwError(COMPONENT_NAME, 'autocomplete suggestions must be an array')\n    }\n  }\n\n  loading.value = true\n  if (isArray(props.fetchSuggestions)) {\n    cb(props.fetchSuggestions)\n  } else {\n    const result = await props.fetchSuggestions(queryString, cb)\n    if (isArray(result)) cb(result)\n  }\n}\n\nconst debounce = computed(() => props.debounce)\nconst debouncedGetData = useDebounceFn(getData, debounce)\n\nconst handleInput = (value: string) => {\n  const valuePresented = !!value\n\n  emit(INPUT_EVENT, value)\n  emit(UPDATE_MODEL_EVENT, value)\n\n  suggestionDisabled.value = false\n  activated.value ||= valuePresented\n\n  if (!props.triggerOnFocus && !value) {\n    suggestionDisabled.value = true\n    suggestions.value = []\n    return\n  }\n\n  debouncedGetData(value)\n}\n\nconst handleMouseDown = (event: MouseEvent) => {\n  if (disabled.value) return\n  if (\n    (event.target as HTMLElement)?.tagName !== 'INPUT' ||\n    refInput.value.includes(document.activeElement as HTMLInputElement)\n  ) {\n    activated.value = true\n  }\n}\n\nconst handleChange = (value: string | number) => {\n  emit(CHANGE_EVENT, value)\n}\n\nconst handleFocus = (evt: FocusEvent) => {\n  if (!ignoreFocusEvent) {\n    activated.value = true\n    emit('focus', evt)\n    const queryString = props.modelValue ?? ''\n    if (props.triggerOnFocus && !readonly) {\n      debouncedGetData(String(queryString))\n    }\n  } else {\n    ignoreFocusEvent = false\n  }\n}\n\nconst handleBlur = (evt: FocusEvent) => {\n  setTimeout(() => {\n    // validate current focus event is inside el-tooltip-content\n    // if so, ignore the blur event and the next focus event\n    if (popperRef.value?.isFocusInsideContent()) {\n      ignoreFocusEvent = true\n      return\n    }\n    activated.value && close()\n    emit('blur', evt)\n  })\n}\n\nconst handleClear = () => {\n  activated.value = false\n  emit(UPDATE_MODEL_EVENT, '')\n  emit('clear')\n}\n\nconst handleKeyEnter = async () => {\n  if (inputRef.value?.isComposing) {\n    return\n  }\n\n  if (\n    suggestionVisible.value &&\n    highlightedIndex.value >= 0 &&\n    highlightedIndex.value < suggestions.value.length\n  ) {\n    handleSelect(suggestions.value[highlightedIndex.value])\n  } else {\n    if (props.selectWhenUnmatched) {\n      emit('select', { value: props.modelValue })\n      suggestions.value = []\n      highlightedIndex.value = -1\n    }\n    activated.value = true\n    debouncedGetData(String(props.modelValue))\n  }\n}\n\nconst handleKeyEscape = (evt: Event) => {\n  if (suggestionVisible.value) {\n    evt.preventDefault()\n    evt.stopPropagation()\n    close()\n  }\n}\n\nconst close = () => {\n  activated.value = false\n}\n\nconst focus = () => {\n  inputRef.value?.focus()\n}\n\nconst blur = () => {\n  inputRef.value?.blur()\n}\n\nconst handleSelect = async (item: any) => {\n  emit(INPUT_EVENT, item[props.valueKey])\n  emit(UPDATE_MODEL_EVENT, item[props.valueKey])\n  emit('select', item)\n  suggestions.value = []\n  highlightedIndex.value = -1\n}\n\nconst highlight = (index: number) => {\n  if (!suggestionVisible.value || loading.value) return\n\n  if (index < 0) {\n    if (!props.loopNavigation) {\n      highlightedIndex.value = -1\n      return\n    }\n    index = suggestions.value.length - 1\n  }\n\n  if (index >= suggestions.value.length) {\n    index = props.loopNavigation ? 0 : suggestions.value.length - 1\n  }\n  const [suggestion, suggestionList] = getSuggestionContext()\n  const highlightItem = suggestionList[index]\n  const scrollTop = suggestion.scrollTop\n  const { offsetTop, scrollHeight } = highlightItem\n\n  if (offsetTop + scrollHeight > scrollTop + suggestion.clientHeight) {\n    suggestion.scrollTop = offsetTop + scrollHeight - suggestion.clientHeight\n  }\n  if (offsetTop < scrollTop) {\n    suggestion.scrollTop = offsetTop\n  }\n  highlightedIndex.value = index\n  inputRef.value?.ref?.setAttribute(\n    'aria-activedescendant',\n    `${listboxId.value}-item-${highlightedIndex.value}`\n  )\n}\nconst getSuggestionContext = () => {\n  const suggestion = regionRef.value!.querySelector(\n    `.${ns.be('suggestion', 'wrap')}`\n  )!\n  const suggestionList = suggestion.querySelectorAll<HTMLElement>(\n    `.${ns.be('suggestion', 'list')} li`\n  )\n  return [suggestion, suggestionList] as const\n}\n\nconst stopHandle = onClickOutside(listboxRef, (event: FocusEvent) => {\n  // Prevent closing if focus is inside popper content\n  if (popperRef.value?.isFocusInsideContent()) return\n  const hadIgnoredFocus = ignoreFocusEvent\n  ignoreFocusEvent = false\n  if (!suggestionVisible.value) return\n  if (hadIgnoredFocus) {\n    handleBlur(new FocusEvent('blur', event))\n  } else {\n    close()\n  }\n})\n\nconst handleKeydown = (e: KeyboardEvent | Event) => {\n  const code = getEventCode(e as KeyboardEvent)\n  switch (code) {\n    case EVENT_CODE.up:\n      e.preventDefault()\n      highlight(highlightedIndex.value - 1)\n      break\n    case EVENT_CODE.down:\n      e.preventDefault()\n      highlight(highlightedIndex.value + 1)\n      break\n    case EVENT_CODE.enter:\n    case EVENT_CODE.numpadEnter:\n      e.preventDefault()\n      handleKeyEnter()\n      break\n    case EVENT_CODE.tab:\n      close()\n      break\n    case EVENT_CODE.esc:\n      handleKeyEscape(e)\n      break\n    case EVENT_CODE.home:\n      e.preventDefault()\n      highlight(0)\n      break\n    case EVENT_CODE.end:\n      e.preventDefault()\n      highlight(suggestions.value.length - 1)\n      break\n    case EVENT_CODE.pageUp:\n      e.preventDefault()\n      highlight(Math.max(0, highlightedIndex.value - 10))\n      break\n    case EVENT_CODE.pageDown:\n      e.preventDefault()\n      highlight(\n        Math.min(suggestions.value.length - 1, highlightedIndex.value + 10)\n      )\n      break\n  }\n}\n\nonBeforeUnmount(() => {\n  stopHandle?.()\n})\n\nonMounted(() => {\n  const inputElement = inputRef.value?.ref\n  if (!inputElement) return\n  ;[\n    { key: 'role', value: 'textbox' },\n    { key: 'aria-autocomplete', value: 'list' },\n    { key: 'aria-controls', value: listboxId.value },\n    {\n      key: 'aria-activedescendant',\n      value: `${listboxId.value}-item-${highlightedIndex.value}`,\n    },\n  ].forEach(({ key, value }) => inputElement.setAttribute(key, value))\n  // get readonly attr\n  readonly = inputElement.hasAttribute('readonly')\n})\n\ndefineExpose({\n  /** @description the index of the currently highlighted item */\n  highlightedIndex,\n  /** @description autocomplete whether activated */\n  activated,\n  /** @description remote search loading status */\n  loading,\n  /** @description el-input component instance */\n  inputRef,\n  /** @description el-tooltip component instance */\n  popperRef,\n  /** @description fetch suggestions result */\n  suggestions,\n  /** @description triggers when a suggestion is clicked */\n  handleSelect,\n  /** @description handle keyboard enter event */\n  handleKeyEnter,\n  /** @description focus the input element */\n  focus,\n  /** @description blur the input element */\n  blur,\n  /** @description close suggestion */\n  close,\n  /** @description highlight an item in a suggestion */\n  highlight,\n  /** @description loading suggestion list */\n  getData,\n})\n</script>\n"],"mappings":""}