{"version":3,"file":"autocomplete.mjs","sources":["../../../../../../packages/components/autocomplete/src/autocomplete.vue"],"sourcesContent":["<template>\n  <hl-tooltip\n    ref=\"popperRef\"\n    :visible=\"suggestionVisible\"\n    :placement=\"placement\"\n    :fallback-placements=\"['bottom-start', 'top-start']\"\n    :popper-class=\"`${namespace}-popper ${popperClass}`\"\n    :teleported=\"compatTeleported\"\n    :gpu-acceleration=\"false\"\n    :offset=\"popperOffset\"\n    :show-arrow=\"false\"\n    trigger=\"click\"\n    :transition=\"transition\"\n    persistent\n    role=\"listbox\"\n    manual-mode\n    @before-show=\"onSuggestionShow\"\n    @hide=\"onHide\"\n  >\n    <div\n      ref=\"listboxRef\"\n      :class=\"[\n        namespace,\n        $attrs.class,\n        {'block': block}\n      ]\"\n      :style=\"styles\"\n      role=\"combobox\"\n      aria-haspopup=\"listbox\"\n      :aria-expanded=\"suggestionVisible\"\n      :aria-owns=\"listboxId\"\n      @click.stop\n    >\n      <hl-input\n        ref=\"inputRef\"\n        v-bind=\"attrs\"\n        :model-value=\"modelValue\"\n        :size=\"inputSize\"\n        :disabled=\"inputDisabled\"\n        :fill=\"inputFill\"\n        :thin=\"thin\"\n        :clearable=\"clearable\"\n        :name=\"name\"\n        :aria-label=\"ariaLabel\"\n        @input=\"handleInput\"\n        @change=\"handleChange\"\n        @focus=\"handleFocus\"\n        @blur=\"handleBlur\"\n        @clear=\"handleClear\"\n        @keydown.up.prevent=\"highlight(highlightedIndex - 1)\"\n        @keydown.down.prevent=\"highlight(highlightedIndex + 1)\"\n        @keydown.enter=\"handleKeyEnter\"\n        @keydown.tab=\"close\"\n        @keydown.esc=\"handleKeyEscape\"\n        @mousedown=\"handleMouseDown\"\n      >\n        <template v-if=\"$slots.prefix\" #prefix>\n          <slot name=\"prefix\"></slot>\n        </template>\n        <template v-if=\"$slots.suffix\" #suffix>\n          <slot name=\"suffix\"></slot>\n        </template>\n      </hl-input>\n    </div>\n    <template #content>\n      <div\n        ref=\"regionRef\"\n        :class=\"['autocomplete-dropdown', suggestionLoading && 'is-loading']\"\n        :style=\"{\n          [fitInputWidth ? 'width' : 'minWidth']: dropdownWidth,\n          outline: 'none'\n        }\"\n        role=\"region\"\n      >\n        <hl-scrollbar\n          :id=\"listboxId\"\n          tag=\"ul\"\n          wrap-class=\"autocomplete-dropdown-wrap\"\n          view-class=\"autocomplete-dropdown-list\"\n          role=\"listbox\"\n        >\n          <li v-if=\"suggestionLoading\">\n            <slot name=\"loading\">\n              <hl-spinner />\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        </hl-scrollbar>\n      </div>\n    </template>\n  </hl-tooltip>\n</template>\n\n<script lang=\"ts\" setup>\nimport {\n  computed,\n  onMounted,\n  onBeforeUnmount,\n  ref,\n  useAttrs as useRawAttrs,\n} from 'vue'\nimport { debounce } from 'lodash-unified'\nimport { onClickOutside } from '@vueuse/core'\nimport { useAttrs, useConsistentProp, useId, useNamespace } from '@hongluan-ui/hooks'\nimport { isArray, throwError } from '@hongluan-ui/utils'\nimport {\n  CHANGE_EVENT,\n  INPUT_EVENT,\n  UPDATE_MODEL_EVENT,\n} from '@hongluan-ui/constants'\nimport { useDeprecateAppendToBody } from '@hongluan-ui/components/popper'\nimport HlInput from '@hongluan-ui/components/input'\nimport HlScrollbar from '@hongluan-ui/components/scrollbar'\nimport HlTooltip from '@hongluan-ui/components/tooltip'\nimport HlSpinner from '@hongluan-ui/components/spinner'\nimport { autocompleteEmits, autocompleteProps } from './autocomplete'\n\nimport type { AutocompleteData } from './autocomplete'\nimport type { StyleValue } from 'vue'\nimport type { TooltipInstance } from '@hongluan-ui/components/tooltip'\nimport type { InputInstance } from '@hongluan-ui/components/input'\n\nconst COMPONENT_NAME = 'Autocomplete'\ndefineOptions({\n  name: COMPONENT_NAME,\n  inheritAttrs: false,\n})\n\nconst props = defineProps(autocompleteProps)\nconst emit = defineEmits(autocompleteEmits)\n\nconst attrs = useAttrs()\nconst rawAttrs = useRawAttrs()\nconst { size: inputSize, disabled: inputDisabled, fill: inputFill } = useConsistentProp()\n\nconst { namespace } = useNamespace('autocomplete')\nconst { compatTeleported } = useDeprecateAppendToBody(\n  COMPONENT_NAME,\n  'popperAppendToBody',\n)\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()\n\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 = async () => {\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}\nconst debouncedGetData = debounce(getData, props.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 (inputDisabled.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) => {\n  emit(CHANGE_EVENT, value)\n}\n\nconst handleFocus = (evt: FocusEvent) => {\n  if (!ignoreFocusEvent) {\n    activated.value = true\n    emit('focus', evt)\n\n    if (props.triggerOnFocus && !readonly) {\n      debouncedGetData(String(props.modelValue))\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 (\n    suggestionVisible.value &&\n    highlightedIndex.value >= 0 &&\n    highlightedIndex.value < suggestions.value.length\n  ) {\n    handleSelect(suggestions.value[highlightedIndex.value])\n  } else if (props.selectWhenUnmatched) {\n    emit('select', { value: props.modelValue })\n    suggestions.value = []\n    highlightedIndex.value = -1\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    highlightedIndex.value = -1\n    return\n  }\n\n  if (index >= suggestions.value.length) {\n    index = suggestions.value.length - 1\n  }\n  const suggestion = regionRef.value.querySelector('.autocomplete-dropdown-wrap')\n  const suggestionList = suggestion.querySelectorAll<HTMLElement>('.autocomplete-dropdown-list li')\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 += scrollHeight\n  }\n  if (offsetTop < scrollTop) {\n    suggestion.scrollTop -= scrollHeight\n  }\n  highlightedIndex.value = index\n  // TODO: use Volar generate dts to fix it.\n  ;(inputRef.value as any).ref!.setAttribute(\n    'aria-activedescendant',\n    `${listboxId.value}-item-${highlightedIndex.value}`,\n  )\n}\n\nconst stopHandle = onClickOutside(listboxRef, () => {\n  suggestionVisible.value && close()\n})\n\nonBeforeUnmount(() => {\n  stopHandle?.()\n})\n\nonMounted(() => {\n  inputRef.value.inputOrTextarea.setAttribute('role', 'textbox')\n  inputRef.value.inputOrTextarea.setAttribute('aria-autocomplete', 'list')\n  inputRef.value.inputOrTextarea.setAttribute('aria-controls', 'id')\n  inputRef.value.inputOrTextarea.setAttribute('aria-activedescendant', `${listboxId.value}-item-${highlightedIndex.value}`)\n  // get readonly attr\n  readonly = inputRef.value.inputOrTextarea.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})\n</script>\n\n"],"names":["useRawAttrs"],"mappings":";;;;;;;;;;;;;;;;;;;;;;oCAuIc;AAAA,EACZ,MAAM;AAAA,EACN,cAAc;AAChB;;;;;;;AAKA,UAAM,QAAQ;AACd,UAAM,WAAWA;AACjB,UAAM,EAAE,MAAM,WAAW,UAAU,eAAe,MAAM,cAAc;AAEtE,UAAM,EAAE,cAAc,aAAa,cAAc;AACjD,UAAM,EAAE,qBAAqB,yBAC3B,gBACA,oBACF;AAEA,UAAM,WAAW;AACjB,UAAM,YAAY;AAClB,UAAM,YAAY;AAClB,UAAM,aAAa;AAEnB,QAAI,WAAW;AACf,QAAI,mBAAmB;AACvB,UAAM,cAAc,IAAsB,EAAE;AAC5C,UAAM,mBAAmB,IAAI,EAAE;AAC/B,UAAM,gBAAgB,IAAI,EAAE;AAC5B,UAAM,YAAY,IAAI,KAAK;AAC3B,UAAM,qBAAqB,IAAI,KAAK;AACpC,UAAM,UAAU,IAAI,KAAK;AAEzB,UAAM,YAAY;AAElB,UAAM,SAAS,SAAS,MAAM,SAAS,KAAmB;AAE1D,UAAM,oBAAoB,SAAS,MAAM;AACvC,YAAM,cAAc,YAAY,MAAM,SAAS;AAC/C,aAAQ,gBAAe,QAAQ,UAAU,UAAU;AAAA,KACpD;AAED,UAAM,oBAAoB,SAAS,MAAM,CAAC,MAAM,eAAe,QAAQ,KAAK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