Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AsideHeader): preventUserRemoving prop for menu item and callbacks for AsideHeader #314

Merged
merged 9 commits into from
Oct 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const AllPagesListItem: React.FC<AllPagesListItemProps> = (props) => {
<Icon className={b('icon')} data={item.icon} size={item.iconSize} />
) : null}
<span className={b('text')}>{item.title}</span>
{editMode && (
{editMode && !item.preventUserRemoving && (
<Button
onClick={onPinButtonClick}
view={item.hidden ? 'flat-secondary' : 'flat-action'}
Expand Down
15 changes: 11 additions & 4 deletions src/components/AllPagesPanel/AllPagesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ interface AllPagesPanelProps {

export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
const {startEditIcon, onEditModeChanged, className} = props;
const {menuItems, onMenuItemsChanged} = useAsideHeaderInnerContext();
const {menuItems, onMenuItemsChanged, editMenuProps} = useAsideHeaderInnerContext();

const menuItemsRef = useRef(menuItems);
menuItemsRef.current = menuItems;

Expand All @@ -37,7 +38,11 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {

useEffect(() => {
onEditModeChanged?.(isEditMode);
}, [isEditMode, onEditModeChanged]);

if (isEditMode) {
editMenuProps?.onOpenEditMode?.();
}
}, [isEditMode, onEditModeChanged, editMenuProps]);

const onItemClick = useCallback((item: ListItemData<MenuItem>) => {
//@ts-ignore TODO fix when @gravity-ui/uikit/List will provide event arg on item click
Expand All @@ -54,6 +59,7 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
const originItems = menuItemsRef.current.filter(
(menuItem) => menuItem.id !== ALL_PAGES_ID,
);
editMenuProps?.onToggleMenuItem?.(changedItem);
onMenuItemsChanged(
originItems.map((menuItem) => {
if (menuItem.id !== changedItem.id) {
Expand All @@ -63,7 +69,7 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
}),
);
},
[onMenuItemsChanged],
[onMenuItemsChanged, editMenuProps],
);

const itemRender = useCallback(
Expand All @@ -81,14 +87,15 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
if (!onMenuItemsChanged) {
return;
}
editMenuProps?.onResetSettingsToDefault?.();
const originItems = menuItemsRef.current.filter((item) => item.id !== ALL_PAGES_ID);
onMenuItemsChanged(
originItems.map((item) => ({
...item,
hidden: false,
})),
);
}, [onMenuItemsChanged]);
}, [onMenuItemsChanged, editMenuProps]);
return (
<Flex className={b(null, className)} gap="5" direction="column">
<Flex gap="4" alignItems="center" justifyContent="space-between">
Expand Down
7 changes: 7 additions & 0 deletions src/components/AsideHeader/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface LayoutProps {
topAlert?: AsideHeaderTopAlertProps;
}

export interface EditMenuProps {
onOpenEditMode?: () => void;
onToggleMenuItem?: (changedItem: MenuItem) => void;
onResetSettingsToDefault?: () => void;
}

export interface AsideHeaderGeneralProps extends QAProps {
logo?: LogoProps;
multipleTooltip?: boolean;
Expand All @@ -35,6 +41,7 @@ export interface AsideHeaderGeneralProps extends QAProps {
compact: boolean;
asideRef: React.RefObject<HTMLDivElement>;
}) => React.ReactNode;
editMenuProps?: EditMenuProps;
onClosePanel?: () => void;
onChangeCompact?: (compact: boolean) => void;
openModalSubscriber?: (subscriber: OpenModalSubscriber) => void;
Expand Down
1 change: 1 addition & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface MenuItem extends QAProps {
ref: React.RefObject<HTMLElement>;
},
) => React.ReactNode;
preventUserRemoving?: boolean;
rightAdornment?: React.ReactNode;
type?: MenuItemType;
afterMoreButton?: boolean;
Expand Down
Loading