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

[PB-3458]: fix/drag and drop issue #41

Merged
merged 3 commits into from
Jan 21, 2025
Merged
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/ui",
"version": "0.0.12",
"version": "0.0.13",
"description": "Library of Internxt components",
"repository": {
"type": "git",
Expand All @@ -18,7 +18,9 @@
],
"peerDependencies": {
"react": "^18.2.0",
"@phosphor-icons/react": "^2.1.5"
"@phosphor-icons/react": "^2.1.5",
"react-dnd": "14.0.5",
"react-dnd-html5-backend": "^14.0.0"
},
"resolutions": {
"jackspeak": "2.1.1"
Expand Down
14 changes: 9 additions & 5 deletions src/components/breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CaretRight, DotsThree } from '@phosphor-icons/react';
import { forwardRef, FunctionComponent, ReactNode, SVGProps } from 'react';
import { Dispatch } from 'redux';
import Dropdown from '../dropdown/Dropdown';
import { DropTargetMonitor } from 'react-dnd';
import BreadcrumbsItem, { BreadcrumbItemData, BreadcrumbsMenuProps } from './BreadcrumbsItem';
import { useDrop } from 'react-dnd';

export interface BreadcrumbsProps<T extends Dispatch> {
items: BreadcrumbItemData[];
Expand All @@ -24,13 +24,12 @@ export interface BreadcrumbsProps<T extends Dispatch> {
isSomeItemSelected: boolean,
selectedItems: [],
dispatch: T,
) => (draggedItem: unknown, monitor: DropTargetMonitor) => Promise<void>;
canItemDrop: (
item: BreadcrumbItemData,
) => (draggedItem: unknown, monitor: DropTargetMonitor<unknown, unknown>) => boolean;
) => (draggedItem: unknown, monitor: unknown) => Promise<void>;
canItemDrop: (item: BreadcrumbItemData) => (draggedItem: unknown, monitor: unknown) => boolean;
itemComponent?: FunctionComponent<SVGProps<SVGSVGElement>>;
acceptedTypes: string[];
dispatch: T;
useDrop: typeof useDrop;
}

/**
Expand Down Expand Up @@ -70,6 +69,9 @@ export interface BreadcrumbsProps<T extends Dispatch> {
*
* @property {Dispatch} dispatch
* - The Redux dispatch function for dispatching actions related to the breadcrumb items.
*
* @property {Functiodn} useDrop
* - Hook for dnd.
*/

const Breadcrumbs = <T extends Dispatch>(props: Readonly<BreadcrumbsProps<T>>): JSX.Element => {
Expand Down Expand Up @@ -120,6 +122,7 @@ const Breadcrumbs = <T extends Dispatch>(props: Readonly<BreadcrumbsProps<T>>):
itemComponent={props.itemComponent}
acceptedTypes={props.acceptedTypes}
dispatch={props.dispatch}
useDrop={props.useDrop}
/>
</MenuItem>,
);
Expand All @@ -139,6 +142,7 @@ const Breadcrumbs = <T extends Dispatch>(props: Readonly<BreadcrumbsProps<T>>):
canItemDrop={props.canItemDrop}
acceptedTypes={props.acceptedTypes}
dispatch={props.dispatch}
useDrop={props.useDrop}
/>,
);
if (i < items.length - 1) {
Expand Down
5 changes: 4 additions & 1 deletion src/components/breadcrumbs/BreadcrumbsItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export interface BreadcrumbsMenuProps {
*
* @property {Dispatch} dispatch
* - The Redux dispatch function for dispatching actions related to the breadcrumb item.
* @property {Functiodn} useDrop
* - Hook for dnd.
*/

export interface BreadcrumbsItemProps<T extends Dispatch> {
Expand Down Expand Up @@ -94,10 +96,11 @@ export interface BreadcrumbsItemProps<T extends Dispatch> {
itemComponent?: FunctionComponent<SVGProps<SVGSVGElement>>;
acceptedTypes: string[];
dispatch: T;
useDrop: typeof useDrop;
}

const BreadcrumbsItem = <T extends Dispatch>(props: BreadcrumbsItemProps<T>): JSX.Element => {
const [{ isOver, canDrop }, drop] = useDrop(
const [{ isOver, canDrop }, drop] = props.useDrop(
() => ({
accept: props.acceptedTypes,
collect: (monitor) => ({
Expand Down
3 changes: 2 additions & 1 deletion src/components/breadcrumbs/__test__/Breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Breadcrumbs, BreadcrumbsProps } from '../';
import { Dispatch, AnyAction } from 'redux';
import { DndProvider } from 'react-dnd';
import { DndProvider, useDrop } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

describe('Breadcrumbs Component', () => {
Expand Down Expand Up @@ -48,6 +48,7 @@ describe('Breadcrumbs Component', () => {
itemComponent: undefined,
acceptedTypes: ['type1', 'type2'],
dispatch: vi.fn(),
useDrop,
};

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('BreadcrumbsItem Component', () => {
onItemDropped: onItemDroppedMock,
dispatch: dispatchMock,
acceptedTypes: ['ITEM_TYPE'],
useDrop: mockedUseDrop,
};

const renderBreadcrumbsItem = (props = {}) => render(<BreadcrumbsItem {...defaultProps} {...props} />);
Expand Down
1 change: 1 addition & 0 deletions src/components/breadcrumbs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as Breadcrumbs } from './Breadcrumbs';
export type { BreadcrumbsProps } from './Breadcrumbs';
export type { BreadcrumbItemData, BreadcrumbsMenuProps } from './BreadcrumbsItem';
3 changes: 2 additions & 1 deletion src/stories/components/breadcrumbs/breadcrumbs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meta, StoryObj } from '@storybook/react';
import { Breadcrumbs, BreadcrumbsProps } from '@/components/breadcrumbs';
import { BreadcrumbItemData, BreadcrumbsMenuProps } from '@/components/breadcrumbs/BreadcrumbsItem';
import { Dispatch } from 'redux';
import { DndProvider } from 'react-dnd';
import { DndProvider, useDrop } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import React, { useEffect, useRef, useState } from 'react';
import { CaretDown } from '@phosphor-icons/react';
Expand Down Expand Up @@ -97,6 +97,7 @@ const defaultBreadcrumbsProps: BreadcrumbsProps<Dispatch> = {
acceptedTypes: ['breadcrumb'],
dispatch: {} as Dispatch,
itemComponent: () => <ExampleIconBlue />,
useDrop: useDrop,
};

const meta: Meta<typeof Breadcrumbs> = {
Expand Down
Loading