diff --git a/apps/admin-x-design-system/src/global/SortMenu.stories.tsx b/apps/admin-x-design-system/src/global/SortMenu.stories.tsx new file mode 100644 index 0000000000..b4f5a6cae2 --- /dev/null +++ b/apps/admin-x-design-system/src/global/SortMenu.stories.tsx @@ -0,0 +1,32 @@ +import type {Meta, StoryObj} from '@storybook/react'; + +import SortMenu from './SortMenu'; + +const meta = { + title: 'Global / SortMenu', + component: SortMenu, + tags: ['autodocs'] +} satisfies Meta; + +export default meta; +type Story = StoryObj; + +const items = [ + {id: 'date-added', label: 'Date added', selected: true}, + {id: 'name', label: 'Name'}, + {id: 'redemptions', label: 'Redemptions'} +]; + +export const Default: Story = { + args: { + items: items, + onSortChange: () => {}, + onDirectionChange: () => {}, + position: 'left' + }, + decorators: [ + ThisStory => ( +
+ ) + ] +}; diff --git a/apps/admin-x-design-system/src/global/SortMenu.tsx b/apps/admin-x-design-system/src/global/SortMenu.tsx new file mode 100644 index 0000000000..80b91a4c16 --- /dev/null +++ b/apps/admin-x-design-system/src/global/SortMenu.tsx @@ -0,0 +1,76 @@ +import React, {useState, useEffect} from 'react'; +import Button, {ButtonProps} from './Button'; +import Popover, {PopoverPosition} from './Popover'; +import {Icon} from '..'; + +export type SortItem = { + id: string, + label: string; + selected?: boolean; +} + +export interface SortMenuProps { + items: SortItem[]; + direction: string; + onSortChange: (selectedOption: string) => void; + onDirectionChange: (selectedDirection: string) => void; + trigger?: React.ReactNode; + triggerButtonProps?: ButtonProps; + position?: PopoverPosition; +} + +const SortMenu: React.FC = ({ + items, + direction, + onSortChange, + onDirectionChange, + trigger, + triggerButtonProps, + position = 'left' +}) => { + const [localItems, setLocalItems] = useState(items); + const [localDirection, setLocalDirection] = useState(direction || 'desc'); + + useEffect(() => { + setLocalItems(items); + }, [items]); + + const handleSortChange = (selectedValue: string) => { + const updatedItems = localItems.map(item => ({ + ...item, + selected: item.id === selectedValue ? true : false + })); + setLocalItems(updatedItems); + + onSortChange(selectedValue); + }; + + const handleSortDirection = () => { + setLocalDirection(currentDirection => (currentDirection === 'desc' ? 'asc' : 'desc')); + onDirectionChange(localDirection); + }; + + if (!trigger) { + trigger = : null} + + ))} + + + ); +}; + +export default SortMenu; diff --git a/apps/admin-x-design-system/src/index.ts b/apps/admin-x-design-system/src/index.ts index e0cbf276f9..e80192f015 100644 --- a/apps/admin-x-design-system/src/index.ts +++ b/apps/admin-x-design-system/src/index.ts @@ -108,6 +108,8 @@ export {default as Separator} from './global/Separator'; export type {SeparatorProps} from './global/Separator'; export {DragIndicator, default as SortableList} from './global/SortableList'; export type {DragIndicatorProps, SortableItemContainerProps, SortableListProps} from './global/SortableList'; +export {default as SortMenu} from './global/SortMenu'; +export type {SortMenuProps} from './global/SortMenu'; export {default as StickyFooter} from './global/StickyFooter'; export type {StickyFooterProps} from './global/StickyFooter'; export {default as TabView} from './global/TabView';