@zidian-primitive/cascader 0.1.1 → 0.1.3
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/README.md +3 -3
- package/esm/components/node/CascaderNode.js +17 -2
- package/esm/hooks/useCascader.js +25 -26
- package/esm/index.js +1 -1
- package/esm/utils/data.js +24 -11
- package/lib/components/node/CascaderNode.d.ts +1 -0
- package/lib/hooks/useCascader.d.ts +5 -4
- package/lib/types/index.d.ts +16 -15
- package/lib/utils/data.d.ts +2 -2
- package/package.json +2 -2
- package/src/index.ts +8 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @zidian-
|
|
1
|
+
# @zidian-primitive/cascader
|
|
2
2
|
|
|
3
3
|
A flexible cascader component library built with component composition pattern, providing complete UI and logic for hierarchical selection.
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@ A flexible cascader component library built with component composition pattern,
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install @zidian-
|
|
18
|
+
npm install @zidian-primitive/cascader
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## Quick Start
|
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
CascaderPanelIndicator,
|
|
31
31
|
useCascader,
|
|
32
32
|
type CascaderItem
|
|
33
|
-
} from '@zidian-
|
|
33
|
+
} from '@zidian-primitive/cascader'
|
|
34
34
|
|
|
35
35
|
const data = [
|
|
36
36
|
{ category: 'Electronics', type: 'Phone', brand: 'iPhone' },
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useContext } from 'react';
|
|
2
|
+
import { useContext, useRef, 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,28 @@ const CascaderNode = (props) => {
|
|
|
13
14
|
} = props;
|
|
14
15
|
const { onSelectChange } = useContext(CascaderContext);
|
|
15
16
|
const { showIndicator, registerNode } = useContext(CascaderPanelContext);
|
|
17
|
+
const nodeRef = useRef(null);
|
|
16
18
|
const handleClick = (event) => {
|
|
17
19
|
if (isLeaf) {
|
|
18
20
|
if (onClick) onClick(node);
|
|
21
|
+
if (onTrigger) onTrigger(node.level, node.indices);
|
|
19
22
|
} else {
|
|
20
23
|
if (onTrigger) onTrigger(node.level, node.indices);
|
|
21
24
|
}
|
|
22
25
|
};
|
|
23
|
-
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (isActive && nodeRef.current) {
|
|
28
|
+
nodeRef.current.scrollIntoView({
|
|
29
|
+
behavior: "smooth",
|
|
30
|
+
block: "nearest",
|
|
31
|
+
inline: "start"
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}, [isActive]);
|
|
35
|
+
return /* @__PURE__ */ jsx("div", { ref: (el) => {
|
|
36
|
+
if (showIndicator) registerNode(node.indices, el);
|
|
37
|
+
nodeRef.current = el;
|
|
38
|
+
}, onClick: handleClick, ...elementProps, children: render ? render({ node, onSelectChange }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
24
39
|
/* @__PURE__ */ jsx("span", { style: { flex: 1 }, children: node.label }),
|
|
25
40
|
!isLeaf && /* @__PURE__ */ jsx(
|
|
26
41
|
"svg",
|
package/esm/hooks/useCascader.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { useMemo, useState } from 'react';
|
|
2
|
-
import { buildCascaderEngine,
|
|
1
|
+
import { useMemo, 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,45 @@ 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 logicNodeData = useMemo(() => {
|
|
20
|
+
const statusArray = new Uint8Array(sourceNodeData.length);
|
|
21
21
|
if (selectOption?.defaultSelected) {
|
|
22
|
-
const defaultIndices =
|
|
22
|
+
const defaultIndices = sourceNodeData.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(sourceNodeData, statusArray, defaultIndices, 1);
|
|
28
28
|
}
|
|
29
29
|
return statusArray;
|
|
30
|
-
}, [
|
|
31
|
-
const [checkStatusArray, setCheckStatusArray] = useState(
|
|
32
|
-
const [
|
|
33
|
-
const columns = useMemo(() => getCascadeColumns(
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
{
|
|
38
|
-
oldShowIndices: showIndices,
|
|
39
|
-
actionIndices: {
|
|
40
|
-
depth,
|
|
41
|
-
nodeIdx: index
|
|
42
|
-
}
|
|
43
|
-
}
|
|
30
|
+
}, [sourceNodeData, selectOption?.defaultSelected]);
|
|
31
|
+
const [checkStatusArray, setCheckStatusArray] = useState(logicNodeData);
|
|
32
|
+
const [path, setPath] = useState(getShowPath(sourceNodeData, sourceNodeData.find((node) => curShow === node.value || curShow === node.label)));
|
|
33
|
+
const columns = useMemo(() => getCascadeColumns(sourceNodeData, path), [sourceNodeData, path]);
|
|
34
|
+
const handleChangePath = (depth, index) => {
|
|
35
|
+
setPath(changeShowPath(
|
|
36
|
+
sourceNodeData,
|
|
37
|
+
{ oldPath: path, action: { depth, nodeIdx: index } }
|
|
44
38
|
));
|
|
45
39
|
};
|
|
46
40
|
const handleChangeCheckStatus = (nodeIdx) => {
|
|
47
|
-
const newStatusArray = setNodeCheckStatus(
|
|
41
|
+
const newStatusArray = setNodeCheckStatus(sourceNodeData, checkStatusArray, nodeIdx);
|
|
48
42
|
setCheckStatusArray(newStatusArray);
|
|
49
43
|
};
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (curShow || curShow === 0) {
|
|
46
|
+
setPath(getShowPath(sourceNodeData, sourceNodeData.find((node) => curShow === node.value || curShow === node.label)));
|
|
47
|
+
}
|
|
48
|
+
}, [curShow]);
|
|
50
49
|
return {
|
|
51
|
-
nodes:
|
|
50
|
+
nodes: sourceNodeData,
|
|
52
51
|
columns,
|
|
53
|
-
|
|
52
|
+
path,
|
|
54
53
|
checkStatusArray,
|
|
55
|
-
|
|
54
|
+
handleChangePath,
|
|
56
55
|
handleChangeCheckStatus
|
|
57
56
|
};
|
|
58
57
|
}
|
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
|
@@ -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;
|
|
@@ -17,20 +17,21 @@ export interface CascaderNodeType<T = any> {
|
|
|
17
17
|
export interface CascaderItem<T> {
|
|
18
18
|
indices: number;
|
|
19
19
|
level: number;
|
|
20
|
+
field: string;
|
|
20
21
|
label: string;
|
|
21
22
|
value: string | number;
|
|
22
23
|
originData?: T;
|
|
23
24
|
checkStatus?: CheckStatus;
|
|
24
25
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
/**--------------------------------------------
|
|
27
|
+
* Util
|
|
28
|
+
*---------------------------------------------**/
|
|
28
29
|
export type FlatDataConfig = {
|
|
29
30
|
levels: (string | [string, string])[];
|
|
30
31
|
};
|
|
31
32
|
export type GetShowIndicesOption = {
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
oldPath: number[];
|
|
34
|
+
action: {
|
|
34
35
|
depth: number;
|
|
35
36
|
nodeIdx: number;
|
|
36
37
|
};
|
|
@@ -38,16 +39,16 @@ export type GetShowIndicesOption = {
|
|
|
38
39
|
export type GetCascaderColumnsOption = {
|
|
39
40
|
checkStatusArray?: Uint8Array<ArrayBufferLike>;
|
|
40
41
|
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export type
|
|
42
|
+
/**--------------------------------------------
|
|
43
|
+
* Other
|
|
44
|
+
*---------------------------------------------**/
|
|
45
|
+
export type CascaderSelectOption = {
|
|
45
46
|
defaultSelected: string[];
|
|
46
47
|
selectType: "single" | "multiple";
|
|
47
48
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
/**--------------------------------------------
|
|
50
|
+
* Slot
|
|
51
|
+
*---------------------------------------------**/
|
|
51
52
|
export type PanelHeaderSlot = (props: {
|
|
52
53
|
column: CascaderNodeType<any>[];
|
|
53
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
3
|
export declare function getCascadeColumns<T>(nodes: CascaderNodeType<T>[], showIndices: number[]): CascaderNodeType<T>[][];
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
4
|
+
export declare function changeShowPath<T>(nodes: CascaderNodeType<T>[], option: GetShowIndicesOption): number[];
|
|
5
|
+
export declare function getShowPath<T>(nodes: CascaderNodeType<T>[], targetNode?: CascaderNodeType): number[];
|
package/package.json
CHANGED