Dropdown Menu with Checkboxes

PreviousNext

A dropdown menu with checkboxes component

Docs
shadcnui-blockscomponent

Preview

Loading preview…
components/customized/dropdown-menu/dropdown-menu-03.tsx
"use client";

import { Button } from "@/registry/ui/button";
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/registry/ui/dropdown-menu";
import { useState } from "react";

const tags = ["Sport", "Music", "Food", "Travel", "Tech", "Science", "Art"];

export default function DropdownMenuWithCheckboxes() {
  const [selectedTags, setSelectedTags] = useState<string[]>([
    tags[0],
    tags[4],
  ]);

  const handleTagChange = (tag: string, checked: boolean) => {
    if (checked) {
      setSelectedTags([...selectedTags, tag]);
    } else {
      setSelectedTags(selectedTags.filter((t) => t !== tag));
    }
  };

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="outline">Select Tags</Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-44">
        <DropdownMenuLabel>Select Tags</DropdownMenuLabel>
        <DropdownMenuSeparator />
        {tags.map((tag) => (
          <DropdownMenuCheckboxItem
            checked={selectedTags.includes(tag)}
            key={tag}
            onCheckedChange={(checked) => handleTagChange(tag, checked)}
            // Prevent the dropdown menu from closing when the checkbox is clicked
            onSelect={(e) => e.preventDefault()}
          >
            {tag}
          </DropdownMenuCheckboxItem>
        ))}
      </DropdownMenuContent>
    </DropdownMenu>
  );
}

Installation

npx shadcn@latest add @shadcnui-blocks/dropdown-menu-03

Usage

import { DropdownMenu03 } from "@/components/dropdown-menu-03"
<DropdownMenu03 />