({
label,
labelClasses,
selectClasses,
options,
onSelect,
selectedValue,
placeholder = 'Select an option',
labelKey,
valueKey,
}: SelectProps)
| 57 | |
| 58 | /** Customizable Select Component :) options receive any data type and converter into label and value to render */ |
| 59 | export const Select = ({ |
| 60 | label, |
| 61 | labelClasses, |
| 62 | selectClasses, |
| 63 | options, |
| 64 | onSelect, |
| 65 | selectedValue, |
| 66 | placeholder = 'Select an option', |
| 67 | labelKey, |
| 68 | valueKey, |
| 69 | }: SelectProps) => { |
| 70 | const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 71 | const [dropdownPosition, setDropdownPosition] = |
| 72 | useState<LayoutRectangle | null>(null); |
| 73 | const selectButtonRef = useRef<TouchableOpacity>(null); |
| 74 | |
| 75 | const new_options = convertToOptions(options, labelKey, valueKey); |
| 76 | |
| 77 | const handleSelect = (value: string | number) => { |
| 78 | onSelect(value); |
| 79 | setIsDropdownOpen(false); |
| 80 | }; |
| 81 | |
| 82 | const openDropdown = () => { |
| 83 | selectButtonRef.current?.measure((_fx, _fy, _w, _h, px, py) => { |
| 84 | setDropdownPosition({ |
| 85 | x: px, |
| 86 | y: py + _h, |
| 87 | width: _w, |
| 88 | height: _h, |
| 89 | }); |
| 90 | setIsDropdownOpen(true); |
| 91 | }); |
| 92 | }; |
| 93 | |
| 94 | return ( |
| 95 | <View className={cn('flex flex-col gap-1.5')}> |
| 96 | {label && ( |
| 97 | <Text className={cn('text-base text-primary', labelClasses)}> |
| 98 | {label} |
| 99 | </Text> |
| 100 | )} |
| 101 | <TouchableOpacity |
| 102 | ref={selectButtonRef} |
| 103 | className={cn( |
| 104 | selectClasses, |
| 105 | 'border border-input py-2.5 px-4 rounded-lg bg-white dark:bg-black' |
| 106 | )} |
| 107 | onPress={openDropdown} |
| 108 | > |
| 109 | <Text className="text-primary"> |
| 110 | {selectedValue |
| 111 | ? new_options.find(option => option.value === selectedValue)?.label |
| 112 | : placeholder} |
| 113 | </Text> |
| 114 | </TouchableOpacity> |
| 115 | |
| 116 | {/* Dropdown modal */} |
nothing calls this directly
no test coverage detected