Skip to content

Commit

Permalink
Controlled combobox data (#232)
Browse files Browse the repository at this point in the history
* feat: add controllable state functionality to combobox items

* chore: add note about possible early thrown error when using controlled state

* Revert "feat: add controllable state functionality to combobox items"

This reverts commit b30921a.

* fix(combobox): fix missing prop assertion logic

* fix(combobox): fix missing prop assertion logic

* feature(combobox,stories): add custom content story

* ci(changesets): add combobox changesets

---------

Co-authored-by: hobbescodes <87732294+hobbescodes@users.noreply.github.com>
  • Loading branch information
coopbri and hobbescodes authored Feb 1, 2024
1 parent b02ef25 commit a2f415a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-tools-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@animareflection/ui": patch
---

Make `Combobox` `groups` prop optional
5 changes: 5 additions & 0 deletions .changeset/quick-chefs-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@animareflection/ui": patch
---

Throw error if `Combobox` is missing either `groups` or `content` props
24 changes: 23 additions & 1 deletion src/components/core/Combobox/Combobox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { comboboxState } from "./Combobox.spec";
import { Combobox, Image } from "components/core";
import { Combobox, Image, Text } from "components/core";
import { Flex } from "generated/panda/jsx";

import type { Meta, StoryObj } from "@storybook/react";

Expand Down Expand Up @@ -84,6 +85,27 @@ export const ImageIcons: Story = {
},
};

/**
* Custom content can be passed to override the default content render.
*/
export const CustomContent: Story = {
args: {
...Default.args,
content: (
<Flex direction="column">
<Text>All the fruit!</Text>

{fruitBasket.map(({ label, icon }) => (
<Flex key={label} align="center">
<Text>{icon}</Text>
<Text>{label}</Text>
</Flex>
))}
</Flex>
),
},
};

export const ComboboxState: Story = {
args: {
...Default.args,
Expand Down
6 changes: 5 additions & 1 deletion src/components/core/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export interface Props
};
/** Custom content render override. */
content?: ReactNode;
groups: Group[];
/** Groups of items to display. */
groups?: Group[];
itemIndicator?: boolean;
triggerEnabled?: boolean;
inputProps?: Omit<InputProps, "size">;
Expand All @@ -89,6 +90,9 @@ const Combobox = ({
const allItems = groups.flatMap((group) => group.items);
const [filteredItems, setFilteredItems] = useState(allItems);

if (!content && !groups.length)
throw new Error("Either the `content` or `groups` prop must be provided.");

const handleChange = (
evt: Parameters<
NonNullable<PrimitiveComboboxProps["onInputValueChange"]>
Expand Down

0 comments on commit a2f415a

Please sign in to comment.