|
1 | 1 | import { useStore } from '../store';
|
2 | 2 | import { useExpandsStore } from '../store/Expands';
|
3 | 3 | import { BracketsClose } from '../symbol/';
|
| 4 | +import { type SectionElementResult } from '../store/Section'; |
4 | 5 |
|
5 |
| -interface NestedCloseProps<T extends object> { |
6 |
| - value?: T; |
| 6 | +interface NestedCloseProps<T extends object> extends SectionElementResult<T> { |
7 | 7 | expandKey: string;
|
8 | 8 | level: number;
|
9 |
| - keys?: (string | number)[]; |
10 | 9 | }
|
11 | 10 |
|
12 | 11 | export const NestedClose = <T extends object>(props: NestedCloseProps<T>) => {
|
13 |
| - const { value, expandKey, level, keys = [] } = props; |
| 12 | + const { keyName, value, expandKey, parentValue, level, keys = [] } = props; |
14 | 13 | const expands = useExpandsStore();
|
15 |
| - const isArray = Array.isArray(value); |
16 | 14 | const { collapsed, shouldExpandNodeInitially } = useStore();
|
17 |
| - const isMySet = value instanceof Set; |
18 | 15 | const defaultExpanded =
|
19 | 16 | typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
|
20 | 17 | const isExpanded = expands[expandKey] ?? defaultExpanded;
|
21 |
| - const len = Object.keys(value!).length; |
22 |
| - const shouldExpand = shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level }); |
23 |
| - if (expands[expandKey] === undefined && shouldExpandNodeInitially && !shouldExpand) { |
| 18 | + const shouldExpand = |
| 19 | + shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, { value, keys, level, keyName, parentValue }); |
| 20 | + if (expands[expandKey] === undefined && !!shouldExpand) { |
24 | 21 | return null;
|
25 | 22 | }
|
| 23 | + const len = Object.keys(value!).length; |
26 | 24 | if (isExpanded || len === 0) {
|
27 | 25 | return null;
|
28 | 26 | }
|
29 | 27 | const style: React.CSSProperties = {
|
30 | 28 | paddingLeft: 4,
|
31 | 29 | };
|
| 30 | + const compProps = { keyName, value, keys, parentValue }; |
| 31 | + const isArray = Array.isArray(value); |
| 32 | + const isMySet = value instanceof Set; |
32 | 33 | return (
|
33 | 34 | <div style={style}>
|
34 |
| - <BracketsClose isBrackets={isArray || isMySet} isVisiable={true} /> |
| 35 | + <BracketsClose isBrackets={isArray || isMySet} {...compProps} isVisiable={true} /> |
35 | 36 | </div>
|
36 | 37 | );
|
37 | 38 | };
|
|
0 commit comments