{"version":3,"sources":["../src/components/Combobox/useCombobox.ts"],"sourcesContent":["'use client';\n\nimport type * as React from 'react';\nimport { mergeCallbacks, useEventCallback, useMergedRefs, slot } from '@fluentui/react-utilities';\nimport type { ComboboxProps, ComboboxState } from './Combobox.types';\nimport { useInputTriggerSlot } from '@fluentui/react-combobox';\nimport { Listbox } from '../Dropdown/Listbox';\nimport { stringifyDataAttribute } from '../../utils';\nimport { useListboxPopupState } from '../Dropdown/useListboxPopupState';\n\nexport const useCombobox = (props: ComboboxProps, ref: React.Ref<HTMLInputElement>): ComboboxState => {\n  const { freeform } = props;\n\n  const {\n    props: mergedProps,\n    triggerRef,\n    activeParentRef,\n    activeDescendantController,\n    triggerNativeProps,\n    internalState,\n    listbox,\n    rootSlot,\n  } = useListboxPopupState<ComboboxProps, HTMLInputElement>(props, {\n    primarySlotTagName: 'input',\n    fieldControlOptions: { supportsLabelFor: true, supportsRequired: true },\n    // For multiselect, editable should be false to display comma-separated values in the trigger\n    // For single-select, editable is true to allow filtering\n    baseStateExtras: p => ({ editable: !p.multiselect }),\n  });\n\n  const { appearance: _appearance, size: _size, ...baseState } = internalState;\n  const { clearable, clearSelection, disabled, hasFocus, multiselect, open, selectedOptions } = baseState;\n\n  const triggerSlot = useInputTriggerSlot(mergedProps.input ?? {}, useMergedRefs(triggerRef, activeParentRef, ref), {\n    state: internalState,\n    freeform,\n    defaultProps: {\n      type: 'text',\n      value: baseState.value ?? '',\n      'aria-controls': open ? listbox?.id : undefined,\n      ...triggerNativeProps,\n    },\n    activeDescendantController,\n  });\n\n  const showClearIcon = selectedOptions.length > 0 && !disabled && clearable && !multiselect;\n\n  const state: ComboboxState = {\n    components: { root: 'div', input: 'input', expandIcon: 'span', clearIcon: 'span', listbox: Listbox },\n    root: rootSlot,\n    input: {\n      ...triggerSlot,\n      'data-state': open ? 'open' : 'closed',\n      'data-disabled': stringifyDataAttribute(triggerSlot.disabled),\n      'data-placeholder': stringifyDataAttribute(!baseState.value),\n    },\n    listbox: open || hasFocus ? listbox : undefined,\n    clearIcon: slot.optional(mergedProps.clearIcon, {\n      defaultProps: { 'aria-hidden': 'true' },\n      elementType: 'span',\n      renderByDefault: true,\n    }),\n    expandIcon: slot.optional(mergedProps.expandIcon, {\n      renderByDefault: true,\n      elementType: 'span',\n    }),\n    showClearIcon,\n    activeDescendantController,\n    ...baseState,\n  };\n\n  const onClearIconMouseDown = useEventCallback(\n    mergeCallbacks(state.clearIcon?.onMouseDown, (ev: React.MouseEvent<HTMLSpanElement>) => {\n      ev.preventDefault();\n    }),\n  );\n  const onClearIconClick = useEventCallback(\n    // eslint-disable-next-line react-hooks/refs\n    mergeCallbacks(state.clearIcon?.onClick, (ev: React.MouseEvent<HTMLSpanElement>) => {\n      clearSelection(ev);\n      triggerRef.current?.focus();\n    }),\n  );\n\n  if (state.clearIcon) {\n    state.clearIcon.onMouseDown = onClearIconMouseDown;\n    state.clearIcon.onClick = onClearIconClick;\n  }\n\n  // Heads up! We don't support \"clearable\" in multiselect mode, so we should never display a slot\n  if (multiselect) {\n    state.clearIcon = undefined;\n  }\n\n  return state;\n};\n"],"names":["useCombobox","props","ref","freeform","mergedProps","triggerRef","activeParentRef","activeDescendantController","triggerNativeProps","internalState","listbox","rootSlot","useListboxPopupState","primarySlotTagName","fieldControlOptions","supportsLabelFor","supportsRequired","baseStateExtras","p","editable","multiselect","appearance","_appearance","size","_size","baseState","clearable","clearSelection","disabled","hasFocus","open","selectedOptions","triggerSlot","useInputTriggerSlot","input","useMergedRefs","state","defaultProps","type","value","id","undefined","showClearIcon","length","components","root","expandIcon","clearIcon","Listbox","stringifyDataAttribute","slot","optional","elementType","renderByDefault","onClearIconMouseDown","useEventCallback","mergeCallbacks","onMouseDown","ev","preventDefault","onClearIconClick","onClick","current","focus"],"mappings":"AAAA;;;;;+BAUaA;;;eAAAA;;;gCAPyD;+BAElC;yBACZ;uBACe;sCACF;AAE9B,MAAMA,cAAc,CAACC,OAAsBC;IAChD,MAAM,EAAEC,QAAQ,E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