import React, {useRef} from 'react' import { StyleProp, StyleSheet, TextStyle, TouchableOpacity, TouchableWithoutFeedback, View, ViewStyle, } from 'react-native' import { FontAwesomeIcon, FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' import RootSiblings from 'react-native-root-siblings' import {Text} from './Text' import {colors} from '../../lib/styles' interface PickerItem { value: string label: string } interface PickerOpts { style?: StyleProp labelStyle?: StyleProp iconStyle?: FontAwesomeIconStyle items: PickerItem[] value: string onChange: (value: string) => void enabled?: boolean } const MENU_WIDTH = 200 export function Picker({ style, labelStyle, iconStyle, items, value, onChange, enabled, }: PickerOpts) { const ref = useRef(null) const valueLabel = items.find(item => item.value === value)?.label || value const onPress = () => { if (!enabled) { return } ref.current?.measure( ( _x: number, _y: number, width: number, height: number, pageX: number, pageY: number, ) => { createDropdownMenu(pageX, pageY + height, MENU_WIDTH, items, onChange) }, ) } return ( {valueLabel} ) } function createDropdownMenu( x: number, y: number, width: number, items: PickerItem[], onChange: (value: string) => void, ): RootSiblings { const onPressItem = (index: number) => { sibling.destroy() onChange(items[index].value) } const onOuterPress = () => sibling.destroy() const sibling = new RootSiblings( ( <> {items.map((item, index) => ( onPressItem(index)}> {item.label} ))} ), ) return sibling } const styles = StyleSheet.create({ outer: { flexDirection: 'row', alignItems: 'center', }, label: { marginRight: 5, }, icon: {}, bg: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, backgroundColor: '#000', opacity: 0.1, }, menu: { position: 'absolute', backgroundColor: '#fff', borderRadius: 14, opacity: 1, paddingVertical: 6, }, menuItem: { flexDirection: 'row', alignItems: 'center', paddingVertical: 6, paddingLeft: 15, paddingRight: 30, }, menuItemBorder: { borderTopWidth: 1, borderTopColor: colors.gray2, marginTop: 4, paddingTop: 12, }, menuItemIcon: { marginLeft: 6, marginRight: 8, }, menuItemLabel: { fontSize: 15, }, })