Skip to content

Commit

Permalink
docs(Dropdown): async options story (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi authored Apr 16, 2024
1 parent 31483d2 commit 7f7b63c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ The Dropdown component supports multiple options selection in two different stat

<Canvas of={DropdownStories.MultiChoiceWithDifferentStates} />

### Async Options

The Dropdown component supports async loading of options.

<Canvas of={DropdownStories.AsyncOptions} />

### Dropdown with avatar

<Canvas of={DropdownStories.DropdownWithAvatar} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ export const TipDevTipPopover = () => (
.
</Tip>
);

export const fakeFetchUsers = () => {
return new Promise(resolve => {
setTimeout(() => {
const users = [
{ id: "1", name: "Yossi Saadi" },
{ id: "2", name: "Shahar Zilberman" },
{ id: "3", name: "Tal Koren" },
{ id: "4", name: "Meirav Ron" },
{ id: "5", name: "Yael Bein" }
];

resolve({ json: () => Promise.resolve(users) });
}, 1000);
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Attach, Email } from "../../Icon/Icons";
import { Avatar, Box, Button, DialogContentContainer, Dropdown, Flex, Label, Modal, ModalContent } from "../../index";
import ModalExampleContent from "../../../storybook/patterns/dropdown-in-modals/ModalExampleContent";
import "./dropdown.stories.scss";
import { fakeFetchUsers } from "./dropdown.stories.helpers";

const metaSettings = createStoryMetaSettingsDecorator({
component: Dropdown,
Expand Down Expand Up @@ -272,6 +273,37 @@ export const MultiChoiceWithDifferentStates = {
play: multiInteractionTests
};

export const AsyncOptions = {
render: () => {
const fetchUserOptions = async () => {
try {
const response = await fakeFetchUsers();
const users = await response.json();

return users.slice(0, 5).map(user => ({
label: user.name,
value: user.id
}));
} catch (error) {
console.error("Error fetching user data:", error);
}
return [];
};

return (
<div
style={{
width: "400px"
}}
>
<Dropdown asyncOptions={fetchUserOptions} placeholder="Async options" cacheOptions defaultOptions />
</div>
);
},

name: "Async Dropdown"
};

export const DropdownWithAvatar = {
render: () => {
const optionsAvatar = useMemo(
Expand Down

0 comments on commit 7f7b63c

Please sign in to comment.