Skip to content

Commit 35be9a0

Browse files
committed
feat: update target-container demo
1 parent 6cd021f commit 35be9a0

File tree

9 files changed

+263
-4
lines changed

9 files changed

+263
-4
lines changed

.dumi/global.less

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
--vp-c-divider: #e2e2e3;
55
--vp-c-bg: #ffffff;
66
--vp-c-text-1: rgba(60, 60, 67);
7+
--vp-c-text-2: rgba(60, 60, 67, .78);
8+
--vp-c-bg-soft: #f6f6f7;
79
}
810

911

@@ -68,3 +70,35 @@ input[type='number'] {
6870
.dumi-default-previewer-demo .demo-actions .demo-platforms {
6971
visibility: hidden;
7072
}
73+
74+
.dumi-default-previewer-demo table{
75+
display: table;
76+
width: 100%;
77+
font-size: 15px;
78+
border-collapse: collapse;
79+
margin: 20px 0;
80+
overflow-x: auto;
81+
}
82+
83+
.dumi-default-previewer-demo th {
84+
text-align: left;
85+
font-size: 14px;
86+
font-weight: 600;
87+
color: var(--vp-c-text-2);
88+
background-color: var(--vp-c-bg-soft);
89+
}
90+
91+
.dumi-default-previewer-demo tr {
92+
background-color: var(--vp-c-bg);
93+
border-top: 1px solid var(--vp-c-divider);
94+
transition: background-color .5s;
95+
}
96+
97+
.dumi-default-previewer-demo tr:nth-child(2n) {
98+
background-color: var(--vp-c-bg-soft);
99+
}
100+
101+
.dumi-default-previewer-demo th, .dumi-default-previewer-demo td {
102+
border: 1px solid var(--vp-c-divider);
103+
padding: 8px 16px;
104+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ We have encapsulated a variety of usages for this, you can use components, funct
1616

1717
## Solve pain points
1818

19-
In `Sortablejs` official `React` components in the past, the drag-and-drop list is implemented by using the component as a direct child element of the list. When we use some component libraries, if there is no slot for the root element of the list in the component library , it is difficult for us to implement a drag list, vue-draggable-plus perfectly solves this problem, it allows you to use a drag list on any element, we can use the selector of the specified element to get the root element of the list, and then Use the root element of the list as `container` of `Sortablejs`, for details, refer to [specify target container](/src/target-container/).
19+
In `Sortablejs` official `React` components in the past, the drag-and-drop list is implemented by using the component as a direct child element of the list. When we use some component libraries, if there is no slot for the root element of the list in the component library , it is difficult for us to implement a drag list, react-draggable-plus perfectly solves this problem, it allows you to use a drag list on any element, we can use the selector of the specified element to get the root element of the list, and then Use the root element of the list as `container` of `Sortablejs`, for details, refer to [specify target container](https://forestxiecode.github.io/react-draggable-plus/components/target-container).
2020

2121

2222
## Install

src/core/compoents.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { useImperativeHandle, useRef } from 'react'
22
import { useDraggable, UseDraggableOptions, UseDraggableReturn } from './useDraggable'
3+
import { RefOrElement } from './types'
34

45
export interface IDraggableProps extends UseDraggableOptions<any> {
56
list: any[]
67
setList: (list: any[]) => void
78
tag?: string
8-
target?: string
9+
target?: RefOrElement
910
children?: React.ReactNode;
1011
className?: string
1112
}
@@ -17,7 +18,7 @@ export interface IDraggable extends UseDraggableReturn {
1718
const ReactDraggable: React.ForwardRefRenderFunction<IDraggable, IDraggableProps> = (props, ref) => {
1819
const { list= [], setList, className, ...options } = props
1920
const target = useRef(null);
20-
const draggable = useDraggable((props?.target || target) as string, list, setList, options)
21+
const draggable = useDraggable(props?.target || target, list, setList, options)
2122
useImperativeHandle(ref, () => ({
2223
el: target.current,
2324
...draggable,

src/core/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export type RefOrValue<T> = T
44
export type RefOrElement<T = HTMLElement> =
55
| T
66
| React.MutableRefObject<any>
7-
| string
87
export type Fn = (...args: any[]) => any

src/target-container/ATable.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { forwardRef } from 'react';
2+
3+
const ATable = (props, ref) => {
4+
return (
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Id</th>
9+
<th>Name</th>
10+
</tr>
11+
</thead>
12+
<tbody ref={ref}>
13+
{props?.list?.map((item) => {
14+
return (
15+
<tr key={item.name} className="cursor-move">
16+
<td>{item.id}</td>
17+
<td>{item.name}</td>
18+
</tr>
19+
);
20+
})}
21+
</tbody>
22+
</table>
23+
);
24+
};
25+
export default forwardRef(ATable);

src/target-container/demo.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { useRef, useState } from 'react';
2+
import { ReactDraggablePlus } from 'react-draggable-plus';
3+
import PreviewList from 'react-draggable-plus/builtins/PreviewList';
4+
import ATable from './ATable';
5+
6+
const Demo = () => {
7+
const [list, setList] = useState([
8+
{
9+
name: 'Joao',
10+
id: 1,
11+
},
12+
{
13+
name: 'Jean',
14+
id: 2,
15+
},
16+
{
17+
name: 'Johanna',
18+
id: 3,
19+
},
20+
{
21+
name: 'Juan',
22+
id: 4,
23+
},
24+
]);
25+
const atEl = useRef(null)
26+
return (
27+
<div>
28+
<div>
29+
<ReactDraggablePlus list={list} setList={setList} animation={150} target={atEl}>
30+
<ATable list={list} ref={atEl}></ATable>
31+
</ReactDraggablePlus>
32+
</div>
33+
<div className="flex justify-between">
34+
<PreviewList list={list} />
35+
</div>
36+
</div>
37+
);
38+
};
39+
40+
export default Demo;

src/target-container/function.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { useRef, useState } from 'react';
2+
import { useDraggable } from 'react-draggable-plus';
3+
import PreviewList from 'react-draggable-plus/builtins/PreviewList';
4+
import ATable from './ATable';
5+
6+
const Function = () => {
7+
const [list, setList] = useState([
8+
{
9+
name: 'Joao',
10+
id: 1,
11+
},
12+
{
13+
name: 'Jean',
14+
id: 2,
15+
},
16+
{
17+
name: 'Johanna',
18+
id: 3,
19+
},
20+
{
21+
name: 'Juan',
22+
id: 4,
23+
},
24+
]);
25+
const atEl = useRef(null);
26+
useDraggable(atEl, list, setList);
27+
28+
return (
29+
<div>
30+
<ATable list={list} ref={atEl}></ATable>
31+
<div className="flex justify-between">
32+
<PreviewList list={list} />
33+
</div>
34+
</div>
35+
);
36+
};
37+
38+
export default Function;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: Specify the target container
3+
nav:
4+
title: 演示
5+
order: 5
6+
toc: content
7+
---
8+
9+
# Specify the target container
10+
11+
Since many components that need to be dragged are not our immediate child elements in our usual use, we need to specify a target container to complete the drag function. We can specify the target container through the `target` attribute.
12+
13+
Here we take the third-party component as an example, assuming `a-table` is a third-party component
14+
15+
### ATable.tsx
16+
17+
```ts
18+
import React, { forwardRef } from 'react';
19+
20+
const ATable = (props, ref) => {
21+
return (
22+
<table>
23+
<thead>
24+
<tr>
25+
<th>Id</th>
26+
<th>Name</th>
27+
</tr>
28+
</thead>
29+
<tbody ref={ref}>
30+
{props?.list?.map((item) => {
31+
return (
32+
<tr key={item.name} className="cursor-move">
33+
<td>{item.id}</td>
34+
<td>{item.name}</td>
35+
</tr>
36+
);
37+
})}
38+
</tbody>
39+
</table>
40+
);
41+
};
42+
43+
export default forwardRef(ATable);
44+
```
45+
46+
## Component Usage
47+
48+
<code src="./demo.tsx"
49+
title="Use components to manipulate target containers"
50+
description="Locate the ref element specified by target">
51+
</code>
52+
53+
## Function Usage
54+
55+
<code src="./function.tsx"
56+
title="Use function to manipulate target containers"
57+
description="Locate the ref element specified by target">
58+
</code>
59+
60+
61+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 指定目标容器
3+
nav:
4+
title: 演示
5+
order: 5
6+
toc: content
7+
---
8+
9+
# 指定目标容器
10+
11+
由于我们平时使用的过程中,很多需要拖拽的组件并非我们的直系子元素,所以我们需要指定一个目标容器,来完成拖拽的功能,我们可以通过 `target` 属性来指定目标容器。
12+
13+
此处我们以第三方组件为例,假设`a-table`为第三方组件
14+
15+
### ATable.tsx
16+
17+
```ts
18+
import React, { forwardRef } from 'react';
19+
20+
const ATable = (props, ref) => {
21+
return (
22+
<table>
23+
<thead>
24+
<tr>
25+
<th>Id</th>
26+
<th>Name</th>
27+
</tr>
28+
</thead>
29+
<tbody ref={ref}>
30+
{props?.list?.map((item) => {
31+
return (
32+
<tr key={item.name} className="cursor-move">
33+
<td>{item.id}</td>
34+
<td>{item.name}</td>
35+
</tr>
36+
);
37+
})}
38+
</tbody>
39+
</table>
40+
);
41+
};
42+
43+
export default forwardRef(ATable);
44+
```
45+
46+
## 组件使用
47+
48+
<code src="./demo.tsx"
49+
title="使用组件操作目标容器"
50+
description="找到target指定的ref元素">
51+
</code>
52+
53+
## 函数使用
54+
55+
<code src="./function.tsx"
56+
title="使用function操作目标容器"
57+
description="找到target指定的ref元素">
58+
</code>
59+
60+
61+

0 commit comments

Comments
 (0)