import React, { forwardRef, useCallback, useEffect, useMemo } from 'react'; import { Picker } from '@react-native-picker/picker'; import useTheme from '../../context/theme/useTheme'; import type { SelectProps } from './types'; import { Box } from '../Box/Box'; import createSxStyle, { getSxStyleAndProps } from '../../lib/sx'; import { BottomSheet } from '../BottomSheet/BottomSheet'; import { Button } from '../Button'; import { InputBox } from '../Input/InputBox'; import { ChevronUpIcon, ChevronDownIcon } from '../../icons'; export const Select = forwardRef, SelectProps>( ( { items, placeholder = 'Select an item', helperText, onChange, startContent, value, shape = 'rounded', endContent, error = false, size = 'middle', color = 'accents.2', background = 'input', borderColor = 'border', okText = 'Apply', cancelText = 'Cancel', style = {}, sx, styles, isDisabled, onSelectionChange, ...restProps }, ref ) => { const [selection, setSelection] = React.useState(undefined); const [preSelection, setPreSelection] = React.useState(undefined); const [isOpen, setIsOpen] = React.useState(false); const theme = useTheme(); const onInternalChange = (_value: string, findIndex: number) => { const index = findIndex > 0 ? findIndex - 1 : findIndex; setPreSelection(items[index]); onSelectionChange?.(_value, index); }; const onApply = () => { onChange?.(preSelection?.value, preSelection); setSelection(preSelection); setIsOpen(false); }; const onOpen = () => { setIsOpen(true); }; const onClose = () => { setIsOpen(false); }; const RenderIcon = useCallback(() => { if (endContent) { return <>{endContent}; } if (isOpen) return ; return ; }, [endContent, isOpen, borderColor]); const resolveProps = getSxStyleAndProps( { ...restProps, sx: sx?.root, style: styles?.root }, theme ); const itemsRecord = useMemo(() => { return items.reduce((acc, item) => { acc[item.value] = item; return acc; }, {}); }, [items]); // effects useEffect(() => { if (![undefined, null].includes(value)) { const valueItem = itemsRecord?.[value]; setPreSelection(valueItem); setSelection(valueItem); } }, [value, itemsRecord]); const text = selection?.label || placeholder; return ( } helperText={helperText} /> } > {items.map((item) => ( ))} ); } );