import React, { forwardRef, RefAttributes, useContext, type JSX } from "react"; import { useSelectState } from "react-stately"; import { HiddenSelect, useSelect } from "@react-aria/select"; import { filterDOMProps } from "@react-aria/utils"; import { PressResponder } from "@react-aria/interactions"; import { CollectionBuilder } from "@react-aria/collections"; import { ListBoxContext, ListStateContext, } from "@components/Dropdowns/ListBox"; import { OverlayTriggerStateContext } from "../../Overlays/OverlayTrigger/context"; import { useRenderProps } from "@hooks/useRenderProps"; import { useContextProps } from "@hooks/useContextProps"; import { Provider } from "@components/Internal/Provider"; import { FieldErrorContext, FieldLabelContext, FieldMessageContext, } from "@components/Fields"; import { PopoverContext } from "@components/Overlays/Popover/Popover.context"; import { ButtonContext } from "@components/Buttons/Button"; import { ButtonText, SelectFieldWrapper } from "./SelectField.styles"; import { SelectFieldInnerProps, SelectFieldProps, SelectValueProps, } from "./SelectField.types"; import { SelectFieldContext, SelectFieldValueContext, SelectStateContext, } from "./SelectField.context"; type ForwardedSelectField = { ( props: SelectFieldProps & RefAttributes ): JSX.Element; displayName: string; /** Renders the selected value of a SelectField */ Value: typeof SelectFieldValue; }; /** Building blocks for building custom & accessible select components */ export const SelectField = forwardRef(function SelectField( props: SelectFieldProps, ref: React.Ref ) { [props, ref] = useContextProps(SelectFieldContext, props, ref); return ( {(collection) => ( )} ); }) as unknown as ForwardedSelectField; SelectField.displayName = "SelectField"; export function SelectFieldInner( props: SelectFieldInnerProps ) { const state = useSelectState({ ...props, children: undefined }); const { selectRef: ref } = props; const { isInvalid, isDisabled, isRequired, isReadOnly, name, size = "medium", } = props; const { labelProps, triggerProps, valueProps, menuProps, errorMessageProps, descriptionProps, } = useSelect({ ...props, label: true }, state, ref); const renderProps = useRenderProps({ componentClassName: "aje-select", ...props, selectors: { "data-invalid": isInvalid, "data-disabled": isDisabled, "data-required": isRequired, "data-readonly": isReadOnly, }, }); return ( {renderProps.children} ); } export function SelectFieldValue(props: SelectValueProps) { const { placeholder } = props; const valueProps = useContext(SelectFieldValueContext); const state = useContext(SelectStateContext); if (!state) return null; return ( {state.selectedItem ? state.selectedItem.rendered : placeholder} ); } SelectFieldValue.displayName = "SelectField.Value"; SelectField.Value = SelectFieldValue;