@canlooks/can-ui 0.0.205 → 0.0.206
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/dist/cjs/components/selectedList/selectedItem.js +2 -1
- package/dist/cjs/components/selectedList/selectedList.js +6 -1
- package/dist/cjs/utils/dnd.d.ts +1 -0
- package/dist/cjs/utils/dnd.js +12 -9
- package/dist/esm/components/selectedList/selectedItem.js +2 -1
- package/dist/esm/components/selectedList/selectedList.js +7 -2
- package/dist/esm/utils/dnd.d.ts +1 -0
- package/dist/esm/utils/dnd.js +12 -9
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ const utils_1 = require("../../utils");
|
|
|
7
7
|
const react_1 = require("react");
|
|
8
8
|
const transitionBase_1 = require("../transitionBase");
|
|
9
9
|
const selectedList_style_1 = require("./selectedList.style");
|
|
10
|
+
const sortableItem_1 = require("../sortableItem");
|
|
10
11
|
function SelectedItem({ value, index, itemProps, onClose, ...props }) {
|
|
11
12
|
const [alertProps, setAlertProps] = (0, utils_1.useSyncState)();
|
|
12
13
|
const [loading, requestFn] = (0, utils_1.useLoading)(async () => {
|
|
@@ -17,5 +18,5 @@ function SelectedItem({ value, index, itemProps, onClose, ...props }) {
|
|
|
17
18
|
alertProps.current?.onClose?.(e);
|
|
18
19
|
onClose?.(value, index);
|
|
19
20
|
};
|
|
20
|
-
return ((0, jsx_runtime_1.jsx)(transitionBase_1.Collapse, { ...props, className: selectedList_style_1.classes.option, children: (0, jsx_runtime_1.jsx)(alert_1.Alert,
|
|
21
|
+
return ((0, jsx_runtime_1.jsx)(transitionBase_1.Collapse, { ...props, className: selectedList_style_1.classes.option, children: (0, jsx_runtime_1.jsx)(sortableItem_1.SortableItem, { component: alert_1.Alert, id: value, index: index, closable: true, color: "info", showIcon: false, ...alertProps.current, className: (0, utils_1.clsx)(selectedList_style_1.classes.optionWrap, alertProps.current?.className), onClose: closeHandler, loading: loading.current }) }));
|
|
21
22
|
}
|
|
@@ -8,6 +8,7 @@ const selectedList_style_1 = require("./selectedList.style");
|
|
|
8
8
|
const react_transition_group_1 = require("react-transition-group");
|
|
9
9
|
const selectionContext_1 = require("../selectionContext");
|
|
10
10
|
const selectedItem_1 = require("./selectedItem");
|
|
11
|
+
const react_2 = require("@dnd-kit/react");
|
|
11
12
|
exports.SelectedList = (0, react_1.memo)(({ value, itemProps, onClose, ...props }) => {
|
|
12
13
|
const { value: innerValue, setValue: setInnerValue } = (0, selectionContext_1.useSelectionContext)();
|
|
13
14
|
const arr = (0, utils_1.toArray)(value || innerValue);
|
|
@@ -15,5 +16,9 @@ exports.SelectedList = (0, react_1.memo)(({ value, itemProps, onClose, ...props
|
|
|
15
16
|
onClose?.(v, i);
|
|
16
17
|
setInnerValue?.(arr?.filter(_v => _v !== v));
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
+
const dragEndHandler = e => {
|
|
20
|
+
const newValue = (0, utils_1.onDndDragEnd)(e, arr);
|
|
21
|
+
newValue && setInnerValue(newValue);
|
|
22
|
+
};
|
|
23
|
+
return ((0, jsx_runtime_1.jsx)(react_2.DragDropProvider, { sensors: utils_1.defaultSensors, onDragEnd: dragEndHandler, children: (0, jsx_runtime_1.jsx)(react_transition_group_1.TransitionGroup, { ...props, css: selectedList_style_1.style, className: (0, utils_1.clsx)(selectedList_style_1.classes.root, props.className), children: arr?.map((v, i) => (0, jsx_runtime_1.jsx)(selectedItem_1.SelectedItem, { value: v, index: i, itemProps: itemProps, onClose: closeHandler }, v)) }) }));
|
|
19
24
|
});
|
package/dist/cjs/utils/dnd.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare const defaultSensors: Customizable<Sensors>;
|
|
|
13
13
|
* @param primaryKey 索引用的主键,默认为`id`
|
|
14
14
|
* @return 返回新的数组,但如果顺序未改变,会得到null
|
|
15
15
|
*/
|
|
16
|
+
export declare function onDndDragEnd<T>({ operation }: Parameters<DragDropEvents<any, any, any>['dragend']>[0], items: T[]): T[] | null;
|
|
16
17
|
export declare function onDndDragEnd<T extends Obj>({ operation }: Parameters<DragDropEvents<any, any, any>['dragend']>[0], items: T[], primaryKey?: keyof T): T[] | null;
|
|
17
18
|
export declare function arrayMove<T>(array: T[], from: number, to: number): T[];
|
|
18
19
|
/**
|
package/dist/cjs/utils/dnd.js
CHANGED
|
@@ -21,20 +21,23 @@ const defaultSensors = defaults => [
|
|
|
21
21
|
})
|
|
22
22
|
];
|
|
23
23
|
exports.defaultSensors = defaultSensors;
|
|
24
|
-
/**
|
|
25
|
-
* <DragDropProvider>组件通用的onDragEnd方法
|
|
26
|
-
* @param e 事件
|
|
27
|
-
* @param items 传入需要排序的数组
|
|
28
|
-
* @param primaryKey 索引用的主键,默认为`id`
|
|
29
|
-
* @return 返回新的数组,但如果顺序未改变,会得到null
|
|
30
|
-
*/
|
|
31
24
|
function onDndDragEnd({ operation }, items, primaryKey = 'id') {
|
|
32
25
|
const { source, target, canceled } = operation;
|
|
33
26
|
if (!source || !target || canceled) {
|
|
34
27
|
return null;
|
|
35
28
|
}
|
|
36
|
-
const sourceIndex = items.findIndex(item =>
|
|
37
|
-
|
|
29
|
+
const sourceIndex = items.findIndex(item => {
|
|
30
|
+
if (typeof item === 'object' && item !== null) {
|
|
31
|
+
return item[primaryKey] === source.id;
|
|
32
|
+
}
|
|
33
|
+
return item === source.id;
|
|
34
|
+
});
|
|
35
|
+
const targetIndex = items.findIndex(item => {
|
|
36
|
+
if (typeof item === 'object' && item !== null) {
|
|
37
|
+
return item[primaryKey] === target.id;
|
|
38
|
+
}
|
|
39
|
+
return item === target.id;
|
|
40
|
+
});
|
|
38
41
|
if (sourceIndex === -1 || targetIndex === -1) {
|
|
39
42
|
if (typeof source.initialIndex === 'number' && typeof source.index === 'number') {
|
|
40
43
|
const from = source.initialIndex;
|
|
@@ -4,6 +4,7 @@ import { clsx, useLoading, useSyncState } from '../../utils/index.js';
|
|
|
4
4
|
import { useMemo } from 'react';
|
|
5
5
|
import { Collapse } from '../transitionBase/index.js';
|
|
6
6
|
import { classes } from './selectedList.style.js';
|
|
7
|
+
import { SortableItem } from '../sortableItem/index.js';
|
|
7
8
|
export function SelectedItem({ value, index, itemProps, onClose, ...props }) {
|
|
8
9
|
const [alertProps, setAlertProps] = useSyncState();
|
|
9
10
|
const [loading, requestFn] = useLoading(async () => {
|
|
@@ -14,5 +15,5 @@ export function SelectedItem({ value, index, itemProps, onClose, ...props }) {
|
|
|
14
15
|
alertProps.current?.onClose?.(e);
|
|
15
16
|
onClose?.(value, index);
|
|
16
17
|
};
|
|
17
|
-
return (_jsx(Collapse, { ...props, className: classes.option, children: _jsx(
|
|
18
|
+
return (_jsx(Collapse, { ...props, className: classes.option, children: _jsx(SortableItem, { component: Alert, id: value, index: index, closable: true, color: "info", showIcon: false, ...alertProps.current, className: clsx(classes.optionWrap, alertProps.current?.className), onClose: closeHandler, loading: loading.current }) }));
|
|
18
19
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
2
2
|
import { memo } from 'react';
|
|
3
|
-
import { clsx, toArray } from '../../utils/index.js';
|
|
3
|
+
import { clsx, defaultSensors, onDndDragEnd, toArray } from '../../utils/index.js';
|
|
4
4
|
import { classes, style } from './selectedList.style.js';
|
|
5
5
|
import { TransitionGroup } from 'react-transition-group';
|
|
6
6
|
import { useSelectionContext } from '../selectionContext/index.js';
|
|
7
7
|
import { SelectedItem } from './selectedItem.js';
|
|
8
|
+
import { DragDropProvider } from '@dnd-kit/react';
|
|
8
9
|
export const SelectedList = memo(({ value, itemProps, onClose, ...props }) => {
|
|
9
10
|
const { value: innerValue, setValue: setInnerValue } = useSelectionContext();
|
|
10
11
|
const arr = toArray(value || innerValue);
|
|
@@ -12,5 +13,9 @@ export const SelectedList = memo(({ value, itemProps, onClose, ...props }) => {
|
|
|
12
13
|
onClose?.(v, i);
|
|
13
14
|
setInnerValue?.(arr?.filter(_v => _v !== v));
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
+
const dragEndHandler = e => {
|
|
17
|
+
const newValue = onDndDragEnd(e, arr);
|
|
18
|
+
newValue && setInnerValue(newValue);
|
|
19
|
+
};
|
|
20
|
+
return (_jsx(DragDropProvider, { sensors: defaultSensors, onDragEnd: dragEndHandler, children: _jsx(TransitionGroup, { ...props, css: style, className: clsx(classes.root, props.className), children: arr?.map((v, i) => _jsx(SelectedItem, { value: v, index: i, itemProps: itemProps, onClose: closeHandler }, v)) }) }));
|
|
16
21
|
});
|
package/dist/esm/utils/dnd.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare const defaultSensors: Customizable<Sensors>;
|
|
|
13
13
|
* @param primaryKey 索引用的主键,默认为`id`
|
|
14
14
|
* @return 返回新的数组,但如果顺序未改变,会得到null
|
|
15
15
|
*/
|
|
16
|
+
export declare function onDndDragEnd<T>({ operation }: Parameters<DragDropEvents<any, any, any>['dragend']>[0], items: T[]): T[] | null;
|
|
16
17
|
export declare function onDndDragEnd<T extends Obj>({ operation }: Parameters<DragDropEvents<any, any, any>['dragend']>[0], items: T[], primaryKey?: keyof T): T[] | null;
|
|
17
18
|
export declare function arrayMove<T>(array: T[], from: number, to: number): T[];
|
|
18
19
|
/**
|
package/dist/esm/utils/dnd.js
CHANGED
|
@@ -13,20 +13,23 @@ export const defaultSensors = defaults => [
|
|
|
13
13
|
]
|
|
14
14
|
})
|
|
15
15
|
];
|
|
16
|
-
/**
|
|
17
|
-
* <DragDropProvider>组件通用的onDragEnd方法
|
|
18
|
-
* @param e 事件
|
|
19
|
-
* @param items 传入需要排序的数组
|
|
20
|
-
* @param primaryKey 索引用的主键,默认为`id`
|
|
21
|
-
* @return 返回新的数组,但如果顺序未改变,会得到null
|
|
22
|
-
*/
|
|
23
16
|
export function onDndDragEnd({ operation }, items, primaryKey = 'id') {
|
|
24
17
|
const { source, target, canceled } = operation;
|
|
25
18
|
if (!source || !target || canceled) {
|
|
26
19
|
return null;
|
|
27
20
|
}
|
|
28
|
-
const sourceIndex = items.findIndex(item =>
|
|
29
|
-
|
|
21
|
+
const sourceIndex = items.findIndex(item => {
|
|
22
|
+
if (typeof item === 'object' && item !== null) {
|
|
23
|
+
return item[primaryKey] === source.id;
|
|
24
|
+
}
|
|
25
|
+
return item === source.id;
|
|
26
|
+
});
|
|
27
|
+
const targetIndex = items.findIndex(item => {
|
|
28
|
+
if (typeof item === 'object' && item !== null) {
|
|
29
|
+
return item[primaryKey] === target.id;
|
|
30
|
+
}
|
|
31
|
+
return item === target.id;
|
|
32
|
+
});
|
|
30
33
|
if (sourceIndex === -1 || targetIndex === -1) {
|
|
31
34
|
if (typeof source.initialIndex === 'number' && typeof source.index === 'number') {
|
|
32
35
|
const from = source.initialIndex;
|