@steedos-widgets/sortable 1.3.4-beta.8 → 1.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steedos-widgets/sortable",
3
- "version": "1.3.4-beta.8",
3
+ "version": "1.3.5",
4
4
  "main": "dist/sortable.cjs.js",
5
5
  "module": "dist/sortable.esm.js",
6
6
  "unpkg": "dist/sortable.umd.js",
@@ -45,7 +45,7 @@
45
45
  "dependencies": {
46
46
  "@dnd-kit/core": "^6.0.5",
47
47
  "@dnd-kit/sortable": "^7.0.1",
48
- "@steedos-widgets/amis-lib": "1.3.4-beta.8"
48
+ "@steedos-widgets/amis-lib": "1.3.5"
49
49
  },
50
- "gitHead": "5a2f9de4ec64c0673b8c796367bd642d14d45aaf"
50
+ "gitHead": "696396061c57decfde1f9d8f14c36fff7f4316b9"
51
51
  }
@@ -1,6 +1,6 @@
1
1
  import React, { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { createPortal, unstable_batchedUpdates } from 'react-dom';
3
- import { map, keyBy, cloneDeep } from 'lodash';
3
+ import { map, keyBy, cloneDeep, keys } from 'lodash';
4
4
 
5
5
  import { createObject } from '@steedos-widgets/amis-lib'
6
6
 
@@ -33,6 +33,7 @@ import {
33
33
  arrayMove,
34
34
  defaultAnimateLayoutChanges,
35
35
  verticalListSortingStrategy,
36
+ rectSortingStrategy,
36
37
  SortingStrategy,
37
38
  horizontalListSortingStrategy,
38
39
  } from '@dnd-kit/sortable';
@@ -153,7 +154,7 @@ interface Props {
153
154
  vertical?: boolean;
154
155
  boardSource: [{id:string, label:string}?],
155
156
  cardSource: [{id:string, label:string, color: string, columnSpan: number, body: [any]}?],
156
- defaultValue: any,
157
+ value: any,
157
158
  onChange: Function,
158
159
  data: any,
159
160
  dispatchEvent: Function,
@@ -171,7 +172,6 @@ const PLACEHOLDER_ID = 'placeholder';
171
172
  const empty: UniqueIdentifier[] = [];
172
173
 
173
174
  export function MultipleContainers(props) {
174
-
175
175
  let {
176
176
  adjustScale = false,
177
177
  itemCount = 3,
@@ -185,14 +185,15 @@ export function MultipleContainers(props) {
185
185
  minimal = false,
186
186
  modifiers,
187
187
  renderItem,
188
- strategy = verticalListSortingStrategy,
188
+ // strategy = verticalListSortingStrategy,
189
+ strategy = rectSortingStrategy,//这里默认值不用rectSortingStrategy的话,会出现字段在左右两边拖动变更顺序时拖动过程中dragOverlay丢失问题
189
190
  addable = false,
190
191
  trashable = false,
191
192
  vertical = false,
192
193
  scrollable,
193
194
  boardSource = [],
194
195
  cardSource = [],
195
- defaultValue,
196
+ value,
196
197
  onChange: amisOnChange,
197
198
  data: amisData,
198
199
  dispatchEvent: amisDispatchEvent,
@@ -212,11 +213,11 @@ export function MultipleContainers(props) {
212
213
  cardClassName = "",
213
214
  }: Props = props
214
215
 
215
- defaultValue && delete(defaultValue.$$id);
216
+ value && delete(value.$$id);
216
217
 
217
218
  const [items, setItems] = useState<Items>(
218
219
  () => {
219
- return (defaultValue as Items) ?? {
220
+ return (value as Items) ?? {
220
221
  A: ['A1', 'A2'],
221
222
  B: ['B1', 'B2'],
222
223
  C: ['C1', 'C2'],
@@ -228,10 +229,15 @@ export function MultipleContainers(props) {
228
229
  Object.keys(items) as UniqueIdentifier[]
229
230
  );
230
231
 
231
- const handleChange = async () => {
232
+ useEffect(() => {
233
+ setItems(value as Items);
234
+ setContainers(Object.keys(value) as UniqueIdentifier[]);
235
+ }, [value]);
236
+
237
+ const handleChange = async (newItems? : any) => {
232
238
  if (!amisDispatchEvent || !amisOnChange)
233
239
  return
234
- const value = items;
240
+ const value = newItems || items;
235
241
 
236
242
  // 支持 amis OnEvent.change
237
243
  const rendererEvent = await amisDispatchEvent(
@@ -244,7 +250,7 @@ export function MultipleContainers(props) {
244
250
  return;
245
251
  }
246
252
 
247
- setTimeout(()=> amisOnChange(value), 1000);
253
+ setTimeout(()=> amisOnChange(value), 500);
248
254
  }
249
255
 
250
256
  const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
@@ -385,6 +391,7 @@ export function MultipleContainers(props) {
385
391
  const overId = over?.id;
386
392
 
387
393
  if (overId == null || overId === TRASH_ID || active.id in items) {
394
+ // 拖动的是分组则跳过后面的逻辑
388
395
  return;
389
396
  }
390
397
 
@@ -494,26 +501,55 @@ export function MultipleContainers(props) {
494
501
 
495
502
  const overContainer = findContainer(overId);
496
503
 
497
- if (overContainer) {
498
- const activeIndex = items[activeContainer].indexOf(active.id);
499
- const overIndex = items[overContainer].indexOf(overId);
504
+ let newItems = items;
500
505
 
501
- if (activeIndex !== overIndex) {
502
- setItems((items) => ({
503
- ...items,
504
- [overContainer]: arrayMove(
505
- items[overContainer],
506
- activeIndex,
507
- overIndex
508
- ),
509
- }));
506
+ if (overContainer) {
507
+ if(activeContainer !== overContainer){
508
+ // 拖动变更分组之间的顺序时,activeContainer 与 overContainer 值不相等
509
+ setTimeout(function(){
510
+ const sortedGroups = over.data.current.sortable.items; //不加setTimeout的话,这里拿到的会是变更前的数据
511
+ newItems = {};
512
+ sortedGroups.forEach((groupKey: string) => {
513
+ newItems[groupKey] = items[groupKey];
514
+ });
515
+ delete newItems[TRASH_ID];
516
+ delete newItems[PLACEHOLDER_ID];
517
+ if(keys(items).join(",") !== keys(newItems).join(",")){
518
+ // 只有顺序发生变化时才触发change事件
519
+ setItems(newItems);
520
+ // console.log('拖动结束2,更新form value')
521
+ handleChange(newItems)
522
+ }
523
+
524
+ setActiveId(null);
525
+ },500);
526
+ return;
527
+ }
528
+ else {
529
+ // 同一个分组中字段顺序变更以及把一个字段从某个分组拖动到另一个分组内时,activeContainer 与 overContainer 值相等
530
+ const activeIndex = items[activeContainer].indexOf(active.id);
531
+ const overIndex = items[overContainer].indexOf(overId);
532
+
533
+ if (activeIndex !== overIndex) {
534
+ setItems((items) => {
535
+ newItems = {
536
+ ...items,
537
+ [overContainer]: arrayMove(
538
+ items[overContainer],
539
+ activeIndex,
540
+ overIndex
541
+ ),
542
+ }
543
+ return newItems;
544
+ });
545
+ }
510
546
  }
511
547
  }
512
548
 
513
549
  setActiveId(null);
514
550
 
515
551
  // console.log('拖动结束2,更新form value')
516
- handleChange()
552
+ handleChange(newItems)
517
553
  }}
518
554
  cancelDrop={cancelDrop}
519
555
  onDragCancel={onDragCancel}