{"version":3,"file":"SelectItem.js","names":[],"sources":["jsx/SelectItem.js"],"sourcesContent":["import { useComposedRefs } from \"@tamagui/compose-refs\";\nimport { useAdaptIsActive } from \"@tamagui/adapt\";\nimport { isWeb, useIsomorphicLayoutEffect } from \"@tamagui/constants\";\nimport {\n  createChangeEventDetails,\n  createStyledHOC,\n  createStyledContext,\n  styled,\n  View\n} from \"@tamagui/core\";\nimport { composeEventHandlers } from \"@tamagui/helpers\";\nimport * as React from \"react\";\nimport { useSelectItemParentContext } from \"./context\";\nimport { getSelectOptionProps } from \"./selectionController\";\nimport { jsx } from \"react/jsx-runtime\";\nconst ITEM_NAME = \"SelectItem\";\nconst {\n  Provider: SelectItemContextProvider,\n  useStyledContext: useSelectItemContext\n} = createStyledContext(null, ITEM_NAME);\nconst SelectItemFrame = styled(View, {\n  displayName: ITEM_NAME,\n  alignItems: \"center\",\n  flexDirection: \"row\"\n});\nconst SelectItem = createStyledHOC(\n  SelectItemFrame,\n  function SelectItem2(propsIn, forwardedRef) {\n    const props = propsIn;\n    const {\n      scope,\n      value,\n      disabled: disabledProp,\n      \"aria-disabled\": ariaDisabled,\n      textValue: textValueProp,\n      ...restProps\n    } = props;\n    const disabled = disabledProp ?? ariaDisabled === true;\n    const context = useSelectItemParentContext(scope);\n    const isAdapted = useAdaptIsActive(context.adaptScope);\n    const {\n      registry,\n      mode,\n      selectedValues,\n      selectValue,\n      activeIndexSubscribe,\n      activeIndexRef,\n      allowMouseUpRef,\n      allowSelectRef,\n      selectTimeoutRef,\n      getItemProps,\n      shouldRenderWebNative,\n      setActiveIndex,\n      lastPointerRef,\n      moveActive,\n      search\n    } = context;\n    const isSelected = selectedValues.includes(value);\n    const pendingMouseUpSelectionRef = React.useRef(false);\n    const itemNodeRef = React.useRef(null);\n    const registrationRef = React.useRef(\n      null\n    );\n    const initialRegistration = React.useRef({\n      value,\n      disabled,\n      textValue: textValueProp\n    });\n    useIsomorphicLayoutEffect(() => {\n      const registration = registry.registerItem(initialRegistration.current);\n      registrationRef.current = registration;\n      registration.setNode(itemNodeRef.current);\n      return () => {\n        registrationRef.current = null;\n        registration.unregister();\n      };\n    }, [registry]);\n    useIsomorphicLayoutEffect(() => {\n      registrationRef.current?.update({ value, disabled, textValue: textValueProp });\n    }, [disabled, textValueProp, value]);\n    useIsomorphicLayoutEffect(() => {\n      registrationRef.current?.setNode(itemNodeRef.current);\n    });\n    const [isActive, setIsActive] = React.useState(false);\n    const isActiveRef = React.useRef(false);\n    React.useEffect(() => {\n      const handleActiveIndex = (i) => {\n        const next = i != null && registry.getIndex(value) === i;\n        if (next !== isActiveRef.current) {\n          isActiveRef.current = next;\n          setIsActive(next);\n        }\n        if (next && isWeb) {\n          requestAnimationFrame(() => {\n            itemNodeRef.current?.focus?.();\n          });\n        }\n      };\n      handleActiveIndex(activeIndexRef?.current ?? null);\n      return activeIndexSubscribe(handleActiveIndex);\n    }, [activeIndexRef, activeIndexSubscribe, registry, value]);\n    const textId = React.useId();\n    const refCallback = React.useCallback((node) => {\n      itemNodeRef.current = node;\n      registrationRef.current?.setNode(node);\n    }, []);\n    const composedRefs = useComposedRefs(forwardedRef, refCallback);\n    const handleSelect = React.useCallback(\n      (event, reason = \"item-press\") => {\n        if (disabled) return;\n        const nativeEvent = event?.nativeEvent || event;\n        selectValue(\n          value,\n          createChangeEventDetails(\n            reason,\n            nativeEvent,\n            event?.currentTarget\n          )\n        );\n      },\n      [disabled, selectValue, value]\n    );\n    const handleMouseMove = React.useCallback(\n      (event) => {\n        if (disabled) return;\n        const index = registry.getIndex(value);\n        if (index < 0) return;\n        const last = lastPointerRef.current;\n        if (event.clientX === last.x && event.clientY === last.y) return;\n        last.x = event.clientX;\n        last.y = event.clientY;\n        setActiveIndex(index, {\n          reason: \"item-hover\",\n          event: event.nativeEvent || event,\n          trigger: event.currentTarget,\n          index\n        });\n      },\n      [disabled, lastPointerRef, registry, setActiveIndex, value]\n    );\n    const handleKeyDown = React.useCallback(\n      (event) => {\n        if (disabled) return;\n        if (event.key === \"Enter\" || event.key === \" \") {\n          event.preventDefault();\n          handleSelect(event, \"keyboard\");\n          return;\n        }\n        if (event.key === \"ArrowDown\" || event.key === \"ArrowUp\") {\n          event.preventDefault();\n          event.stopPropagation();\n          moveActive(event.key === \"ArrowDown\" ? 1 : -1, event.nativeEvent || event);\n          return;\n        }\n        if (!getItemProps && event.key?.length === 1 && !event.metaKey && !event.ctrlKey) {\n          search(event.key, event.nativeEvent || event);\n        }\n        if (allowSelectRef) {\n          allowSelectRef.current = true;\n        }\n      },\n      [allowSelectRef, disabled, handleSelect, getItemProps, moveActive, search]\n    );\n    const selectItemProps = React.useMemo(() => {\n      if (getItemProps) {\n        const {\n          onTouchMove,\n          onTouchEnd,\n          onKeyDown,\n          onClick,\n          onMouseUp,\n          onMouseMove,\n          onPress,\n          ...itemProps\n        } = restProps;\n        const interactionProps = getItemProps({\n          ...itemProps,\n          onTouchMove() {\n            allowSelectRef.current = true;\n            allowMouseUpRef.current = false;\n          },\n          onTouchEnd() {\n            allowSelectRef.current = false;\n            allowMouseUpRef.current = true;\n          },\n          onMouseMove: handleMouseMove,\n          onKeyDown: handleKeyDown,\n          onClick(event) {\n            if (disabled) return;\n            const shouldSelect = pendingMouseUpSelectionRef.current || allowSelectRef.current;\n            pendingMouseUpSelectionRef.current = false;\n            clearTimeout(selectTimeoutRef.current);\n            allowSelectRef.current = true;\n            if (shouldSelect) {\n              handleSelect(event);\n            }\n          },\n          onMouseUp(event) {\n            if (disabled) return;\n            if (!allowMouseUpRef.current) {\n              allowMouseUpRef.current = true;\n              allowSelectRef.current = true;\n              return;\n            }\n            pendingMouseUpSelectionRef.current = allowSelectRef.current;\n            clearTimeout(selectTimeoutRef.current);\n            selectTimeoutRef.current = setTimeout(() => {\n              allowSelectRef.current = true;\n              if (pendingMouseUpSelectionRef.current) {\n                pendingMouseUpSelectionRef.current = false;\n                handleSelect(event);\n              }\n            });\n          }\n        });\n        return {\n          ...interactionProps,\n          onTouchMove: composeEventHandlers(\n            onTouchMove,\n            interactionProps.onTouchMove\n          ),\n          onTouchEnd: composeEventHandlers(\n            onTouchEnd,\n            interactionProps.onTouchEnd\n          ),\n          onKeyDown: composeEventHandlers(onKeyDown, interactionProps.onKeyDown),\n          onClick(event) {\n            onClick?.(event);\n            if (event.defaultPrevented) {\n              pendingMouseUpSelectionRef.current = false;\n              clearTimeout(selectTimeoutRef.current);\n              allowSelectRef.current = true;\n              return;\n            }\n            interactionProps.onClick?.(event);\n          },\n          onMouseUp: composeEventHandlers(onMouseUp, interactionProps.onMouseUp),\n          onMouseMove: composeEventHandlers(\n            onMouseMove,\n            interactionProps.onMouseMove\n          ),\n          onPress\n        };\n      }\n      return {\n        ...restProps,\n        onKeyDown: composeEventHandlers(restProps.onKeyDown, handleKeyDown),\n        onMouseMove: composeEventHandlers(restProps.onMouseMove, handleMouseMove),\n        onPress: composeEventHandlers(restProps.onPress, handleSelect)\n      };\n    }, [\n      allowMouseUpRef,\n      allowSelectRef,\n      disabled,\n      handleKeyDown,\n      handleMouseMove,\n      handleSelect,\n      getItemProps,\n      restProps,\n      selectTimeoutRef\n    ]);\n    const accessibilityProps = getSelectOptionProps(\n      mode,\n      isSelected,\n      disabled,\n      isWeb ? \"web\" : \"native\"\n    );\n    return /* @__PURE__ */ jsx(\n      SelectItemContextProvider,\n      {\n        scope,\n        value,\n        textId: textId || \"\",\n        textValue: textValueProp,\n        isSelected,\n        children: shouldRenderWebNative ? /* @__PURE__ */ jsx(\"option\", { value, disabled, children: props.children }) : /* @__PURE__ */ jsx(\n          SelectItemFrame,\n          {\n            render: \"div\",\n            ref: composedRefs,\n            \"aria-labelledby\": textId,\n            \"data-state\": isSelected ? \"active\" : \"inactive\",\n            \"data-disabled\": disabled ? \"\" : void 0,\n            tabIndex: disabled ? void 0 : isWeb && isAdapted && (isActive || activeIndexRef?.current == null && isSelected) ? 0 : -1,\n            zIndex: 100,\n            ...selectItemProps,\n            ...accessibilityProps\n          }\n        )\n      }\n    );\n  },\n  {\n    disableTheme: true\n  }\n);\nexport {\n  SelectItem,\n  SelectItemContextProvider,\n  SelectItemFrame,\n  useSelectItemContext\n};\n//# sourceMappingURL=SelectItem.js.map\n"],"mappings":";;;;;;;;;;;AAeA,MAAM,YAAY;AAClB,MAAM,EACJ,UAAU,2BACV,kBAAkB,yBAChB,oBAAoB,MAAM,SAAS;AACvC,MAAM,kBAAkB,OAAO,MAAM;CACnC,aAAa;CACb,YAAY;CACZ,eAAe;AACjB,CAAC;AACD,MAAM,aAAa,gBACjB,iBACA,SAAS,YAAY,SAAS,cAAc;CAC1C,MAAM,QAAQ;CACd,MAAM,EACJ,OACA,OACA,UAAU,cACV,iBAAiB,cACjB,WAAW,eACX,GAAG,cACD;CACJ,MAAM,WAAW,gBAAgB,iBAAiB;CAClD,MAAM,UAAU,2BAA2B,KAAK;CAChD,MAAM,YAAY,iBAAiB,QAAQ,UAAU;CACrD,MAAM,EACJ,UACA,MACA,gBACA,aACA,sBACA,gBACA,iBACA,gBACA,kBACA,cACA,uBACA,gBACA,gBACA,YACA,WACE;CACJ,MAAM,aAAa,eAAe,SAAS,KAAK;CAChD,MAAM,6BAA6B,MAAM,OAAO,KAAK;CACrD,MAAM,cAAc,MAAM,OAAO,IAAI;CACrC,MAAM,kBAAkB,MAAM,OAC5B,IACF;CACA,MAAM,sBAAsB,MAAM,OAAO;EACvC;EACA;EACA,WAAW;CACb,CAAC;CACD,gCAAgC;EAC9B,MAAM,eAAe,SAAS,aAAa,oBAAoB,OAAO;EACtE,gBAAgB,UAAU;EAC1B,aAAa,QAAQ,YAAY,OAAO;EACxC,aAAa;GACX,gBAAgB,UAAU;GAC1B,aAAa,WAAW;EAC1B;CACF,GAAG,CAAC,QAAQ,CAAC;CACb,gCAAgC;EAC9B,gBAAgB,SAAS,OAAO;GAAE;GAAO;GAAU,WAAW;EAAc,CAAC;CAC/E,GAAG;EAAC;EAAU;EAAe;CAAK,CAAC;CACnC,gCAAgC;EAC9B,gBAAgB,SAAS,QAAQ,YAAY,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