Refactor draggable list (#1874)
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import { DragDropContext, Droppable } from '@hello-pangea/dnd';
|
||||||
import { Meta, StoryObj } from '@storybook/react';
|
import { Meta, StoryObj } from '@storybook/react';
|
||||||
|
|
||||||
import { IconBell } from '@/ui/icon';
|
import { IconBell } from '@/ui/icon';
|
||||||
@ -5,43 +6,32 @@ import { MenuItemDraggable } from '@/ui/menu-item/components/MenuItemDraggable';
|
|||||||
import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator';
|
import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator';
|
||||||
|
|
||||||
import { DraggableItem } from '../components/DraggableItem';
|
import { DraggableItem } from '../components/DraggableItem';
|
||||||
import { DroppableList } from '../components/DroppableList';
|
|
||||||
|
|
||||||
const meta: Meta<typeof DraggableItem> = {
|
const meta: Meta<typeof DraggableItem> = {
|
||||||
title: 'ui/draggable-list/DraggableItem',
|
title: 'UI/DraggableList/DraggableItem',
|
||||||
component: DraggableItem,
|
component: DraggableItem,
|
||||||
decorators: [
|
decorators: [
|
||||||
(Story, { parameters }) => (
|
(Story) => (
|
||||||
<DroppableList
|
<DragDropContext onDragEnd={() => jest.fn()}>
|
||||||
droppableId={parameters.droppableId}
|
<Droppable droppableId="droppable-id">
|
||||||
onDragEnd={parameters.onDragEnd}
|
{(_provided) => <Story />}
|
||||||
draggableItems={<Story />}
|
</Droppable>
|
||||||
/>
|
</DragDropContext>
|
||||||
),
|
),
|
||||||
ComponentDecorator,
|
ComponentDecorator,
|
||||||
],
|
],
|
||||||
parameters: {
|
parameters: {
|
||||||
droppableId: 'droppable',
|
container: { width: 100 },
|
||||||
onDragEnd: () => console.log('dragged'),
|
},
|
||||||
|
argTypes: {
|
||||||
|
itemComponent: { control: { disable: true } },
|
||||||
},
|
},
|
||||||
args: {
|
args: {
|
||||||
draggableId: 'draggable-1',
|
draggableId: 'draggable-1',
|
||||||
key: 'key-1',
|
|
||||||
index: 0,
|
index: 0,
|
||||||
isDragDisabled: false,
|
isDragDisabled: false,
|
||||||
itemComponent: (
|
itemComponent: (
|
||||||
<>
|
<MenuItemDraggable LeftIcon={IconBell} text="Draggable item 1" />
|
||||||
<MenuItemDraggable
|
|
||||||
LeftIcon={IconBell}
|
|
||||||
key="key-1"
|
|
||||||
text="Draggable item 1"
|
|
||||||
/>
|
|
||||||
<MenuItemDraggable
|
|
||||||
LeftIcon={IconBell}
|
|
||||||
key="key-2"
|
|
||||||
text="Draggable item 2"
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -0,0 +1,57 @@
|
|||||||
|
import { Meta, StoryObj } from '@storybook/react';
|
||||||
|
|
||||||
|
import { IconBell } from '@/ui/icon';
|
||||||
|
import { MenuItemDraggable } from '@/ui/menu-item/components/MenuItemDraggable';
|
||||||
|
import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator';
|
||||||
|
|
||||||
|
import { DraggableItem } from '../components/DraggableItem';
|
||||||
|
import { DraggableList } from '../components/DraggableList';
|
||||||
|
|
||||||
|
const meta: Meta<typeof DraggableList> = {
|
||||||
|
title: 'UI/DraggableList/DraggableList',
|
||||||
|
component: DraggableList,
|
||||||
|
decorators: [ComponentDecorator],
|
||||||
|
parameters: {
|
||||||
|
onDragEnd: () => console.log('dragged'),
|
||||||
|
},
|
||||||
|
argTypes: {
|
||||||
|
draggableItems: { control: false },
|
||||||
|
},
|
||||||
|
args: {
|
||||||
|
draggableItems: (
|
||||||
|
<>
|
||||||
|
<DraggableItem
|
||||||
|
draggableId="draggable-1"
|
||||||
|
index={0}
|
||||||
|
isDragDisabled={false}
|
||||||
|
itemComponent={
|
||||||
|
<MenuItemDraggable
|
||||||
|
LeftIcon={IconBell}
|
||||||
|
text="Non Draggable item 1"
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<DraggableItem
|
||||||
|
draggableId="draggable-2"
|
||||||
|
index={1}
|
||||||
|
itemComponent={
|
||||||
|
<MenuItemDraggable LeftIcon={IconBell} text="Draggable item 2" />
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<DraggableItem
|
||||||
|
draggableId="draggable-3"
|
||||||
|
index={2}
|
||||||
|
itemComponent={
|
||||||
|
<MenuItemDraggable LeftIcon={IconBell} text="Draggable item 3" />
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default meta;
|
||||||
|
|
||||||
|
type Story = StoryObj<typeof DraggableItem>;
|
||||||
|
|
||||||
|
export const Default: Story = {};
|
||||||
@ -3,7 +3,6 @@ import { useTheme } from '@emotion/react';
|
|||||||
import { Draggable } from '@hello-pangea/dnd';
|
import { Draggable } from '@hello-pangea/dnd';
|
||||||
|
|
||||||
type DraggableItemProps = {
|
type DraggableItemProps = {
|
||||||
key: string;
|
|
||||||
draggableId: string;
|
draggableId: string;
|
||||||
isDragDisabled?: boolean;
|
isDragDisabled?: boolean;
|
||||||
index: number;
|
index: number;
|
||||||
@ -11,7 +10,6 @@ type DraggableItemProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const DraggableItem = ({
|
export const DraggableItem = ({
|
||||||
key,
|
|
||||||
draggableId,
|
draggableId,
|
||||||
isDragDisabled = false,
|
isDragDisabled = false,
|
||||||
index,
|
index,
|
||||||
@ -20,7 +18,7 @@ export const DraggableItem = ({
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
return (
|
return (
|
||||||
<Draggable
|
<Draggable
|
||||||
key={key}
|
key={draggableId}
|
||||||
draggableId={draggableId}
|
draggableId={draggableId}
|
||||||
index={index}
|
index={index}
|
||||||
isDragDisabled={isDragDisabled}
|
isDragDisabled={isDragDisabled}
|
||||||
|
|||||||
@ -4,9 +4,9 @@ import {
|
|||||||
Droppable,
|
Droppable,
|
||||||
OnDragEndResponder,
|
OnDragEndResponder,
|
||||||
} from '@hello-pangea/dnd';
|
} from '@hello-pangea/dnd';
|
||||||
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
type DroppableListProps = {
|
type DraggableListProps = {
|
||||||
droppableId: string;
|
|
||||||
draggableItems: React.ReactNode;
|
draggableItems: React.ReactNode;
|
||||||
onDragEnd: OnDragEndResponder;
|
onDragEnd: OnDragEndResponder;
|
||||||
};
|
};
|
||||||
@ -15,15 +15,14 @@ const StyledDragDropItemsWrapper = styled.div`
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const DroppableList = ({
|
export const DraggableList = ({
|
||||||
droppableId,
|
|
||||||
draggableItems,
|
draggableItems,
|
||||||
onDragEnd,
|
onDragEnd,
|
||||||
}: DroppableListProps) => {
|
}: DraggableListProps) => {
|
||||||
return (
|
return (
|
||||||
<DragDropContext onDragEnd={onDragEnd}>
|
<DragDropContext onDragEnd={onDragEnd}>
|
||||||
<StyledDragDropItemsWrapper>
|
<StyledDragDropItemsWrapper>
|
||||||
<Droppable droppableId={droppableId}>
|
<Droppable droppableId={v4()}>
|
||||||
{(provided) => (
|
{(provided) => (
|
||||||
<div ref={provided.innerRef} {...provided.droppableProps}>
|
<div ref={provided.innerRef} {...provided.droppableProps}>
|
||||||
{draggableItems}
|
{draggableItems}
|
||||||
@ -8,7 +8,6 @@ import { MenuItemAccent } from '../types/MenuItemAccent';
|
|||||||
import { MenuItemIconButton } from './MenuItem';
|
import { MenuItemIconButton } from './MenuItem';
|
||||||
|
|
||||||
export type MenuItemDraggableProps = {
|
export type MenuItemDraggableProps = {
|
||||||
key: string;
|
|
||||||
LeftIcon: IconComponent | undefined;
|
LeftIcon: IconComponent | undefined;
|
||||||
accent?: MenuItemAccent;
|
accent?: MenuItemAccent;
|
||||||
iconButtons?: MenuItemIconButton[];
|
iconButtons?: MenuItemIconButton[];
|
||||||
@ -18,7 +17,6 @@ export type MenuItemDraggableProps = {
|
|||||||
className?: string;
|
className?: string;
|
||||||
};
|
};
|
||||||
export const MenuItemDraggable = ({
|
export const MenuItemDraggable = ({
|
||||||
key,
|
|
||||||
LeftIcon,
|
LeftIcon,
|
||||||
accent = 'default',
|
accent = 'default',
|
||||||
iconButtons,
|
iconButtons,
|
||||||
@ -31,7 +29,6 @@ export const MenuItemDraggable = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledHoverableMenuItemBase
|
<StyledHoverableMenuItemBase
|
||||||
data-testid={key ?? undefined}
|
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
accent={accent}
|
accent={accent}
|
||||||
className={className}
|
className={className}
|
||||||
@ -39,7 +36,6 @@ export const MenuItemDraggable = ({
|
|||||||
<MenuItemLeftContent
|
<MenuItemLeftContent
|
||||||
LeftIcon={LeftIcon}
|
LeftIcon={LeftIcon}
|
||||||
text={text}
|
text={text}
|
||||||
key={key}
|
|
||||||
showGrip={!isDragDisabled}
|
showGrip={!isDragDisabled}
|
||||||
/>
|
/>
|
||||||
<div className="hoverable-buttons">
|
<div className="hoverable-buttons">
|
||||||
|
|||||||
@ -22,7 +22,6 @@ type Story = StoryObj<typeof MenuItemDraggable>;
|
|||||||
|
|
||||||
export const Default: Story = {
|
export const Default: Story = {
|
||||||
args: {
|
args: {
|
||||||
key: 'key-1',
|
|
||||||
LeftIcon: IconBell,
|
LeftIcon: IconBell,
|
||||||
accent: 'default',
|
accent: 'default',
|
||||||
iconButtons: [{ Icon: IconMinus, onClick: () => console.log('Clicked') }],
|
iconButtons: [{ Icon: IconMinus, onClick: () => console.log('Clicked') }],
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {
|
|||||||
} from '@hello-pangea/dnd';
|
} from '@hello-pangea/dnd';
|
||||||
|
|
||||||
import { DraggableItem } from '@/ui/draggable-list/components/DraggableItem';
|
import { DraggableItem } from '@/ui/draggable-list/components/DraggableItem';
|
||||||
import { DroppableList } from '@/ui/draggable-list/components/DroppableList';
|
import { DraggableList } from '@/ui/draggable-list/components/DraggableList';
|
||||||
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
|
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
|
||||||
import { StyledDropdownMenuSubheader } from '@/ui/dropdown/components/StyledDropdownMenuSubheader';
|
import { StyledDropdownMenuSubheader } from '@/ui/dropdown/components/StyledDropdownMenuSubheader';
|
||||||
import { IconMinus, IconPlus } from '@/ui/icon';
|
import { IconMinus, IconPlus } from '@/ui/icon';
|
||||||
@ -49,8 +49,7 @@ export const ViewFieldsVisibilityDropdownSection = ({
|
|||||||
<StyledDropdownMenuSubheader>{title}</StyledDropdownMenuSubheader>
|
<StyledDropdownMenuSubheader>{title}</StyledDropdownMenuSubheader>
|
||||||
<StyledDropdownMenuItemsContainer>
|
<StyledDropdownMenuItemsContainer>
|
||||||
{isDraggable && (
|
{isDraggable && (
|
||||||
<DroppableList
|
<DraggableList
|
||||||
droppableId="droppable"
|
|
||||||
onDragEnd={handleOnDrag}
|
onDragEnd={handleOnDrag}
|
||||||
draggableItems={
|
draggableItems={
|
||||||
<>
|
<>
|
||||||
|
|||||||
Reference in New Issue
Block a user