import * as React from 'react'; import { useState } from 'react'; import { Dimensions, FlatList, Modal } from 'react-native'; import { Normalize } from '../../utils/normalize'; import CaretRight from '../../icons/CaretRight'; import { Box } from '../Box'; import { Text } from '../Text'; import { TouchableComponent } from '../TouchableComponent'; import { ListHeaderComponent } from './components/ListHeaderComponent'; import { ListBottomComponent } from './components/ListBottomComponent'; import { RadioButton } from './components/RadioButton'; import { useTheme } from '../../theme/ThemeProvider'; import type { VariantProps } from '@shopify/restyle'; import type { Theme } from './../../theme/theme'; export interface ISelect { label: string; value: string | number; } type SelectProps = { items: ISelect[]; placeholder: string; setState: ({ value, label }: ISelect) => void; animationType?: AnimationType; disabled?: boolean; height?: number; variant?: VariantProps['variant']; withLabel?: boolean; modalBody?: ( items: any, setModalVisible: React.Dispatch> ) => JSX.Element; customHeader?: ( setModalVisible: React.Dispatch> ) => JSX.Element; state: ISelect | null; modalTitle?: string; full?: boolean; }; type AnimationType = 'none' | 'slide' | 'fade'; type selectVariants = 'disabled' | 'unfilled' | 'filled'; const windowHeight = Dimensions.get('window').height; const HEADER_HEIGHT = Normalize(64); const MAX_HEIGHT = windowHeight - HEADER_HEIGHT; export const SelectPicker = ({ items, placeholder, setState, animationType = 'fade', disabled = false, state, height = Normalize(44), withLabel = true, variant = 'body', modalBody, customHeader, full = false, }: SelectProps) => { const [modalVisible, setModalVisible] = useState(false); const [currentState, setcurrentState] = useState(state); React.useEffect(() => { console.log(state); if (!modalVisible) setcurrentState(state); return () => { setcurrentState(null); }; }, [modalVisible, state]); const typeStyle: () => selectVariants = () => { if (disabled) return 'disabled'; else if (state) return 'filled'; else return 'unfilled'; }; const { colors } = useTheme(); return ( setModalVisible(true)} alignSelf="center" paddingLeft={'spacing-s'} paddingRight={'spacing-xms'} paddingHorizontal={'spacing-s'} borderRadius={Normalize(4)} flexDirection="row" justifyContent="space-between" alignItems="center" backgroundColor={disabled ? 'gray25' : 'white'} width="100%" minHeight={height} borderColor={ typeStyle() === 'disabled' || typeStyle() === 'unfilled' ? 'gray60' : 'gray100' } borderWidth={1} testID="SelectPicker" > {!state ? ( {placeholder} ) : null} {state && state.value && withLabel ? ( {placeholder} ) : null} {state && state.label ? ( {state.label} ) : null} setModalVisible(false)} > {customHeader ? ( customHeader(setModalVisible) ) : ( )} {modalBody ? ( modalBody(items, setModalVisible) ) : ( <> `modal-item-${index}` } scrollEnabled={true} renderItem={({ item }: any) => { const isItemChecked = currentState?.value === item.value; return ( { setcurrentState(Object.assign({}, item)); }} > {item.label} ); }} /> )} ); };