From e33477677157efa54b85b110766ce9d47e114f4f Mon Sep 17 00:00:00 2001 From: Andrew Bierman <94939237+andrew-bierman@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:47:24 -0500 Subject: [PATCH] quick fix on dropdowns --- client/components/pack/AddPack.tsx | 2 - client/components/pack/PackContainer.tsx | 2 + packages/ui/src/RSelect/index.tsx | 95 +++++++++++++++--------- 3 files changed, 60 insertions(+), 39 deletions(-) diff --git a/client/components/pack/AddPack.tsx b/client/components/pack/AddPack.tsx index a1d5f6cfb..9f222e512 100644 --- a/client/components/pack/AddPack.tsx +++ b/client/components/pack/AddPack.tsx @@ -9,9 +9,7 @@ import useTheme from '../../hooks/useTheme'; import useCustomStyles from '~/hooks/useCustomStyles'; import { useAddNewPack } from '~/hooks/packs'; import { useRouter } from 'expo-router'; -import { Box, Button, CheckIcon, Input, Select, Text } from 'native-base'; import { packSelectOptions } from '~/constants/options'; - export const AddPack = ({ isCreatingTrip = false }) => { //Hooks diff --git a/client/components/pack/PackContainer.tsx b/client/components/pack/PackContainer.tsx index 29a4e2473..2025b6aa5 100644 --- a/client/components/pack/PackContainer.tsx +++ b/client/components/pack/PackContainer.tsx @@ -74,6 +74,8 @@ export default function PackContainer({ isCreatingTrip = false }) { - ) + + ); } -const extractOptionAttributes = (item, index) => { - if (typeof item !== 'object' || item === null) return { text: item, value: item, index }; - return { - text: item.name, - value: item.id || item._id || item.name, - index, +// Function to extract option attributes from data items +const extractOptionAttributes = (item, index, textKey, valueKey) => { + // Handle simple types: strings, numbers, and booleans + if ( + typeof item === 'string' || + typeof item === 'number' || + typeof item === 'boolean' + ) { + return { text: item.toString(), value: item.toString(), index }; } -} + // Handle objects + if (typeof item === 'object' && item !== null) { + return { + text: item[textKey] ?? item.toString(), + value: item[valueKey] ?? item.toString(), + index, + }; + } + + // Default case for other invalid types + return { text: 'Invalid Item', value: 'invalid', index }; +}; + +// Component to render the Select dropdown export function SelectItem(props) { const { - value, + value = '', onValueChange, - data, - placeholder, + data = [], + placeholder = 'Select an option', + textKey = 'label', + valueKey = 'value', ...forwardedProps } = props; - const handleChange = (newValue) => onValueChange && onValueChange(newValue); + const handleChange = (newValue) => { + if (typeof onValueChange === 'function') { + onValueChange(newValue); + } + }; const options = useMemo(() => { - if (!data) return null; + if (!Array.isArray(data)) return []; return data - .map(extractOptionAttributes) + .map((item, index) => + extractOptionAttributes(item, index, textKey, valueKey), + ) .map(({ text, value, index }) => ( - - {text.charAt(0).toUpperCase() + text.slice(1)} + + + {text.charAt(0).toUpperCase() + text.slice(1)} + - )) - }, [data]) + )); + }, [data, textKey, valueKey]); + + // Conditional rendering based on options + if (options.length === 0) { + return
No options available
; + } return ( - ) + ); }