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
19 changes: 16 additions & 3 deletions src/components/AllPagesPanel/AllPagesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@

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

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

Expand All @@ -37,7 +44,11 @@

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

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

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 +65,7 @@
const originItems = menuItemsRef.current.filter(
(menuItem) => menuItem.id !== ALL_PAGES_ID,
);
onToggleMenuItem?.(changedItem);
onMenuItemsChanged(
originItems.map((menuItem) => {
if (menuItem.id !== changedItem.id) {
Expand All @@ -63,7 +75,7 @@
}),
);
},
[onMenuItemsChanged],
[onMenuItemsChanged, onToggleMenuItem],
);

const itemRender = useCallback(
Expand All @@ -81,6 +93,7 @@
if (!onMenuItemsChanged) {
return;
}
onResetSettingsToDefault?.();
const originItems = menuItemsRef.current.filter((item) => item.id !== ALL_PAGES_ID);
onMenuItemsChanged(
originItems.map((item) => ({
Expand All @@ -88,7 +101,7 @@
hidden: false,
})),
);
}, [onMenuItemsChanged]);

Check warning on line 104 in src/components/AllPagesPanel/AllPagesPanel.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

React Hook useCallback has a missing dependency: 'onResetSettingsToDefault'. Either include it or remove the dependency array
return (
<Flex className={b(null, className)} gap="5" direction="column">
<Flex gap="4" alignItems="center" justifyContent="space-between">
Expand Down
3 changes: 3 additions & 0 deletions src/components/AsideHeader/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface AsideHeaderGeneralProps extends QAProps {
}) => React.ReactNode;
onClosePanel?: () => void;
onChangeCompact?: (compact: boolean) => void;
onOpenEditMode?: () => void;
jenshenJ marked this conversation as resolved.
Show resolved Hide resolved
onToggleMenuItem?: (changedItem: MenuItem) => void;
onResetSettingsToDefault?: () => 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