1 | import { useMemo } from 'react';
|
2 | export function useColumnsFn(options) {
|
3 | const depth = useMemo(() => {
|
4 | let depth = 0;
|
5 | function traverse(options, currentDepth) {
|
6 | if (currentDepth > depth) depth = currentDepth;
|
7 | const nextDepth = currentDepth + 1;
|
8 | options.forEach(option => {
|
9 | if (option.children) {
|
10 | traverse(option.children, nextDepth);
|
11 | }
|
12 | });
|
13 | }
|
14 | traverse(options, 1);
|
15 | return depth;
|
16 | }, [options]);
|
17 | return selected => {
|
18 | const columns = [];
|
19 | let currentOptions = options;
|
20 | let i = 0;
|
21 | while (true) {
|
22 | columns.push(currentOptions.map(option => ({
|
23 | label: option.label,
|
24 | value: option.value
|
25 | })));
|
26 | const x = selected[i];
|
27 | const targetOptions = currentOptions.find(option => option.value === x);
|
28 | if (!targetOptions || !targetOptions.children) break;
|
29 | currentOptions = targetOptions.children;
|
30 | i++;
|
31 | }
|
32 | while (i < depth - 1) {
|
33 | columns.push([]);
|
34 | i++;
|
35 | }
|
36 | return columns;
|
37 | };
|
38 | } |
\ | No newline at end of file |