Skip to content

Commit

Permalink
feat(ConfigMenu): Added actions entry value
Browse files Browse the repository at this point in the history
  • Loading branch information
HHogg committed Dec 11, 2023
1 parent 1ea0819 commit f31ace8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
60 changes: 40 additions & 20 deletions workspaces/package/src/ConfigMenu/ConfigMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export type MenuConfigEntryValue =
| MenuConfigNumber
| MenuConfigOneOf
| MenuConfigManyOf
| MenuConfigAction;
| MenuConfigAction
| MenuConfigActions;

export type MenuConfigBoolean = {
type: 'boolean';
Expand Down Expand Up @@ -66,6 +67,14 @@ export type MenuConfigAction = {
onAction: () => void;
};

export type MenuConfigActions = {
type: 'actions';
actions: {
label: string;
onAction: () => void;
}[];
};

const isBoolean = (value: MenuConfigEntryValue): value is MenuConfigBoolean =>
value.type === 'boolean';

Expand All @@ -82,6 +91,9 @@ const isManyOf = (
const isAction = (value: MenuConfigEntryValue): value is MenuConfigAction =>
value.type === 'action';

const isActions = (value: MenuConfigEntryValue): value is MenuConfigActions =>
value.type === 'actions';

const getLabel = (entry: MenuConfigEntry) => {
switch (entry.config.type) {
case 'boolean':
Expand All @@ -101,7 +113,8 @@ const getLabel = (entry: MenuConfigEntry) => {
return 'All';
}
case 'action':
return entry.label;
case 'actions':
return '';
}
};

Expand All @@ -123,25 +136,20 @@ export const ConfigMenu = ({
const activeIndex = config.findIndex((entry) => entry.label === activeKey);
const activeEntry = activeKey === __root ? undefined : config[activeIndex];

const createUpdateHandler =
(
entry: MenuConfigEntry,
value: Exclude<MenuConfigEntryValue, MenuConfigAction>['value']
) =>
() => {
if (!isAction(entry.config)) {
// Todo: Why as needed here?
(
entry.config.onChange as (
v: Exclude<MenuConfigEntryValue, MenuConfigAction>['value']
) => void
)(value);
}
const createUpdateHandler = (entry: MenuConfigEntry, value: any) => () => {
if (
isBoolean(entry.config) ||
isNumber(entry.config) ||
isOneOf(entry.config) ||
isManyOf(entry.config)
) {
(entry.config.onChange as any)(value);
}

if (!isManyOf(entry.config)) {
setActiveKey(__root);
}
};
if (!isManyOf(entry.config)) {
setActiveKey(__root);
}
};

return (
<TransitionBox
Expand Down Expand Up @@ -252,6 +260,18 @@ export const ConfigMenu = ({
</MenuItemCheckBox>
)
)}

{isActions(activeEntry.config) &&
activeEntry.config.actions.map((action) => (
<MenuItemAction
key={action.label}
onClick={() => {
action.onAction();
setActiveKey(__root);
}}
title={action.label}
/>
))}
</Menu>
)}
</TransitionBox>
Expand Down
10 changes: 6 additions & 4 deletions workspaces/package/src/ConfigMenu/MenuItemAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Text } from '../Text/Text';
import MenuItem, { MenuItemProps } from './MenuItem';

type MenuItemNavigateProps = MenuItemProps & {
Icon: LucideIcon;
Icon?: LucideIcon;
title: string;
};

Expand All @@ -15,9 +15,11 @@ export default function MenuItemAction({
}: MenuItemNavigateProps) {
return (
<MenuItem {...rest}>
<Box>
<Icon size="1rem" />
</Box>
{Icon && (
<Box>
<Icon size="1rem" />
</Box>
)}

<Box grow minWidth="120px">
<Text weight="x2">{title}</Text>
Expand Down
17 changes: 17 additions & 0 deletions workspaces/site/src/docs/catalog/ConfigMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ const Item: CatalogueItem<{
onAction: () => {},
},
},
{
label: 'Save As',
icon: SaveIcon,
config: {
type: 'actions',
actions: [
{
label: 'PNG',
onAction: () => {},
},
{
label: 'SVG',
onAction: () => {},
},
],
},
},
];

return (
Expand Down

0 comments on commit f31ace8

Please sign in to comment.