Skip to content

Commit 5ee816f

Browse files
committed
feat(components): 新增 DraggableGroup 拖拽组组件
1 parent 5e8f928 commit 5ee816f

File tree

6 files changed

+32
-26
lines changed

6 files changed

+32
-26
lines changed

packages/components/src/draggable/demo/draggable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: draggable
2+
* title: 禁止拖拽选项
33
* description: html 元素设置 draggable 为 false 时不可拖动。
44
*/
55

packages/components/src/draggable/demo/tag.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: tag
2+
* title: 根标签
33
* description: 默认根元素为 div,当 tag 为 null 时 Draggable 组件不提供根元素。
44
*/
55

packages/components/src/draggable/demo/transition.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* title: transition
2+
* title: 动画
33
* description: 开启 transition 动画。
44
*/
55

packages/components/src/draggable/hooks/useDraggableChildren.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ export function useDraggableChildren({
4646
const id = stateRef.current.id;
4747
const item = listRef.current.modify[idx];
4848
const emit = (): void => {
49-
console.log('emit', stateRef.current.id);
5049
// preverRef.current = null;
5150
if (isListChanged()) {
5251
const result = listRef.current.modify.slice();
53-
console.log('onchange', result);
5452
onChange?.(result);
5553
}
5654
};
@@ -118,14 +116,17 @@ export function useDraggableChildren({
118116
//
119117
// if (from === index) return;
120118
// move(from, index);
121-
console.log('onDrop');
122119
e.preventDefault();
123120
stateRef.current.index = -1;
124121

125122
const from = fromRef.current;
126123
if (!from) return;
127124

128125
if (from.id !== id) from.drop?.(id);
126+
else if (!isListChanged()) {
127+
// 当在同一个组件拖动过但又没有实际变动时,刷新当前拖动项的选中状态
128+
updateChildrenOf(from.children, idx);
129+
}
129130
// 在同一个组件内拖拽的话 to = from
130131
// if (from.id !== toRef.current?.id) toRef.current?.drop?.(id);
131132
const to = toRef.current;
@@ -137,7 +138,6 @@ export function useDraggableChildren({
137138
// eslint-disable-next-line perfectionist/sort-objects
138139
onDragEnterCapture(e: DragEvent): void {
139140
const index = listRef.current.modify.indexOf(item);
140-
console.log('enter', id, index, stateRef.current.index);
141141
const target = e.target as HTMLElement;
142142
e.preventDefault();
143143

@@ -150,12 +150,9 @@ export function useDraggableChildren({
150150

151151
const from = fromRef.current;
152152
const prever = toRef.current || from;
153-
console.log(prever);
154153
if (!prever || !from) return;
155-
console.log(item, from.item);
156154
prever.enter?.(id);
157155

158-
console.log(prever.id, id, prever.id === id);
159156
const fromIndex = listRef.current.modify.indexOf(from.item);
160157
if (from.item !== item) {
161158
if (prever.id === id || fromIndex !== -1) {
@@ -175,7 +172,6 @@ export function useDraggableChildren({
175172
move(cloneAndAddClassName(from.children), fromIndex, toIndex);
176173
} else moveFromGroup(from.item, from.children, index);
177174
} else {
178-
console.log('uuuuuuuuuuup');
179175
updateChildrenOf(
180176
cloneAndAddClassName(
181177
modifyChildrenRef.current[fromIndex] as ReactElement,
@@ -187,14 +183,12 @@ export function useDraggableChildren({
187183
stateRef.current.index = index;
188184
toRef.current = {
189185
enter: (id2): void => {
190-
console.log('id===id', id2 === id);
191186
if (id2 !== id) {
192187
stateRef.current.index = -1;
193188
if (!listRef.current.origin.includes(from.item)) {
194189
removeItem(from.item);
195190
}
196191
if (type === 'move') {
197-
console.log('mmmmmmmmm', item, from.item);
198192
updateChildrenOf(
199193
cloneAndAddClassName(from.children, cls['--'].hidden),
200194
listRef.current.modify.indexOf(from.item),
@@ -205,13 +199,11 @@ export function useDraggableChildren({
205199
stateRef.current.index = -1;
206200
},
207201
cancel: (_id2): void => {
208-
console.log('oncancel', id, _id2, prever.id, target);
209202
stateRef.current.index = -1;
210203
if (!listRef.current.origin.includes(from.item)) {
211204
removeItem(from.item);
212205
toRef.current = null;
213206
} else {
214-
console.log('ccccccccccccc');
215207
const index = listRef.current.modify.indexOf(from.item);
216208
// 还原成未加工状态
217209
updateChildrenOf(from.children, index);
@@ -225,7 +217,6 @@ export function useDraggableChildren({
225217
item,
226218
id,
227219
};
228-
console.log('-'.repeat(20));
229220
},
230221
onDragOver(e: DragEvent) {
231222
if (el.props.draggable === false) return;
@@ -249,7 +240,6 @@ export function useDraggableChildren({
249240
forceUpdate();
250241
}
251242
function moveFromGroup(item: unknown, children: ReactElement, to: number) {
252-
console.log('moveFromGroup', item, to, item);
253243
children = cloneAndAddClassName(children);
254244
const temp = modifyChildrenRef.current.slice();
255245
insertToArray(children, to, temp);

packages/components/src/draggable/index.en-US.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ Draggable 拖拽。
2222

2323
## API
2424

25+
### Draggable
26+
2527
Draggable 的属性说明如下:
2628

27-
| 属性 | 说明 | 类型 | 默认值 | 版本 |
28-
| ---------- | ---------------- | --------------------------------------------------------- | ------ | ---- |
29-
| list | 列表对应的数组 | any[] | -- | -- |
30-
| onChange | 列表改动回调 | (list: T[]) => void | -- | -- |
31-
| tag | 组件 html 根元素 | string \| null | 'div' | -- |
32-
| transition | 动画效果 | boolean \| [TransitionGroupProps](./transition-group#api) | 'div' | -- |
33-
| attrs | html 标签属性 | Partial\<React.HTMLAttributes\<HTMLDivElement>> | -- | -- |
29+
| 属性 | 说明 | 类型 | 默认值 | 版本 |
30+
| ---------- | ---------------- | ------------------------------------------------------------ | ------ | ---- |
31+
| list | 列表对应的数组 | any[] | -- | -- |
32+
| onChange | 列表改动回调 | (list: T[]) => void | -- | -- |
33+
| tag | 组件 html 根元素 | string \| null | 'div' | -- |
34+
| transition | 动画效果 | boolean \| [TransitionGroupProps](./transition-group-cn#api) | 'div' | -- |
35+
| attrs | html 标签属性 | Partial\<React.HTMLAttributes\<HTMLDivElement>> | -- | -- |
36+
37+
### DraggableGroup
38+
39+
DraggableGroup 的属性说明如下:
3440

35-
其他说明。
41+
| 属性 | 说明 | 类型 | 默认值 | 版本 |
42+
| ---- | ---------------------------------- | ---------------- | ------ | ---- |
43+
| type | 拖动类型,对拖动数据是复制还是移动 | 'copy' \| 'move' | 'move' | -- |

packages/components/src/draggable/index.zh-CN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Draggable 拖拽。
2222

2323
## API
2424

25+
### Draggable
26+
2527
Draggable 的属性说明如下:
2628

2729
| 属性 | 说明 | 类型 | 默认值 | 版本 |
@@ -32,4 +34,10 @@ Draggable 的属性说明如下:
3234
| transition | 动画效果 | boolean \| [TransitionGroupProps](./transition-group-cn#api) | 'div' | -- |
3335
| attrs | html 标签属性 | Partial\<React.HTMLAttributes\<HTMLDivElement>> | -- | -- |
3436

35-
其他说明。
37+
### DraggableGroup
38+
39+
DraggableGroup 的属性说明如下:
40+
41+
| 属性 | 说明 | 类型 | 默认值 | 版本 |
42+
| ---- | ---------------------------------- | ---------------- | ------ | ---- |
43+
| type | 拖动类型,对拖动数据是复制还是移动 | 'copy' \| 'move' | 'move' | -- |

0 commit comments

Comments
 (0)