Skip to content

Convert DropdownMenu to use CSS #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions src/app/routes/order-form/OrderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
} from '@/components/ui/command';
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import {
Expand Down Expand Up @@ -44,6 +46,8 @@ import { inputOrderSchema } from '@/types';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';

const SELECT_PRODUCT_PLACEHOLDER = 'Select a product';

export interface OrderFormProps {
existingOrder?: InputOrder;
products: Product[];
Expand Down Expand Up @@ -72,9 +76,9 @@ export function OrderForm({
`${product.manufacturer} - ${product.name}`;

const getProductIdDisplayText = (productId: string | undefined) => {
if (productId === undefined) return 'Select product';
if (productId === undefined) return undefined;
const product = products.find((product) => product.id === productId);
return product ? getProductDisplayText(product) : productId;
return product ? getProductDisplayText(product) : undefined;
};

return (
Expand All @@ -93,7 +97,7 @@ export function OrderForm({
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a product" />
<SelectValue placeholder={SELECT_PRODUCT_PLACEHOLDER} />
</SelectTrigger>
</FormControl>
<SelectContent>
Expand All @@ -109,7 +113,7 @@ export function OrderForm({
)}
/>

{/* ---------- Dropdown Menu ---------- */}
{/* ---------- Dropdown Menu with DropdownMenuItem ---------- */}
<FormField
control={form.control}
name="product2Id"
Expand All @@ -119,21 +123,24 @@ export function OrderForm({
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button className="inline-flex w-fit" variant="outline">
{getProductIdDisplayText(field.value)}
{getProductIdDisplayText(field.value) ?? (
<span className="text-muted-foreground">
{SELECT_PRODUCT_PLACEHOLDER}
</span>
)}
<Icons.chevronDown className="text-muted-foreground" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuContent align="start">
{products.map((product) => (
<DropdownMenuCheckboxItem
checked={product.id === field.value}
<DropdownMenuItem
key={product.id}
onClick={() => {
field.onChange(product.id);
}}
>
{getProductDisplayText(product)}
</DropdownMenuCheckboxItem>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
Expand All @@ -142,16 +149,55 @@ export function OrderForm({
)}
/>

{/* ---------- Dropdown Menu ---------- */}
{/* ---------- Dropdown Menu with DropdownMenuRadioItem ---------- */}
<FormField
control={form.control}
name="product3Id"
render={({ field }) => (
<FormItem className="flex flex-col items-start">
<FormLabel>Product 3</FormLabel>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button className="inline-flex w-fit" variant="outline">
{getProductIdDisplayText(field.value) ?? (
<span className="text-muted-foreground">
{SELECT_PRODUCT_PLACEHOLDER}
</span>
)}
<Icons.chevronDown className="text-muted-foreground" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
<DropdownMenuRadioGroup
onValueChange={field.onChange}
value={field.value}
>
{products.map((product) => (
<DropdownMenuRadioItem
key={product.id}
value={product.id}
>
{getProductDisplayText(product)}
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
<FormMessage />
</FormItem>
)}
/>

{/* ---------- Dropdown Menu ---------- */}
<FormField
control={form.control}
name="product4Id"
render={({ field }) => (
<FormItem className="flex flex-col items-start">
<FormLabel>Product 4</FormLabel>
<TreeSelect
onChange={field.onChange}
placeholder="Select a product"
placeholder={SELECT_PRODUCT_PLACEHOLDER}
rootNode={productTree}
value={field.value}
/>
Expand All @@ -163,15 +209,19 @@ export function OrderForm({
{/* ---------- Command Menu ---------- */}
<FormField
control={form.control}
name="product4Id"
name="product5Id"
render={({ field }) => (
<FormItem className="flex flex-col items-start">
<FormLabel>Product 4</FormLabel>
<FormLabel>Product 5</FormLabel>
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button className="inline-flex w-fit" variant="outline">
{getProductIdDisplayText(field.value)}
{getProductIdDisplayText(field.value) ?? (
<span className="text-muted-foreground">
{SELECT_PRODUCT_PLACEHOLDER}
</span>
)}
<Icons.chevronDown className="text-muted-foreground" />
</Button>
</FormControl>
Expand All @@ -192,7 +242,7 @@ export function OrderForm({
<CommandItem
key={product.id}
onSelect={() => {
form.setValue('product4Id', product.id);
form.setValue('product5Id', product.id);
}}
value={getProductDisplayText(product)}
>
Expand Down
4 changes: 2 additions & 2 deletions src/app/routes/order-form/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import { OrderForm } from './OrderForm';
import { ObjectViewer } from '@/components/ObjectViewer';
import { inputOrder1, productTree, products } from '@/data';
import { newOrder, productTree, products } from '@/data';
import type { InputOrder } from '@/types';
import { useState } from 'react';

export function OrderFormPage() {
const [inputOrder, setInputOrder] = useState<InputOrder | undefined>(
inputOrder1,
newOrder,
);

function handleSubmit(newInputOrder: InputOrder) {
Expand Down
11 changes: 7 additions & 4 deletions src/components/TreeSelect/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export function TreeSelect({

// Helper function to get display text for a node
const getNodeDisplayText = (nodeId: string | undefined) => {
if (nodeId === undefined) return placeholder;
return nodeMap.get(nodeId)?.name ?? nodeId;
if (nodeId === undefined) return undefined;
const node = nodeMap.get(nodeId);
return node ? node.name : undefined;
};

// Recursive function to render nested menu items
Expand Down Expand Up @@ -92,11 +93,13 @@ export function TreeSelect({
className={cn('inline-flex w-fit', className)}
variant="outline"
>
{getNodeDisplayText(value)}
{getNodeDisplayText(value) ?? (
<span className="text-muted-foreground">{placeholder}</span>
)}
<Icons.chevronDown className="text-muted-foreground" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuContent align="start">
{renderTreeNodes(rootNode.children)}
</DropdownMenuContent>
</DropdownMenu>
Expand Down
Loading