Skip to content

Commit

Permalink
feat: added disabled listbox state
Browse files Browse the repository at this point in the history
  • Loading branch information
coderwelsch committed Feb 13, 2024
1 parent 306d96d commit ebff0d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/components/form-field/listbox/listbox-option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ import { Listbox } from "@headlessui/react";
import React, { Fragment } from "react";
import { ListboxBadgeOption } from "./listbox-badge-option";
import { ListboxTextOption } from "./listbox-text-option";
import { classNames } from "../../../util/class-names";

const listboxOptionStyles = {
base: "relative cursor-pointer px-3 py-2 ",
selected:
"ui-selected:bg-primary-100 ui-selected:text-primary-500 ui-selected:before:absolute ui-selected:before:bottom-0 ui-selected:before:left-0 ui-selected:before:top-0 ui-selected:before:block ui-selected:before:w-[2px] ui-selected:before:rounded-r-md ui-selected:before:bg-primary-400",
active: "ui-active:bg-neutral-50 ui-active:ui-selected:bg-primary-100",
disabled:
"ui-disabled:cursor-not-allowed ui-disabled:bg-neutral-50 ui-disabled:text-neutral-400",
};

export interface ListboxOptionProps<TValue> {
value: TValue;
children: React.ReactNode;
disabled?: boolean;
}

const ListboxOption = <TValue,>({ value, children }: ListboxOptionProps<TValue>) => {
const ListboxOption = <TValue,>({ value, disabled, children }: ListboxOptionProps<TValue>) => {
return (
<Listbox.Option value={value} as={Fragment}>
<li className="relative cursor-pointer px-3 py-2 ui-selected:bg-primary-100 ui-selected:text-primary-500 ui-selected:before:absolute ui-selected:before:bottom-0 ui-selected:before:left-0 ui-selected:before:top-0 ui-selected:before:block ui-selected:before:w-[2px] ui-selected:before:rounded-r-md ui-selected:before:bg-primary-400 ui-active:bg-neutral-50 ui-active:ui-selected:bg-primary-100">
<Listbox.Option value={value} as={Fragment} disabled={disabled}>
<li
className={classNames(
listboxOptionStyles.base,
listboxOptionStyles.selected,
listboxOptionStyles.active,
listboxOptionStyles.disabled
)}
>
{children}
</li>
</Listbox.Option>
Expand Down
9 changes: 7 additions & 2 deletions src/components/form-field/listbox/listbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ type Story = StoryObj<typeof FormField.Listbox>;
interface Person {
id: number;
name: string;
isDead?: boolean;
}

const people: Person[] = [
{ id: 1, name: "Durward Reynolds" },
{ id: 1, name: "John Lennon", isDead: true },
{ id: 2, name: "Kenton Towne" },
{ id: 3, name: "Therese Wunsch" },
{ id: 4, name: "Benedict Kessler" },
Expand All @@ -43,7 +44,11 @@ const ListboxTextWithHooks = () => {
</FormField.Listbox.Button>
<FormField.Listbox.Options>
{people.map((person) => (
<FormField.Listbox.Option value={person} key={person.id}>
<FormField.Listbox.Option
value={person}
key={person.id}
disabled={person.isDead}
>
<FormField.Listbox.Option.TextOption>
{person.name}
</FormField.Listbox.Option.TextOption>
Expand Down

0 comments on commit ebff0d0

Please sign in to comment.