Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shadcn combobox disabled even thougyh i have not intentionally doing it

I am creating a custom combobox using shadcn, and passing data as prop parameter which is working, the issue persists when i am getting the loist the list is disabled andi am not able to select any value, i want to select, which is by default disabled. Can anyone help me out here

shadcn reference : https://ui.shadcn.com/docs/components/combobox


import React, { useState } from "react";
import { CaretSortIcon, CheckIcon } from "@radix-ui/react-icons";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";

interface Option {
    value: string;
    label: React.ReactNode;
}

interface Props {
    data: Option[];
    onSelect: (selectedValue: string) => void;
}

function Combobox({ data = [], onSelect }: Props) {
    const [open, setOpen] = useState(false);
    const [value, setValue] = useState("");

    const handleSelect = (selectedValue: string) => {
        setValue(selectedValue);
        setOpen(false);
        onSelect(selectedValue);
    };

    console.log("Combobox rendered", data, value, open);

    return (
        <Popover open={open} onOpenChange={setOpen}>
            <PopoverTrigger asChild>
                <Button
                    variant="outline"
                    role="combobox"
                    aria-expanded={open}
                    className="w-full justify-between"
                >
                    {value ? data.find((item) => item.value === value)?.label : "Select..."}
                    <CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
                </Button>
            </PopoverTrigger>
            <PopoverContent className="w-full p-0">
                <Command>
                    <CommandInput placeholder="Search..." className="h-9" />
                    <CommandList

                    >
                        <CommandEmpty>No item found.</CommandEmpty>
                        <CommandGroup heading="Options">
                            {data.map((item) => (
                                <CommandItem
                                    key={item.value}
                                    onSelect={() => handleSelect(item.value)}
                                    // className={cn("flex items-center", {
                                    //     "bg-primary-50": item.value === value,
                                    //     "cursor-not-allowed opacity-50": item.value === value,
                                    // })}
                                    disabled={item.value === value}
                                >
                                    {item.label}
                                    <CheckIcon
                                        className={cn(
                                            "ml-auto h-4 w-4",
                                            value === item.value ? "opacity-100" : "opacity-0"
                                        )}
                                    />
                                </CommandItem>
                            ))}
                        </CommandGroup>
                    </CommandList>
                </Command>
            </PopoverContent>
        </Popover>
    );
}

export default Combobox;


I tried removing commandList but then i was getting error undefined not iterable. I am confused, can anyone help me out here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

cmdk@1 came with bracking changes

You have two options:

  1. downgrade cmdk to 0.x version

Run

npm install cmdk@0.2
# or
pnpm add cmdk@0.2

Or

  1. replace data-[disabled]: with data-[disabled=true]: in components/ui/command.tsx
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading