diff --git a/components/shared/SplitButton.stories.tsx b/components/shared/SplitButton.stories.tsx new file mode 100644 index 000000000..cc815aaeb --- /dev/null +++ b/components/shared/SplitButton.stories.tsx @@ -0,0 +1,40 @@ +import { action } from "@storybook/addon-actions"; +import { SplitButton } from "./SplitButton"; + +export default { + title: "Components/SplitButton", + component: SplitButton, + argTypes: { + actions: { + control: "object", + description: "Array of actions with label and onClick function", + }, + }, +}; + +const createAction = (label: string) => ({ + label, + onClick: action(`${label} clicked`), +}); + +export const Default = { + args: { + actions: [createAction("Action 1"), createAction("Action 2"), createAction("Action 3")], + }, +}; + +export const SingleOption = { + args: { + actions: [createAction("Only Option")], + }, +}; + +export const LongLabels = { + args: { + actions: [ + createAction("This is a very long action label"), + createAction("Another lengthy option here"), + createAction("Yet another extended choice"), + ], + }, +}; diff --git a/components/shared/SplitButton.tsx b/components/shared/SplitButton.tsx new file mode 100644 index 000000000..cda01c8e7 --- /dev/null +++ b/components/shared/SplitButton.tsx @@ -0,0 +1,47 @@ +import { ChevronDownIcon } from "@heroicons/react/24/outline"; +import { useState } from "react"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from "components/atoms/Dropdown/dropdown"; +import Button from "./Button/button"; + +type Action = { label: string; onClick: () => void }; +type NonEmptyArray = [T, ...T[]]; + +type SplitButtonProps = { + actions: NonEmptyArray; +}; + +export const SplitButton = ({ actions }: SplitButtonProps) => { + const [action, setAction] = useState(actions[0]); + + return ( +
+ + + + + + + {actions.map((actionItem) => ( + { + setAction(actionItem); + }} + > + {actionItem.label} + + ))} + + +
+ ); +};