{"version":3,"sources":["../src/components/Dropdown/useDropdown.ts"],"sourcesContent":["'use client';\n\nimport type * as React from 'react';\nimport { mergeCallbacks, slot, useEventCallback, useMergedRefs } from '@fluentui/react-utilities';\n\nimport type { DropdownProps, DropdownState } from './Dropdown.types';\nimport { useButtonTriggerSlot } from '@fluentui/react-combobox';\nimport { Listbox } from './Listbox';\nimport { stringifyDataAttribute } from '../../utils';\nimport { useListboxPopupState } from './useListboxPopupState';\n\n/**\n * Create the state required to render Dropdown.\n *\n * The returned state can be modified with hooks such as useDropdownStyles,\n * before being passed to renderDropdown.\n *\n * @param props - props from this instance of Dropdown\n * @param ref - reference to root HTMLButtonElement of Dropdown\n */\nexport const useDropdown = (props: DropdownProps, ref: React.Ref<HTMLButtonElement>): DropdownState => {\n  const {\n    props: mergedProps,\n    triggerRef,\n    activeParentRef,\n    activeDescendantController,\n    triggerNativeProps,\n    internalState,\n    listbox,\n    rootSlot,\n  } = useListboxPopupState<DropdownProps, HTMLButtonElement>(props, {\n    primarySlotTagName: 'button',\n    fieldControlOptions: { supportsLabelFor: true },\n    baseStateExtras: () => ({ freeform: false }),\n    rootDefaultProps: p => ({ children: p.children }),\n  });\n\n  const { appearance: _appearance, size: _size, freeform: _freeform, ...baseState } = internalState;\n  const { clearable, clearSelection, disabled, hasFocus, multiselect, open, selectedOptions } = baseState;\n\n  const trigger = useButtonTriggerSlot(mergedProps.button ?? {}, useMergedRefs(triggerRef, activeParentRef, ref), {\n    state: internalState,\n    defaultProps: {\n      type: 'button',\n      // tabster navigation breaks if the button is disabled and tabIndex is 0\n      tabIndex: triggerNativeProps.disabled ? undefined : 0,\n      children: baseState.value || mergedProps.placeholder,\n      'aria-controls': open ? listbox?.id : undefined,\n      ...triggerNativeProps,\n    },\n    activeDescendantController,\n  });\n\n  const showClearButton = selectedOptions.length > 0 && !disabled && clearable && !multiselect;\n  const state: DropdownState = {\n    components: { root: 'div', button: 'button', clearButton: 'button', expandIcon: 'span', listbox: Listbox },\n    root: rootSlot,\n    button: {\n      ...trigger,\n      'data-state': open ? 'open' : 'closed',\n      'data-disabled': stringifyDataAttribute(trigger.disabled),\n      'data-placeholder': stringifyDataAttribute(!baseState.value),\n    },\n    listbox: open || hasFocus ? listbox : undefined,\n    clearButton: slot.optional(mergedProps.clearButton, {\n      defaultProps: {\n        'aria-label': 'Clear selection',\n        // Safari doesn't allow to focus an element with this\n        // when the element is not visible (display: none) we need to remove it to avoid tabster issues\n        tabIndex: showClearButton ? 0 : undefined,\n        type: 'button',\n      },\n      elementType: 'button',\n      renderByDefault: true,\n    }),\n    expandIcon: slot.optional(mergedProps.expandIcon, {\n      renderByDefault: true,\n      elementType: 'span',\n    }),\n    placeholderVisible: !baseState.value && !!mergedProps.placeholder,\n    showClearButton,\n    activeDescendantController,\n    ...baseState,\n  };\n\n  const onClearButtonClick = useEventCallback(\n    // eslint-disable-next-line react-hooks/refs\n    mergeCallbacks(state.clearButton?.onClick, (ev: React.MouseEvent<HTMLButtonElement>) => {\n      clearSelection(ev);\n      triggerRef.current?.focus();\n    }),\n  );\n\n  if (state.clearButton) {\n    state.clearButton.onClick = onClearButtonClick;\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.clearButton = undefined;\n  }\n\n  return state;\n};\n"],"names":["useDropdown","props","ref","mergedProps","triggerRef","activeParentRef","activeDescendantController","triggerNativeProps","internalState","listbox","rootSlot","useListboxPopupState","primarySlotTagName","fieldControlOptions","supportsLabelFor","baseStateExtras","freeform","rootDefaultProps","p","children","appearance","_appearance","size","_size","_freeform","baseState","clearable","clearSelection","disabled","hasFocus","multiselect","open","selectedOptions","trigger","useButtonTriggerSlot","button","useMergedRefs","state","defaultProps","type","tabIndex","undefined","value","placeholder","id","showClearButton","length","components","root","clearButton","expandIcon","Listbox","stringifyDataAttribute","slot","optional","elementType","renderByDefault","placeholderVisible","onClearButtonClick","useEventCallback","mergeCallbacks","onClick","ev","current","focus"],"mappings":"AAAA;;;;;+B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