{"ast":null,"code":"import _toConsumableArray from \"@babel/runtime/helpers/toConsumableArray\";\nimport _defineProperty from \"@babel/runtime/helpers/defineProperty\";\nimport _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport _objectWithoutProperties from \"@babel/runtime/helpers/objectWithoutProperties\";\nvar _excluded = [\"onSelectedItemChange\", \"options\", \"renderItem\", \"getOptionLabel\", \"getOptionKey\", \"onChange\", \"numberOfItems\"];\nfunction ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }\nfunction _extends() {\n  _extends = Object.assign || function (target) {\n    for (var i = 1; i < arguments.length; i++) {\n      var source = arguments[i];\n      for (var key in source) {\n        if (Object.prototype.hasOwnProperty.call(source, key)) {\n          target[key] = source[key];\n        }\n      }\n    }\n    return target;\n  };\n  return _extends.apply(this, arguments);\n}\nimport React from 'react';\nimport { useButton } from '@react-native-aria/button';\nimport { useComboBoxState } from '@react-stately/combobox';\nimport { useComboBox } from '@react-native-aria/combobox';\nimport { useListBox, useOption } from '@react-native-aria/listbox';\nimport ScrollView from \"react-native-web/dist/exports/ScrollView\";\nimport findNodeHandle from \"react-native-web/dist/exports/findNodeHandle\";\nimport Platform from \"react-native-web/dist/exports/Platform\";\nimport { Item } from '@react-stately/collections';\nimport Box from \"../../primitives/Box\";\nimport { Pressable } from \"../../primitives/Pressable\";\nimport Text from \"../../primitives/Text\";\nimport { extractInObject } from \"../../../theme/tools\";\nimport { layoutPropsList } from \"./types\";\nimport { Input } from \"../../primitives/Input\";\nimport { useThemeProps } from \"../../../hooks\";\nexport var Typeahead = React.forwardRef(function (_ref, ref) {\n  var onSelectedItemChange = _ref.onSelectedItemChange,\n    options = _ref.options,\n    renderItem = _ref.renderItem,\n    getOptionLabel = _ref.getOptionLabel,\n    getOptionKey = _ref.getOptionKey,\n    onChange = _ref.onChange,\n    numberOfItems = _ref.numberOfItems,\n    rest = _objectWithoutProperties(_ref, _excluded);\n  return React.createElement(ComboBoxImplementation, _extends({}, rest, {\n    onSelectionChange: onSelectedItemChange,\n    items: numberOfItems !== undefined ? options.slice(0, numberOfItems) : options,\n    onInputChange: onChange,\n    ref: ref\n  }), function (item) {\n    if (typeof item !== 'string' && getOptionLabel === undefined) {\n      throw new Error('Please use getOptionLabel prop');\n    }\n    if (item.id === undefined && getOptionKey === undefined) {\n      throw new Error('Please use getOptionKey prop');\n    }\n    var optionLabel = getOptionLabel ? getOptionLabel(item) : item;\n    var optionKey = getOptionKey ? getOptionKey(item) : item.id !== undefined ? item.id : optionLabel;\n    return React.createElement(Item, {\n      textValue: optionLabel,\n      key: optionKey\n    }, renderItem ? renderItem(item) : React.createElement(Box, {\n      p: 2,\n      justifyContent: \"center\"\n    }, React.createElement(Text, null, optionLabel)));\n  });\n});\nvar ComboBoxImplementation = React.forwardRef(function (props, ref) {\n  var _extractInObject = extractInObject(props, layoutPropsList),\n    _extractInObject2 = _slicedToArray(_extractInObject, 1),\n    layoutProps = _extractInObject2[0];\n  var state = useComboBoxState(props);\n  var triggerRef = React.useRef(null);\n  var inputRef = React.useRef(null);\n  var listBoxRef = React.useRef(null);\n  var popoverRef = React.useRef(null);\n  var _useComboBox = useComboBox(_objectSpread(_objectSpread({}, props), {}, {\n      inputRef: inputRef,\n      buttonRef: triggerRef,\n      listBoxRef: listBoxRef,\n      popoverRef: popoverRef,\n      menuTrigger: 'input'\n    }), state),\n    triggerProps = _useComboBox.buttonProps,\n    inputProps = _useComboBox.inputProps,\n    listBoxProps = _useComboBox.listBoxProps,\n    labelProps = _useComboBox.labelProps;\n  var toggleIconSetter = function toggleIconSetter() {\n    if (typeof props.toggleIcon === 'function') return props.toggleIcon({\n      isOpen: state.isOpen\n    });\n    return props.toggleIcon;\n  };\n  var _useButton = useButton(triggerProps),\n    buttonProps = _useButton.buttonProps;\n  return React.createElement(Box, _extends({\n    flexDirection: \"row\"\n  }, layoutProps, {\n    ref: ref\n  }), React.createElement(Box, {\n    flex: 1\n  }, props.label && React.createElement(Text, _extends({}, labelProps, {\n    pb: 1\n  }), props.label), React.createElement(Input, _extends({}, inputProps, {\n    ref: inputRef,\n    InputRightElement: React.createElement(Pressable, _extends({}, buttonProps, {\n      ref: triggerRef\n    }), toggleIconSetter())\n  })), state.isOpen && React.createElement(ListBoxPopup, _extends({}, listBoxProps, {\n    listBoxRef: listBoxRef,\n    popoverRef: popoverRef,\n    state: state,\n    label: props.label\n  }))));\n});\nfunction ListBoxPopup(props) {\n  var popoverRef = props.popoverRef,\n    listBoxRef = props.listBoxRef,\n    state = props.state,\n    dropdownHeight = props.dropdownHeight,\n    label = props.label;\n  var _useListBox = useListBox({\n      label: label,\n      autoFocus: state.focusStrategy,\n      disallowEmptySelection: true\n    }, state, listBoxRef),\n    listBoxProps = _useListBox.listBoxProps;\n  return React.createElement(Box, {\n    ref: popoverRef\n  }, React.createElement(Box, {\n    position: \"absolute\",\n    width: \"100%\",\n    maxHeight: dropdownHeight !== null && dropdownHeight !== void 0 ? dropdownHeight : 200\n  }, React.createElement(ScrollView, _extends({}, listBoxProps, {\n    keyboardShouldPersistTaps: \"handled\",\n    ref: function ref(node) {\n      if (Platform.OS === 'web') {\n        listBoxRef.current = findNodeHandle(node);\n      } else {\n        listBoxRef.current = node;\n      }\n    }\n  }), _toConsumableArray(state.collection).map(function (item) {\n    return React.createElement(Option, {\n      key: item.key,\n      item: item,\n      state: state\n    });\n  }))));\n}\nfunction Option(_ref2) {\n  var item = _ref2.item,\n    state = _ref2.state;\n  var searchItemStyle = useThemeProps('TypeAheadSearchItem', {});\n  var ref = React.useRef(null);\n  var isDisabled = state.disabledKeys.has(item.key);\n  var isSelected = state.selectionManager.isSelected(item.key);\n  var isFocused = state.selectionManager.focusedKey === item.key;\n  var _useOption = useOption({\n      key: item.key,\n      isDisabled: isDisabled,\n      isSelected: isSelected,\n      shouldFocusOnHover: true,\n      shouldUseVirtualFocus: true\n    }, state, ref),\n    optionProps = _useOption.optionProps;\n  var backgroundColor = searchItemStyle.backgroundColor;\n  var opacity = 1;\n  if (isSelected) {\n    backgroundColor = searchItemStyle._focus.backgroundColor;\n  } else if (isFocused) {\n    backgroundColor = searchItemStyle._focus.backgroundColor;\n  } else if (isDisabled) {\n    opacity = 0.6;\n    backgroundColor = searchItemStyle._disabled.backgroundColor;\n  }\n  return React.createElement(Pressable, _extends({}, optionProps, {\n    opacity: opacity,\n    cursor: isDisabled ? Platform.OS === 'web' ? 'not-allowed' : null : null,\n    backgroundColor: backgroundColor,\n    ref: ref\n  }), item.rendered);\n}","map":{"version":3,"sources":["Typeahead.tsx"],"names":["React","useButton","useComboBoxState","useComboBox","useListBox","useOption","Item","Box","Pressable","Text","extractInObject","layoutPropsList","Input","useThemeProps","Typeahead","forwardRef","onSelectedItemChange","options","renderItem","getOptionLabel","getOptionKey","onChange","numberOfItems","rest","ref","undefined","slice","item","Error","id","optionLabel","optionKey","ComboBoxImplementation","props","layoutProps","state","triggerRef","useRef","inputRef","listBoxRef","popoverRef","buttonProps","triggerProps","inputProps","listBoxProps","labelProps","buttonRef","menuTrigger","toggleIconSetter","toggleIcon","isOpen","label","ListBoxPopup","dropdownHeight","autoFocus","focusStrategy","disallowEmptySelection","node","Platform","OS","current","findNodeHandle","collection","map","key","Option","searchItemStyle","isDisabled","disabledKeys","has","isSelected","selectionManager","isFocused","focusedKey","optionProps","shouldFocusOnHover","shouldUseVirtualFocus","backgroundColor","opacity","_foc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SAAS,GAAGrC,KAAK,CAACoC,gBAANpC,CAAuBsC,UAAvBtC,KAAsCR,IAAI,CAACqC,GAA3D;EAEA,IAAA,UAAA,GAAsB3D,SAAS,CAC7B;MACE2D,GAAG,EAAErC,IAAI,CAACqC,GADZ;MAEEG,UAFF,EAEEA,UAFF;MAGEG,UAHF,EAGEA,UAHF;MAIEK,kBAAkB,EAAE,IAJtB;MAKEC,qBAAqB,EAAE;IALzB,CAD6B,EAQ7BzC,KAR6B,EAS7BX,GAT6B,CAA/B;IAAMkD,WAAAA,GAAAA,UAAAA,CAAAA,WAAAA;EAYN,IAAIG,eAAe,GAAGX,eAAe,CAACW,eAAtC;EACA,IAAIC,OAAO,GAAG,CAAd;EAEA,IAAIR,UAAJ,EAAgB;IACdO,eAAe,GAAGX,eAAe,CAACa,MAAhBb,CAAuBW,eAAzCA;EACD,CAFD,MAEO,IAAIL,SAAJ,EAAe;IACpBK,eAAe,GAAGX,eAAe,CAACa,MAAhBb,CAAuBW,eAAzCA;EACD,CAFM,MAEA,IAAIV,UAAJ,EAAgB;IACrBW,OAAO,GAAG,GAAVA;IACAD,eAAe,GAAGX,eAAe,CAACc,SAAhBd,CAA0BW,eAA5CA;EACD;EAED,OACE,KAAA,CAAA,aAAA,CAAC,SAAD,EAAA,QAAA,CAAA,CAAA,CAAA,EACMH,WADN,EAAA;IAEE,OAAO,EAAEI,OAFX;IAGE,MAAM,EACJX,UAAU,GAAIT,QAAQ,CAACC,EAATD,KAAgB,KAAhBA,GAAwB,aAAxBA,GAAwC,IAA5C,GAAoD,IAJlE;IAME,eAAe,EAAEmB,eANnB;IAOE,GAAG,EAAErD;EAPP,CAAA,CAAA,EASGG,IAAI,CAACsD,QATR,CADF;AAaD","sourcesContent":["import React from 'react';\nimport { useButton } from '@react-native-aria/button';\nimport { ComboBoxState, useComboBoxState } from '@react-stately/combobox';\nimport { useComboBox } from '@react-native-aria/combobox';\nimport { useListBox, useOption } from '@react-native-aria/listbox';\nimport { ScrollView, findNodeHandle, Platform } from 'react-native';\nimport { Item } from '@react-stately/collections';\nimport Box from '../../primitives/Box';\nimport { Pressable } from '../../primitives/Pressable';\nimport Text from '../../primitives/Text';\nimport { extractInObject } from '../../../theme/tools';\nimport { ITypeaheadProps, IComboBoxProps, layoutPropsList } from './types';\nimport { Input } from '../../primitives/Input';\nimport { useThemeProps } from '../../../hooks';\n\nexport const Typeahead = React.forwardRef(\n  (\n    {\n      onSelectedItemChange,\n      options,\n      renderItem,\n      getOptionLabel,\n      getOptionKey,\n      onChange,\n      numberOfItems,\n      ...rest\n    }: ITypeaheadProps,\n    ref?: any\n  ) => {\n    return (\n      <ComboBoxImplementation\n        {...rest}\n        onSelectionChange={onSelectedItemChange}\n        items={\n          numberOfItems !== undefined\n            ? options.slice(0, numberOfItems)\n            : options\n        }\n        onInputChange={onChange}\n        ref={ref}\n      >\n        {(item: any) => {\n          if (typeof item !== 'string' && getOptionLabel === undefined) {\n            throw new Error('Please use getOptionLabel prop');\n          }\n\n          if (item.id === undefined && getOptionKey === undefined) {\n            throw new Error('Please use getOptionKey prop');\n          }\n\n          const optionLabel = getOptionLabel ? getOptionLabel(item) : item;\n          const optionKey = getOptionKey\n            ? getOptionKey(item)\n            : item.id !== undefined\n            ? item.id\n            : optionLabel;\n\n          return (\n            <Item textValue={optionLabel} key={optionKey}>\n              {renderItem ? (\n                renderItem(item)\n              ) : (\n                <Box p={2} justifyContent=\"center\">\n                  <Text>{optionLabel}</Text>\n                </Box>\n              )}\n            </Item>\n          );\n        }}\n      </ComboBoxImplementation>\n    );\n  }\n);\n\nconst ComboBoxImplementation = React.forwardRef(\n  (props: IComboBoxProps, ref?: any) => {\n    const [layoutProps] = extractInObject(props, layoutPropsList);\n    let state = useComboBoxState(props);\n\n    let triggerRef = React.useRef(null);\n    let inputRef = React.useRef(null);\n    let listBoxRef = React.useRef(null);\n    let popoverRef = React.useRef(null);\n\n    let {\n      buttonProps: triggerProps,\n      inputProps,\n      listBoxProps,\n      labelProps,\n    } = useComboBox(\n      {\n        ...props,\n        inputRef,\n        buttonRef: triggerRef,\n        listBoxRef,\n        popoverRef,\n        menuTrigger: 'input',\n      },\n      state\n    );\n\n    const toggleIconSetter = () => {\n      if (typeof props.toggleIcon === 'function')\n        return props.toggleIcon({\n          isOpen: state.isOpen,\n        });\n      return props.toggleIcon;\n    };\n\n    let { buttonProps } = useButton(triggerProps);\n\n    return (\n      <Box flexDirection=\"row\" {...layoutProps} ref={ref}>\n        <Box flex={1}>\n          {props.label && (\n            <Text {...labelProps} pb={1}>\n              {props.label}\n            </Text>\n          )}\n          <Input\n            {...inputProps}\n            ref={inputRef}\n            InputRightElement={\n              // @ts-ignore - RN has hitSlop type inconsistent for View and Pressable!\n              <Pressable {...buttonProps} ref={triggerRef}>\n                {toggleIconSetter()}\n              </Pressable>\n            }\n          />\n\n          {state.isOpen && (\n            <ListBoxPopup\n              {...listBoxProps}\n              listBoxRef={listBoxRef}\n              popoverRef={popoverRef}\n              state={state}\n              label={props.label}\n            />\n          )}\n        </Box>\n      </Box>\n    );\n  }\n);\n\ntype IListBoxProps = {\n  popoverRef: any;\n  listBoxRef: any;\n  state: ComboBoxState<any>;\n  dropdownHeight: number;\n  label: string;\n};\n\nfunction ListBoxPopup(props: IListBoxProps) {\n  let { popoverRef, listBoxRef, state, dropdownHeight, label } = props;\n\n  let { listBoxProps } = useListBox(\n    {\n      label,\n      autoFocus: state.focusStrategy,\n      disallowEmptySelection: true,\n    },\n    state,\n    listBoxRef\n  );\n\n  return (\n    <Box ref={popoverRef}>\n      <Box position=\"absolute\" width=\"100%\" maxHeight={dropdownHeight ?? 200}>\n        <ScrollView\n          {...listBoxProps}\n          keyboardShouldPersistTaps=\"handled\"\n          ref={(node) => {\n            if (Platform.OS === 'web') {\n              listBoxRef.current = findNodeHandle(node);\n            } else {\n              listBoxRef.current = node;\n            }\n          }}\n        >\n          {[...state.collection].map((item) => (\n            <Option key={item.key} item={item} state={state} />\n          ))}\n        </ScrollView>\n      </Box>\n    </Box>\n  );\n}\n\nfunction Option({ item, state }: { item: any; state: ComboBoxState<any> }) {\n  const searchItemStyle = useThemeProps('TypeAheadSearchItem', {});\n\n  let ref = React.useRef(null);\n  let isDisabled = state.disabledKeys.has(item.key);\n  let isSelected = state.selectionManager.isSelected(item.key);\n  let isFocused = state.selectionManager.focusedKey === item.key;\n\n  let { optionProps } = useOption(\n    {\n      key: item.key,\n      isDisabled,\n      isSelected,\n      shouldFocusOnHover: true,\n      shouldUseVirtualFocus: true,\n    },\n    state,\n    ref\n  );\n\n  let backgroundColor = searchItemStyle.backgroundColor;\n  let opacity = 1;\n\n  if (isSelected) {\n    backgroundColor = searchItemStyle._focus.backgroundColor;\n  } else if (isFocused) {\n    backgroundColor = searchItemStyle._focus.backgroundColor;\n  } else if (isDisabled) {\n    opacity = 0.6;\n    backgroundColor = searchItemStyle._disabled.backgroundColor;\n  }\n\n  return (\n    <Pressable\n      {...optionProps}\n      opacity={opacity}\n      cursor={\n        isDisabled ? (Platform.OS === 'web' ? 'not-allowed' : null) : null\n      }\n      backgroundColor={backgroundColor}\n      ref={ref}\n    >\n      {item.rendered}\n    </Pressable>\n  );\n}\n"]},"metadata":{},"sourceType":"module"}