ui: [bug]: CommandInput of Combobox inside a Dialog not working

Describe the bug

CommandInput field of a Combobox placed inside a Dialog is not working (cannot type anything in the input field).

Affected component/components

Combobox

How to reproduce

import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { zodResolver } from "@hookform/resolvers/zod";
import { Check, ChevronsUpDown } from "lucide-react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { useState } from "react";

const languages = [
  { label: "English", value: "en" },
  { label: "French", value: "fr" },
  { label: "German", value: "de" },
  { label: "Spanish", value: "es" },
  { label: "Portuguese", value: "pt" },
  { label: "Russian", value: "ru" },
  { label: "Japanese", value: "ja" },
  { label: "Korean", value: "ko" },
  { label: "Chinese", value: "zh" },
] as const;

const FormSchema = z.object({
  language: z.string({
    required_error: "Please select a language.",
  }),
});

export default function TestModal() {
  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
  });

  const [open, setOpen] = useState(false);

  function onSubmit(data: z.infer<typeof FormSchema>) {
    console.log(data);
  }

  return (
    <Dialog>
      <DialogTrigger>Open</DialogTrigger>

      <DialogContent>
        <DialogHeader>
          <DialogTitle>Test</DialogTitle>
        </DialogHeader>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
            <FormField
              control={form.control}
              name="language"
              render={({ field }) => (
                <FormItem className="flex flex-col">
                  <FormLabel>Language</FormLabel>
                  <Popover open={open} onOpenChange={setOpen} modal={true}>
                    <PopoverTrigger asChild>
                      <FormControl>
                        <Button
                          variant="outline"
                          role="combobox"
                          aria-expanded={open}
                          className={cn(
                            "w-[200px] justify-between",
                            !field.value && "text-muted-foreground",
                          )}
                        >
                          {field.value
                            ? languages.find(
                                (language) => language.value === field.value,
                              )?.label
                            : "Select language"}
                          <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
                        </Button>
                      </FormControl>
                    </PopoverTrigger>
                    <PopoverContent className="w-[200px] p-0">
                      <Command>
                        {/* Here not working */}
                        <CommandInput placeholder="Search language..." />
                        <CommandList>
                          <CommandEmpty>No language found.</CommandEmpty>
                          <CommandGroup>
                            {languages.map((language) => (
                              <CommandItem
                                value={language.label}
                                key={language.value}
                                onSelect={() => {
                                  form.setValue("language", language.value);
                                  setOpen(false);
                                }}
                              >
                                <Check
                                  className={cn(
                                    "mr-2 h-4 w-4",
                                    language.value === field.value
                                      ? "opacity-100"
                                      : "opacity-0",
                                  )}
                                />
                                {language.label}
                              </CommandItem>
                            ))}
                          </CommandGroup>
                        </CommandList>
                      </Command>
                    </PopoverContent>
                  </Popover>
                  <FormDescription>
                    This is the language that will be used in the dashboard.
                  </FormDescription>
                  <FormMessage />
                </FormItem>
              )}
            />
            <Button type="submit">Submit</Button>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}

Codesandbox/StackBlitz link

No response

Logs

No response

System Info

No relevant info

Before submitting

  • I’ve made research efforts and searched the documentation
  • I’ve searched for existing issues

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 21

Most upvoted comments

I encountered multiple issues related to this bug:

  • Controls inside the popover became (partially) unusable as described here, a click on any control would dismiss the popover or nothing would happen.
  • Pressing the Escape key immediately closed the dialog, instead of dismissing the popover first and then the dialog on a second press.
  • The page became unresponsive after closing the dialog (#1912).

For me, all these problems originated from the same underlying issue: a version mismatch of the @radix-ui/react-dismissable-layer package in my project.

To resolve the issue, I:

  • Updated all @radix-ui dependencies to their latest versions.
  • Updated additional dependencies relying on DismissableLayer indirectly via @radix-ui/react-dialog, such as cmdk (for combobox) and vaul (for drawer).
  • Removed package-lock.json and the node_modules folder.
  • Ran npm install to reinstall all dependencies.

After these steps, the issues were resolved. Any previous changes/workarounds (such as adding modal={false}) were not needed anymore.

Still getting this even with

“@radix-ui/react-popover”: “^1.1.1”, “cmdk”: “^1.0.0”,

pass modal={false} to the Popover AND the Dialog elements

Still getting this even with

“@radix-ui/react-popover”: “^1.1.1”, “cmdk”: “^1.0.0”,

As @platysma explains. There are multiple issues that can be solved by updating the dependencies. It can however be a little confusing how to do the update shadcn-ui and all it’s dependencies from Radix etc.

Remove all modal={true/false} from your dialog and popover (or whereever you have tried to put it in desperation…😃)

Update Shadcn-ui components by running: npx shadcn-ui@latest add Select the components that you are using in your solution (The filenames in your ‘components/ui’ folder will give you a clue which you have installed). you will be prompted to update each of the components as the command runs. Be aware that if you have customized any of the components you would have to do so again.

Delete your node_modules folder and the package-lock.json file

run npm install or whatever package manager you are using to reinstall all dependencies.

Make sure that you restart your project/dev server.

I have similar setup of a dialog with a form where one of the inputs are a combobox, and was facing all above issues.

ı passed modal={false} to the Popover and Dialog elements and CommandInput worked.but now dialog’s backgrounds removed.

Experienced this issue. modal={false} in <Dialog /> fixes the problem, but as a previous comment points out this removes the background of the dialog which is not a satisfactory outcome.

Simply upgrading:

"@radix-ui/react-popover": "^1.1.4"
"@radix-ui/react-dialog": "^1.1.4"
"cmdk": "1.0.4"

did the trick for me without having to mess with pointer events, z-indexes, or setting modal={false}.

Updating from:

"@radix-ui/react-popover": "^1.0.7",
"cmdk": "^0.2.0",

to:

"@radix-ui/react-popover": "^1.1.1",
"cmdk": "^1.0.0",

solved the issue for me.

@francescotau You need to use CommandDialog instead of Popover. Check it out here: https://ui.shadcn.com/docs/components/command

Nope, this is not the same thing. I have a Dialog that contains a Form consisting of multiple fields and one field of the Form should be the Combobox.

For me, I tried the following changes:

replace the DialogPortal wrapper from dialog.tsx by a div, and now is working well.

ı passed modal={false} to the Popover and Dialog elements and CommandInput worked.but now dialog’s backgrounds removed.

Same issue! Any update regarding a solution someone?