@zidian-primitive/cascader 0.1.2 → 0.2.0
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/esm/components/node/CascaderNode.js +25 -2
- package/esm/components/panel/CascaderPanel.js +14 -8
- package/esm/components/panel/CascaderPanelIndicator.js +2 -1
- package/esm/hooks/useCascader.js +34 -26
- package/esm/index.js +1 -1
- package/esm/utils/data.js +26 -13
- package/lib/components/node/CascaderNode.d.ts +1 -0
- package/lib/hooks/useCascader.d.ts +5 -4
- package/lib/types/index.d.ts +15 -15
- package/lib/utils/data.d.ts +3 -3
- package/package.json +1 -1
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useContext } from 'react';
|
|
2
|
+
import { useContext, useRef, useCallback, useEffect } from 'react';
|
|
3
3
|
import { CascaderContext, CascaderPanelContext } from '../../context/index.js';
|
|
4
4
|
|
|
5
5
|
const CascaderNode = (props) => {
|
|
6
6
|
const {
|
|
7
7
|
node,
|
|
8
8
|
isLeaf,
|
|
9
|
+
isActive,
|
|
9
10
|
onClick,
|
|
10
11
|
onTrigger,
|
|
11
12
|
render,
|
|
@@ -13,14 +14,36 @@ const CascaderNode = (props) => {
|
|
|
13
14
|
} = props;
|
|
14
15
|
const { onSelectChange } = useContext(CascaderContext);
|
|
15
16
|
const { showIndicator, registerNode } = useContext(CascaderPanelContext);
|
|
17
|
+
const nodeRef = useRef(null);
|
|
18
|
+
const handleRef = useCallback((el) => {
|
|
19
|
+
nodeRef.current = el;
|
|
20
|
+
if (el && showIndicator) {
|
|
21
|
+
registerNode(node.indices, el);
|
|
22
|
+
}
|
|
23
|
+
}, [showIndicator, registerNode, node.indices]);
|
|
16
24
|
const handleClick = (event) => {
|
|
17
25
|
if (isLeaf) {
|
|
18
26
|
if (onClick) onClick(node);
|
|
27
|
+
if (onTrigger) onTrigger(node.level, node.indices);
|
|
19
28
|
} else {
|
|
20
29
|
if (onTrigger) onTrigger(node.level, node.indices);
|
|
21
30
|
}
|
|
22
31
|
};
|
|
23
|
-
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (isActive && nodeRef.current) {
|
|
34
|
+
const timer = requestAnimationFrame(() => {
|
|
35
|
+
if (nodeRef.current) {
|
|
36
|
+
nodeRef.current.scrollIntoView({
|
|
37
|
+
behavior: "smooth",
|
|
38
|
+
block: "nearest",
|
|
39
|
+
inline: "start"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return () => cancelAnimationFrame(timer);
|
|
44
|
+
}
|
|
45
|
+
}, [isActive]);
|
|
46
|
+
return /* @__PURE__ */ jsx("div", { ref: handleRef, onClick: handleClick, "data-active": isActive, ...elementProps, children: render ? render({ node, onSelectChange }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
24
47
|
/* @__PURE__ */ jsx("span", { style: { flex: 1 }, children: node.label }),
|
|
25
48
|
!isLeaf && /* @__PURE__ */ jsx(
|
|
26
49
|
"svg",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx } from 'react/jsx-runtime';
|
|
2
|
-
import { forwardRef, useRef, useState, useCallback,
|
|
2
|
+
import { forwardRef, useRef, useState, useCallback, useEffect } from 'react';
|
|
3
3
|
import { CascaderPanelContext } from '../../context/index.js';
|
|
4
4
|
|
|
5
5
|
const CascaderPanel = forwardRef((props, ref) => {
|
|
@@ -16,21 +16,27 @@ const CascaderPanel = forwardRef((props, ref) => {
|
|
|
16
16
|
if (el) nodeRefs.current.set(index, el);
|
|
17
17
|
else nodeRefs.current.delete(index);
|
|
18
18
|
}, []);
|
|
19
|
-
|
|
19
|
+
useEffect(() => {
|
|
20
20
|
if (activeIndices === void 0) return;
|
|
21
21
|
const activeEl = nodeRefs.current.get(activeIndices);
|
|
22
22
|
if (activeEl) {
|
|
23
23
|
const measure = () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
requestAnimationFrame(() => {
|
|
25
|
+
if (!activeEl) return;
|
|
26
|
+
setIndicatorStyle({
|
|
27
|
+
["--indicator-offset-top"]: `${activeEl.offsetTop}px`,
|
|
28
|
+
["--indicator-height"]: `${activeEl.offsetHeight}px`,
|
|
29
|
+
transform: `translateY(var(--indicator-offset-top))`,
|
|
30
|
+
height: `${activeEl.offsetHeight}px`
|
|
31
|
+
});
|
|
29
32
|
});
|
|
30
33
|
};
|
|
31
34
|
measure();
|
|
32
|
-
const resizeObserver = new ResizeObserver(
|
|
35
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
36
|
+
requestAnimationFrame(measure);
|
|
37
|
+
});
|
|
33
38
|
resizeObserver.observe(activeEl);
|
|
39
|
+
return () => resizeObserver.disconnect();
|
|
34
40
|
}
|
|
35
41
|
}, [activeIndices]);
|
|
36
42
|
return /* @__PURE__ */ jsx(CascaderPanelContext.Provider, { value: { showIndicator, activeIndices, registerNode, indicatorStyle }, children: /* @__PURE__ */ jsx("div", { ref, "data-depth": depth, "data-active-indices": activeIndices, ...elementProps, children }) });
|
|
@@ -4,13 +4,14 @@ import { CascaderPanelContext } from '../../context/index.js';
|
|
|
4
4
|
|
|
5
5
|
const CascaderPanelIndicator = (props) => {
|
|
6
6
|
const {
|
|
7
|
+
style,
|
|
7
8
|
render,
|
|
8
9
|
...elementProps
|
|
9
10
|
} = props;
|
|
10
11
|
const { indicatorStyle, activeIndices } = useContext(CascaderPanelContext);
|
|
11
12
|
if (render) return render();
|
|
12
13
|
if (activeIndices === void 0) return null;
|
|
13
|
-
return /* @__PURE__ */ jsx("div", { style: indicatorStyle, ...elementProps });
|
|
14
|
+
return /* @__PURE__ */ jsx("div", { style: Object.assign(style ?? {}, indicatorStyle ?? {}), ...elementProps });
|
|
14
15
|
};
|
|
15
16
|
CascaderPanelIndicator.displayName = "CascaderPanelIndicator";
|
|
16
17
|
|
package/esm/hooks/useCascader.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { useMemo, useState } from 'react';
|
|
2
|
-
import { buildCascaderEngine,
|
|
1
|
+
import { useMemo, useCallback, useState, useEffect } from 'react';
|
|
2
|
+
import { buildCascaderEngine, getShowPath, getCascadeColumns, changeShowPath } from '../utils/data.js';
|
|
3
3
|
import { setMultipleNodeStatus, setNodeCheckStatus } from '../utils/logic.js';
|
|
4
4
|
|
|
5
5
|
function useCascader(options) {
|
|
6
|
-
const { data, handleDataConfig, selectOption } = options;
|
|
6
|
+
const { data, handleDataConfig, selectOption, curShow } = options;
|
|
7
7
|
const sortedData = useMemo(() => {
|
|
8
8
|
return [...data].sort((a, b) => {
|
|
9
9
|
for (const field of handleDataConfig.levels) {
|
|
@@ -13,46 +13,54 @@ function useCascader(options) {
|
|
|
13
13
|
return 0;
|
|
14
14
|
});
|
|
15
15
|
}, [data, handleDataConfig]);
|
|
16
|
-
const
|
|
16
|
+
const sourceNodeData = useMemo(() => {
|
|
17
17
|
return buildCascaderEngine(sortedData, handleDataConfig);
|
|
18
18
|
}, [sortedData, handleDataConfig]);
|
|
19
|
-
const
|
|
20
|
-
const statusArray = new Uint8Array(
|
|
19
|
+
const getInitialStatus = useCallback((sourceNodeData2) => {
|
|
20
|
+
const statusArray = new Uint8Array(sourceNodeData2.length);
|
|
21
21
|
if (selectOption?.defaultSelected) {
|
|
22
|
-
const defaultIndices =
|
|
22
|
+
const defaultIndices = sourceNodeData2.filter(
|
|
23
23
|
(node) => selectOption.defaultSelected.find(
|
|
24
24
|
(selected) => node.raw?.value === selected
|
|
25
25
|
)
|
|
26
26
|
).map((node) => node.index);
|
|
27
|
-
return setMultipleNodeStatus(
|
|
27
|
+
return setMultipleNodeStatus(sourceNodeData2, statusArray, defaultIndices, 1);
|
|
28
28
|
}
|
|
29
29
|
return statusArray;
|
|
30
|
-
}, [
|
|
31
|
-
const
|
|
32
|
-
const [
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
nodeIdx: index
|
|
42
|
-
}
|
|
43
|
-
}
|
|
30
|
+
}, [selectOption]);
|
|
31
|
+
const initPath = useMemo(() => getShowPath(sourceNodeData, sourceNodeData.find((node) => curShow === node.value || curShow === node.label)), [sourceNodeData, curShow]);
|
|
32
|
+
const [checkStatusArray, setCheckStatusArray] = useState(getInitialStatus(sourceNodeData));
|
|
33
|
+
const [path, setPath] = useState(initPath);
|
|
34
|
+
const columns = useMemo(() => {
|
|
35
|
+
return getCascadeColumns(sourceNodeData, path);
|
|
36
|
+
}, [sourceNodeData, initPath, path]);
|
|
37
|
+
const handleChangePath = (depth, index) => {
|
|
38
|
+
setPath(changeShowPath(
|
|
39
|
+
sourceNodeData,
|
|
40
|
+
{ oldPath: path, action: { depth, nodeIdx: index } }
|
|
44
41
|
));
|
|
45
42
|
};
|
|
46
43
|
const handleChangeCheckStatus = (nodeIdx) => {
|
|
47
|
-
const newStatusArray = setNodeCheckStatus(
|
|
44
|
+
const newStatusArray = setNodeCheckStatus(sourceNodeData, checkStatusArray, nodeIdx);
|
|
48
45
|
setCheckStatusArray(newStatusArray);
|
|
49
46
|
};
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
setCheckStatusArray(getInitialStatus(sourceNodeData));
|
|
49
|
+
const targetNode = sourceNodeData.find((n) => n.value === curShow);
|
|
50
|
+
setPath(getShowPath(sourceNodeData, targetNode));
|
|
51
|
+
}, [data]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
console.log(111);
|
|
54
|
+
if (curShow || curShow === 0) {
|
|
55
|
+
setPath(getShowPath(sourceNodeData, sourceNodeData.find((node) => curShow === node.value || curShow === node.label)));
|
|
56
|
+
}
|
|
57
|
+
}, [curShow]);
|
|
50
58
|
return {
|
|
51
|
-
nodes:
|
|
59
|
+
nodes: sourceNodeData,
|
|
52
60
|
columns,
|
|
53
|
-
|
|
61
|
+
path,
|
|
54
62
|
checkStatusArray,
|
|
55
|
-
|
|
63
|
+
handleChangePath,
|
|
56
64
|
handleChangeCheckStatus
|
|
57
65
|
};
|
|
58
66
|
}
|
package/esm/index.js
CHANGED
|
@@ -5,5 +5,5 @@ export { CascaderPanel } from './components/panel/CascaderPanel.js';
|
|
|
5
5
|
export { CascaderPanelHeader } from './components/panel/CascaderPanelHeader.js';
|
|
6
6
|
export { CascaderPanelIndicator } from './components/panel/CascaderPanelIndicator.js';
|
|
7
7
|
export { useCascader } from './hooks/useCascader.js';
|
|
8
|
-
export { buildCascaderEngine,
|
|
8
|
+
export { buildCascaderEngine, changeShowPath, getCascadeColumns, getShowPath } from './utils/data.js';
|
|
9
9
|
export { setMultipleNodeStatus, setNodeCheckStatus } from './utils/logic.js';
|
package/esm/utils/data.js
CHANGED
|
@@ -65,11 +65,11 @@ function buildCascaderEngine(data, config) {
|
|
|
65
65
|
}
|
|
66
66
|
return nodes;
|
|
67
67
|
}
|
|
68
|
-
function getCascadeColumns(nodes,
|
|
68
|
+
function getCascadeColumns(nodes, path) {
|
|
69
69
|
const columns = [];
|
|
70
70
|
const rootColumn = nodes.filter((node) => node.parentIndex === -1);
|
|
71
71
|
columns.push(rootColumn);
|
|
72
|
-
|
|
72
|
+
path.forEach((selectedIndex) => {
|
|
73
73
|
const parentNode = nodes[selectedIndex];
|
|
74
74
|
if (parentNode && parentNode.childrenIndices.length > 0) {
|
|
75
75
|
const nextColumn = parentNode.childrenIndices.map((idx) => nodes[idx]);
|
|
@@ -78,25 +78,38 @@ function getCascadeColumns(nodes, showIndices) {
|
|
|
78
78
|
});
|
|
79
79
|
return columns;
|
|
80
80
|
}
|
|
81
|
-
function
|
|
82
|
-
const {
|
|
81
|
+
function changeShowPath(nodes, option) {
|
|
82
|
+
const { oldPath, action } = option;
|
|
83
83
|
let targetPath = [];
|
|
84
|
-
const { depth, nodeIdx } =
|
|
84
|
+
const { depth, nodeIdx } = action;
|
|
85
85
|
let currentNode = nodes[nodeIdx];
|
|
86
86
|
while (currentNode.childrenIndices.length > 0) {
|
|
87
87
|
targetPath.push(currentNode.index);
|
|
88
88
|
currentNode = nodes[currentNode.childrenIndices[0]];
|
|
89
89
|
}
|
|
90
|
-
return
|
|
90
|
+
return oldPath.slice(0, depth).concat(targetPath, currentNode.index);
|
|
91
91
|
}
|
|
92
|
-
function
|
|
93
|
-
let defaultIndices = [];
|
|
92
|
+
function getShowPath(nodes, targetNode) {
|
|
94
93
|
let currentNode = nodes[0];
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
let showPath = [];
|
|
95
|
+
if (targetNode) {
|
|
96
|
+
let parentNode = targetNode;
|
|
97
|
+
showPath = new Array((targetNode?.depth ?? 0) + 1);
|
|
98
|
+
while (parentNode) {
|
|
99
|
+
showPath[parentNode.depth] = parentNode.index;
|
|
100
|
+
parentNode = parentNode.parentIndex !== -1 ? nodes[parentNode.parentIndex] : void 0;
|
|
101
|
+
}
|
|
102
|
+
return showPath;
|
|
103
|
+
}
|
|
104
|
+
while (currentNode) {
|
|
105
|
+
showPath.push(currentNode.index);
|
|
106
|
+
if (currentNode.childrenIndices.length > 0) {
|
|
107
|
+
currentNode = nodes[currentNode.childrenIndices[0]];
|
|
108
|
+
} else {
|
|
109
|
+
currentNode = null;
|
|
110
|
+
}
|
|
98
111
|
}
|
|
99
|
-
return
|
|
112
|
+
return showPath;
|
|
100
113
|
}
|
|
101
114
|
|
|
102
|
-
export { buildCascaderEngine,
|
|
115
|
+
export { buildCascaderEngine, changeShowPath, getCascadeColumns, getShowPath };
|
|
@@ -3,6 +3,7 @@ import { CascaderItem, NodeSlotType } from "../../types";
|
|
|
3
3
|
export interface CascaderNodeProps extends Omit<ComponentProps<'div'>, "onClick"> {
|
|
4
4
|
node: CascaderItem<any>;
|
|
5
5
|
isLeaf: boolean;
|
|
6
|
+
isActive: boolean;
|
|
6
7
|
onClick?: (node: CascaderItem<any>) => void;
|
|
7
8
|
onTrigger?: (depth: number, nodeIdx: number) => void;
|
|
8
9
|
render?: NodeSlotType;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { CascaderNodeType,
|
|
1
|
+
import { CascaderNodeType, CascaderSelectOption, FlatDataConfig } from "../types";
|
|
2
2
|
export type CascaderHookOption<T> = {
|
|
3
3
|
data: T[];
|
|
4
4
|
handleDataConfig: FlatDataConfig;
|
|
5
|
-
selectOption?:
|
|
5
|
+
selectOption?: CascaderSelectOption;
|
|
6
|
+
curShow?: string | number;
|
|
6
7
|
};
|
|
7
8
|
export declare function useCascader<T extends Record<string, any>>(options: CascaderHookOption<T>): {
|
|
8
9
|
nodes: CascaderNodeType<T>[];
|
|
9
10
|
columns: CascaderNodeType<T>[][];
|
|
10
|
-
|
|
11
|
+
path: number[];
|
|
11
12
|
checkStatusArray: Uint8Array<ArrayBufferLike>;
|
|
12
|
-
|
|
13
|
+
handleChangePath: (depth: number, index: number) => void;
|
|
13
14
|
handleChangeCheckStatus: (nodeIdx: number) => void;
|
|
14
15
|
};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ReactNode } from "react";
|
|
2
2
|
export type CheckStatus = 0 | 1 | 2;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**--------------------------------------------
|
|
4
|
+
* Data
|
|
5
|
+
*---------------------------------------------**/
|
|
6
6
|
export interface CascaderNodeType<T = any> {
|
|
7
7
|
depth: number;
|
|
8
8
|
index: number;
|
|
@@ -23,15 +23,15 @@ export interface CascaderItem<T> {
|
|
|
23
23
|
originData?: T;
|
|
24
24
|
checkStatus?: CheckStatus;
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
/**--------------------------------------------
|
|
27
|
+
* Util
|
|
28
|
+
*---------------------------------------------**/
|
|
29
29
|
export type FlatDataConfig = {
|
|
30
30
|
levels: (string | [string, string])[];
|
|
31
31
|
};
|
|
32
32
|
export type GetShowIndicesOption = {
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
oldPath: number[];
|
|
34
|
+
action: {
|
|
35
35
|
depth: number;
|
|
36
36
|
nodeIdx: number;
|
|
37
37
|
};
|
|
@@ -39,16 +39,16 @@ export type GetShowIndicesOption = {
|
|
|
39
39
|
export type GetCascaderColumnsOption = {
|
|
40
40
|
checkStatusArray?: Uint8Array<ArrayBufferLike>;
|
|
41
41
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export type
|
|
42
|
+
/**--------------------------------------------
|
|
43
|
+
* Other
|
|
44
|
+
*---------------------------------------------**/
|
|
45
|
+
export type CascaderSelectOption = {
|
|
46
46
|
defaultSelected: string[];
|
|
47
47
|
selectType: "single" | "multiple";
|
|
48
48
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
/**--------------------------------------------
|
|
50
|
+
* Slot
|
|
51
|
+
*---------------------------------------------**/
|
|
52
52
|
export type PanelHeaderSlot = (props: {
|
|
53
53
|
column: CascaderNodeType<any>[];
|
|
54
54
|
showNode: CascaderItem<any> | null;
|
package/lib/utils/data.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CascaderNodeType, FlatDataConfig, GetShowIndicesOption } from '../types/index.js';
|
|
2
2
|
export declare function buildCascaderEngine<T extends Record<string, any>>(data: T[], config: FlatDataConfig): CascaderNodeType<T>[];
|
|
3
|
-
export declare function getCascadeColumns<T>(nodes: CascaderNodeType<T>[],
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
3
|
+
export declare function getCascadeColumns<T>(nodes: CascaderNodeType<T>[], path: number[]): CascaderNodeType<T>[][];
|
|
4
|
+
export declare function changeShowPath<T>(nodes: CascaderNodeType<T>[], option: GetShowIndicesOption): number[];
|
|
5
|
+
export declare function getShowPath<T>(nodes: CascaderNodeType<T>[], targetNode?: CascaderNodeType): number[];
|