-
Hello there👋 Here's a simplified version of my code: const { setRoom } = useRoom();
const handleRoomChange = (newRoom: Room) => {
console.log('Selected Room:', newRoom);
setRoom(newRoom);
};
return (
<Select.Root>
<Select.Trigger>
<Select.Icon>🌵</Select.Icon>
<Select.Value placeholder='Select a room...' />
</Select.Trigger>
<Select.Content>
<Select.Viewport>
{/* Rendering Select.Groups and Select.Items based on available rooms */}
{rooms.map((room, k) => (
<Select.Item
key={room.id}
value={uuidv4()}
onPointerDown={(event) => event.preventDefault()}
onClick={() => handleRoomChange(room)}
>
<Select.ItemText>{room.roomName}</Select.ItemText>
</Select.Item>
))}
</Select.Viewport>
</Select.Content>
</Select.Root>
); The issue is that the I'm considering whether I should explore creating a custom Select component using the Radix UI Select API to better suit my requirements. Any insights or recommendations on handling complex objects within Radix UI Select would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
@Jauki the radix select only allows string values because it applies your values to the native const roomsMap = React.useMemo(() => new Map(rooms.map(room => [room.id, room])), [rooms]);
const { room, setRoom } = useRoom();
return (
<Select.Root value={room.id} onValueChange={id => setRoom(roomsMap.get(id))}>
<Select.Trigger>
<Select.Icon>🌵</Select.Icon>
<Select.Value placeholder='Select a room...' />
</Select.Trigger>
<Select.Content>
<Select.Viewport>
{rooms.map((room, k) => (
<Select.Item key={room.id} value={room.id}>
<Select.ItemText>{room.roomName}</Select.ItemText>
</Select.Item>
))}
</Select.Viewport>
</Select.Content>
</Select.Root>
); |
Beta Was this translation helpful? Give feedback.
@Jauki the radix select only allows string values because it applies your values to the native
<select>
options for a11y, and HTML can only have string values. instead it provides anonValueChange
api so that you can derive the value: