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(AsideHeadser): Sort elements in AsideHeader #337

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ uiKitConfigure({

const withContextProvider: Decorator = (Story, context) => {
return (
<React.StrictMode>
<ThemeProvider theme={context.globals.theme}>
<MobileProvider>
<Story {...context} />
</MobileProvider>
</ThemeProvider>
</React.StrictMode>
<ThemeProvider theme={context.globals.theme}>
<MobileProvider>
<Story {...context} />
</MobileProvider>
</ThemeProvider>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
&__icon {
color: var(--g-color-text-misc);
}

&_edit-mode {
padding: 0 0 0 var(--g-spacing-4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ const b = block('all-pages-list-item');
interface AllPagesListItemProps {
item: MenuItem;
editMode?: boolean;
enableSorting?: boolean;
onToggle: () => void;
onDragStart?: () => void;
onDragEnd?: () => void;
}

export const AllPagesListItem: React.FC<AllPagesListItemProps> = (props) => {
const {item, editMode, onToggle} = props;
const {item, editMode, onToggle, enableSorting, onDragStart, onDragEnd} = props;
const onPinButtonClick = useCallback(
(e: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => {
e.stopPropagation();
Expand All @@ -33,8 +36,32 @@ export const AllPagesListItem: React.FC<AllPagesListItemProps> = (props) => {
e.preventDefault();
}
};

const onItemDrugStart = (e: MouseEvent<HTMLDivElement>) => {
if (editMode && onDragStart) {
e.stopPropagation();
e.preventDefault();
onDragStart();
}
};

const onItemDrugEnd = (e: MouseEvent<HTMLDivElement>) => {
if (editMode && onDragEnd) {
e.stopPropagation();
e.preventDefault();
onDragEnd();
}
};

return (
<div className={b()} onClick={onItemClick}>
<div
key={item.id}
className={b({'edit-mode': editMode && enableSorting})}
onClick={onItemClick}
draggable={editMode}
onMouseDown={onItemDrugStart}
onMouseUp={onItemDrugEnd}
>
{item.icon ? (
<Icon className={b('icon')} data={item.icon} size={item.iconSize} />
) : null}
Expand Down
11 changes: 11 additions & 0 deletions src/components/AllPagesPanel/AllPagesPanel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
&__discoverable-feature-wrapper {
display: flex;
}

&__item_editMode {
padding: 0 var(--g-spacing-6);
}

&__drag-placeholder {
padding-left: 88px;
padding-right: 68px;
text-wrap: nowrap;
visibility: hidden;
}
}
108 changes: 83 additions & 25 deletions src/components/AllPagesPanel/AllPagesPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useEffect, useRef, useState} from 'react';
import React, {ReactNode, useCallback, useEffect, useMemo, useRef, useState} from 'react';

import {Gear} from '@gravity-ui/icons';
import {Button, Flex, Icon, List, ListItemData, Text} from '@gravity-ui/uikit';
Expand Down Expand Up @@ -30,6 +30,9 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
menuItemsRef.current = menuItems;

const [isEditMode, setIsEditMode] = useState(false);

const [dragingItemTitle, setDragingItemTitle] = useState<ReactNode | null>(null);

const toggleEditMode = useCallback(() => {
setIsEditMode((prev) => !prev);
}, []);
Expand Down Expand Up @@ -72,15 +75,28 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
[onMenuItemsChanged, editMenuProps],
);

const onDragEnd = useCallback(() => {
setDragingItemTitle(null);
}, [setDragingItemTitle]);

const itemRender = useCallback(
(item: ListItemData<MenuItem>, _isActive: boolean, _itemIndex: number) => (
<AllPagesListItem
item={item}
editMode={isEditMode}
onToggle={() => togglePageVisibility(item)}
/>
),
[isEditMode, togglePageVisibility],
(item: ListItemData<MenuItem>, _isActive: boolean, _itemIndex: number) => {
const onDragStart = () => {
setDragingItemTitle(item.title);
};

return (
<AllPagesListItem
item={item}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
editMode={isEditMode}
onToggle={() => togglePageVisibility(item)}
enableSorting={editMenuProps?.enableSorting}
/>
);
},
[isEditMode, togglePageVisibility, onDragEnd, setDragingItemTitle, editMenuProps],
);

const onResetToDefaultClick = useCallback(() => {
Expand All @@ -96,6 +112,28 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
})),
);
}, [onMenuItemsChanged, editMenuProps]);

const changeItemsOrder = useCallback(
({oldIndex, newIndex}: {oldIndex: number; newIndex: number}) => {
const newItems = menuItemsRef.current.filter((item) => item.id !== ALL_PAGES_ID);

const element = newItems.splice(oldIndex, 1)[0];
newItems.splice(newIndex, 0, element);

onMenuItemsChanged?.(newItems);

setDragingItemTitle(null);
editMenuProps?.onChangeItemsOrder?.(element, oldIndex, newIndex);
},
[onMenuItemsChanged, editMenuProps],
);

const sortableItems = useMemo(() => {
return menuItemsRef.current.filter(
(item) => item.id !== ALL_PAGES_ID && !item.afterMoreButton,
);
}, []);

return (
<Flex className={b(null, className)} gap="5" direction="column">
<Flex gap="4" alignItems="center" justifyContent="space-between">
Expand All @@ -107,22 +145,42 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
</Button>
</Flex>
<Flex className={b('content')} gap="5" direction="column">
{Object.keys(groupedItems).map((category) => {
return (
<Flex key={category} direction="column" gap="3">
<Text className={b('category')} variant="body-1" color="secondary">
{category}
</Text>
<List
virtualized={false}
filterable={false}
items={groupedItems[category]}
onItemClick={onItemClick}
renderItem={itemRender}
/>
</Flex>
);
})}
{isEditMode && editMenuProps?.enableSorting ? (
<div>
<List
itemClassName={b('item', {editMode: true})}
itemHeight={40}
onSortEnd={changeItemsOrder}
sortable
virtualized={false}
filterable={false}
items={sortableItems}
onItemClick={onItemClick}
renderItem={itemRender}
/>

{dragingItemTitle && (
<div className={b('drag-placeholder')}>{dragingItemTitle}</div>
)}
</div>
) : (
Object.keys(groupedItems).map((category) => {
return (
<Flex key={category} direction="column" gap="3">
<Text className={b('category')} variant="body-1" color="secondary">
{category}
</Text>
<List
virtualized={false}
filterable={false}
items={groupedItems[category]}
onItemClick={onItemClick}
renderItem={itemRender}
/>
</Flex>
);
})
)}
</Flex>
{isEditMode && (
<Button onClick={onResetToDefaultClick}>{i18n('all-panel.resetToDefault')}</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
const openModalCount = data?.meta?.layers?.filter(
({type}) => type === 'modal',
).length;
callback(openModalCount !== 0);

Check warning on line 75 in src/components/AsideHeader/__stories__/AsideHeaderShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Expected return with your callback function
}
});
};
Expand Down Expand Up @@ -163,7 +163,7 @@
multipleTooltip={multipleTooltip}
openModalSubscriber={openModalSubscriber}
topAlert={topAlert}
renderFooter={({compact, asideRef}) => (

Check warning on line 166 in src/components/AsideHeader/__stories__/AsideHeaderShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'compact' is already declared in the upper scope on line 62 column 12
<React.Fragment>
<FooterItem
compact={compact}
Expand Down Expand Up @@ -295,8 +295,11 @@
onChangeCompact={(v) => {
setCompact(v);
}}
onMenuMoreClick={() => console.log('onMenuMoreClick')}

Check warning on line 298 in src/components/AsideHeader/__stories__/AsideHeaderShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
onAllPagesClick={() => console.log('onAllPagesClick')}

Check warning on line 299 in src/components/AsideHeader/__stories__/AsideHeaderShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
editMenuProps={{
enableSorting: true,
}}
/>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/AsideHeader/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface EditMenuProps {
onOpenEditMode?: () => void;
onToggleMenuItem?: (changedItem: MenuItem) => void;
onResetSettingsToDefault?: () => void;
enableSorting?: boolean;
onChangeItemsOrder?: (changedItem: MenuItem, oldIndex: number, newIndex: number) => void;
}

export interface AsideHeaderGeneralProps extends QAProps {
Expand Down
Loading