Skip to content

Commit

Permalink
Support separators in Select and Dropdown (#992)
Browse files Browse the repository at this point in the history
* Support separators in Select and Dropdown

* fix breadcrumbs

* colors
  • Loading branch information
dani-moreno authored Dec 19, 2024
1 parent 5e79a29 commit 83616fa
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/experimental/Forms/Fields/Select/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const WithCustomTrigger: Story = {
options: [
{ value: "red", label: "Red" },
{ value: "green", label: "Green" },
"separator",
{ value: "blue", label: "Blue" },
],
},
Expand Down
24 changes: 17 additions & 7 deletions lib/experimental/Forms/Fields/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import {
SelectContent,
SelectItem as SelectItemPrimitive,
Select as SelectPrimitive,
SelectSeparator,
SelectTrigger,
SelectValue as SelectValuePrimitive,
} from "@/ui/select"
import { forwardRef } from "react"

type SelectItemProps<T> = {
type SelectItemObject<T> = {
value: T
label: string
icon?: IconType
description?: string
avatar?: AvatarVariant
}

type SelectItemProps<T> = SelectItemObject<T> | "separator"

type SelectProps<T> = {
placeholder: string
onChange: (value: T) => void
Expand All @@ -33,7 +36,7 @@ type SelectProps<T> = {
onOpenChange?: (open: boolean) => void
}

const SelectItem = ({ item }: { item: SelectItemProps<string> }) => {
const SelectItem = ({ item }: { item: SelectItemObject<string> }) => {
return (
<SelectItemPrimitive value={item.value}>
<div className="flex items-start gap-1.5">
Expand All @@ -56,7 +59,7 @@ const SelectItem = ({ item }: { item: SelectItemProps<string> }) => {
)
}

const SelectValue = ({ item }: { item: SelectItemProps<string> }) => {
const SelectValue = ({ item }: { item: SelectItemObject<string> }) => {
return (
<div className="flex items-center gap-1.5">
{item.icon && (
Expand Down Expand Up @@ -87,7 +90,10 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps<string>>(
},
ref
) {
const selectedOption = options.find((option) => option.value === value)
const selectedOption = options.find(
(option): option is Exclude<typeof option, "separator"> =>
option !== "separator" && option.value === value
)

return (
<SelectPrimitive
Expand Down Expand Up @@ -118,9 +124,13 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps<string>>(
)}
</SelectTrigger>
<SelectContent>
{options.map((option) => (
<SelectItem key={option.value} item={option} />
))}
{options.map((option, index) =>
option === "separator" ? (
<SelectSeparator key={`separator-${index}`} />
) : (
<SelectItem key={option.value} item={option} />
)
)}
</SelectContent>
</SelectPrimitive>
)
Expand Down
1 change: 1 addition & 0 deletions lib/experimental/Navigation/Dropdown/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const Default: Story = {
icon: Icons.Save,
description: "Preserve changes",
},
"separator",
{
label: "Delete",
onClick: () => console.log("Delete clicked"),
Expand Down
29 changes: 18 additions & 11 deletions lib/experimental/Navigation/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/ui/dropdown-menu"
import { NavigationItem } from "../utils"

export type DropdownItem = NavigationItem & {
export type DropdownItemObject = NavigationItem & {
onClick?: () => void
icon?: IconType
description?: string
critical?: boolean
avatar?: AvatarVariant
}

export type DropdownItem = DropdownItemObject | "separator"

type DropdownProps = {
items: DropdownItem[]
children?: React.ReactNode
}

const DropdownItem = ({ item }: { item: DropdownItem }) => {
const DropdownItem = ({ item }: { item: DropdownItemObject }) => {
const { label, ...props } = item

const content = (
Expand Down Expand Up @@ -101,15 +104,19 @@ export function Dropdown({ items, children }: DropdownProps) {
</DropdownMenuTrigger>
<DropdownMenuContent className="min-w-[var(--radix-dropdown-menu-trigger-width)]">
<div className="flex flex-col">
{items.map((item, index) => (
<DropdownItem
key={index}
item={{
...item,
onClick: item.onClick,
}}
/>
))}
{items.map((item, index) =>
item === "separator" ? (
<DropdownMenuSeparator key={`separator-${index}`} />
) : (
<DropdownItem
key={index}
item={{
...item,
onClick: item.onClick,
}}
/>
)
)}
</div>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
7 changes: 5 additions & 2 deletions lib/experimental/Navigation/Header/Breadcrumbs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {

import { ModuleAvatar } from "@/experimental/Information/ModuleAvatar"

import { Dropdown, type DropdownItem } from "@/experimental/Navigation/Dropdown"
import {
Dropdown,
type DropdownItemObject,
} from "@/experimental/Navigation/Dropdown"

import { ChevronRight } from "@/icons/app"
import { Link } from "@/lib/linkHandler"
Expand All @@ -28,7 +31,7 @@ interface BreadcrumbItemProps {
isLast: boolean
}

type DropdownItemWithoutIcon = Omit<DropdownItem, "icon">
type DropdownItemWithoutIcon = Omit<DropdownItemObject, "icon">

function BreadcrumbItem({ item, isLast }: BreadcrumbItemProps) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const DropdownMenuSeparator = React.forwardRef<
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.Separator
ref={ref}
className={cn("-mx-1 my-1 h-px bg-f1-background-secondary", className)}
className={cn("-mx-1 my-1 h-px bg-f1-border-secondary", className)}
{...props}
/>
))
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const SelectSeparator = React.forwardRef<
>(({ className, ...props }, ref) => (
<SelectPrimitive.Separator
ref={ref}
className={cn("-mx-1 my-1 h-px bg-f1-background-secondary", className)}
className={cn("-mx-1 my-1 h-px bg-f1-border-secondary", className)}
{...props}
/>
))
Expand Down

0 comments on commit 83616fa

Please sign in to comment.