@zidian-primitive/cascader 0.1.3 → 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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useContext, useRef, useEffect } 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) => {
|
|
@@ -15,6 +15,12 @@ const CascaderNode = (props) => {
|
|
|
15
15
|
const { onSelectChange } = useContext(CascaderContext);
|
|
16
16
|
const { showIndicator, registerNode } = useContext(CascaderPanelContext);
|
|
17
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]);
|
|
18
24
|
const handleClick = (event) => {
|
|
19
25
|
if (isLeaf) {
|
|
20
26
|
if (onClick) onClick(node);
|
|
@@ -25,17 +31,19 @@ const CascaderNode = (props) => {
|
|
|
25
31
|
};
|
|
26
32
|
useEffect(() => {
|
|
27
33
|
if (isActive && nodeRef.current) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
const timer = requestAnimationFrame(() => {
|
|
35
|
+
if (nodeRef.current) {
|
|
36
|
+
nodeRef.current.scrollIntoView({
|
|
37
|
+
behavior: "smooth",
|
|
38
|
+
block: "nearest",
|
|
39
|
+
inline: "start"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
32
42
|
});
|
|
43
|
+
return () => cancelAnimationFrame(timer);
|
|
33
44
|
}
|
|
34
45
|
}, [isActive]);
|
|
35
|
-
return /* @__PURE__ */ jsx("div", { ref: (
|
|
36
|
-
if (showIndicator) registerNode(node.indices, el);
|
|
37
|
-
nodeRef.current = el;
|
|
38
|
-
}, onClick: handleClick, ...elementProps, children: render ? render({ node, onSelectChange }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
46
|
+
return /* @__PURE__ */ jsx("div", { ref: handleRef, onClick: handleClick, "data-active": isActive, ...elementProps, children: render ? render({ node, onSelectChange }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
39
47
|
/* @__PURE__ */ jsx("span", { style: { flex: 1 }, children: node.label }),
|
|
40
48
|
!isLeaf && /* @__PURE__ */ jsx(
|
|
41
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,4 +1,4 @@
|
|
|
1
|
-
import { useMemo, useState, useEffect } from 'react';
|
|
1
|
+
import { useMemo, useCallback, useState, useEffect } from 'react';
|
|
2
2
|
import { buildCascaderEngine, getShowPath, getCascadeColumns, changeShowPath } from '../utils/data.js';
|
|
3
3
|
import { setMultipleNodeStatus, setNodeCheckStatus } from '../utils/logic.js';
|
|
4
4
|
|
|
@@ -16,21 +16,24 @@ function useCascader(options) {
|
|
|
16
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
|
|
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]);
|
|
34
37
|
const handleChangePath = (depth, index) => {
|
|
35
38
|
setPath(changeShowPath(
|
|
36
39
|
sourceNodeData,
|
|
@@ -42,6 +45,12 @@ function useCascader(options) {
|
|
|
42
45
|
setCheckStatusArray(newStatusArray);
|
|
43
46
|
};
|
|
44
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);
|
|
45
54
|
if (curShow || curShow === 0) {
|
|
46
55
|
setPath(getShowPath(sourceNodeData, sourceNodeData.find((node) => curShow === node.value || curShow === node.label)));
|
|
47
56
|
}
|
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]);
|
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>[],
|
|
3
|
+
export declare function getCascadeColumns<T>(nodes: CascaderNodeType<T>[], path: number[]): CascaderNodeType<T>[][];
|
|
4
4
|
export declare function changeShowPath<T>(nodes: CascaderNodeType<T>[], option: GetShowIndicesOption): number[];
|
|
5
5
|
export declare function getShowPath<T>(nodes: CascaderNodeType<T>[], targetNode?: CascaderNodeType): number[];
|