Skip to content

Commit ab34905

Browse files
committed
feat: add initially collapsed and tests
1 parent 745d69c commit ab34905

8 files changed

+57
-22
lines changed

src/ReactUnipika/ReactUnipika.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ReactUnipikaProps {
2323
renderToolbar?: (props: ToolbarProps) => React.ReactNode;
2424
collapseIconType?: CollapseIconType;
2525
showContainerSize?: boolean;
26+
initiallyCollapsed?: boolean;
2627
}
2728

2829
const defaultUnipikaSettings = {
@@ -47,6 +48,7 @@ export function ReactUnipika({
4748
renderToolbar,
4849
collapseIconType,
4950
showContainerSize,
51+
initiallyCollapsed,
5052
}: ReactUnipikaProps) {
5153
const convertedValue = React.useMemo(() => {
5254
// TODO: fix me later
@@ -101,6 +103,7 @@ export function ReactUnipika({
101103
renderToolbar={renderToolbar}
102104
collapseIconType={collapseIconType}
103105
showContainerSize={showContainerSize}
106+
initiallyCollapsed={initiallyCollapsed}
104107
/>
105108
) : (
106109
<pre
Loading
Loading
Loading
Loading

src/ReactUnipika/__stories__/ReactUnipika.stories.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export const WithContainerSize: StoryObj<ReactUnipikaProps> = {
4545
},
4646
};
4747

48+
export const WithContainerSizeCollapsed: StoryObj<ReactUnipikaProps> = {
49+
args: {
50+
value: data,
51+
showContainerSize: true,
52+
initiallyCollapsed: true,
53+
},
54+
};
55+
4856
export const WithContainerSizeYson: StoryObj<ReactUnipikaProps> = {
4957
args: {
5058
value: data,

src/ReactUnipika/__tests__/ReactUnipika.visual.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,13 @@ test('ReactUnipika: json with container size', async ({mount, expectScreenshot,
8181

8282
await expectScreenshot({component: page});
8383
});
84+
85+
test('ReactUnipika: json with container size collapsed initially', async ({
86+
mount,
87+
expectScreenshot,
88+
page,
89+
}) => {
90+
await mount(<Stories.WithContainerSizeCollapsed />, {width: 1280});
91+
92+
await expectScreenshot({component: page});
93+
});

src/StructuredYson/StructuredYson.tsx

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface Props {
4040
renderToolbar?: (props: ToolbarProps) => React.ReactNode;
4141
collapseIconType?: CollapseIconType;
4242
showContainerSize?: boolean;
43+
initiallyCollapsed?: boolean;
4344
}
4445

4546
interface State {
@@ -100,21 +101,30 @@ export class StructuredYson extends React.PureComponent<Props, State> {
100101
return isEmpty_(res) ? null : res;
101102
}
102103

103-
state: State = {
104-
value: {} as any, // valid value will be provided from getDerivedStateFromProps call
105-
flattenResult: {data: [], searchIndex: {}},
106-
settings: {},
107-
yson: true,
108-
collapsedState: {},
109-
110-
filter: '',
111-
matchIndex: -1,
112-
matchedRows: [],
113-
};
104+
state: State = this.getInitialState();
114105

115106
tableRef: TableProps['scrollToRef'] = React.createRef();
116107
searchRef = React.createRef<HTMLInputElement>();
117108

109+
getInitialState(): State {
110+
const {initiallyCollapsed, value, settings} = this.props;
111+
const collapsedState = initiallyCollapsed
112+
? this.getFullyCollapsedState(value, settings.format !== 'yson')
113+
: {};
114+
115+
return {
116+
value: {} as any, // valid value will be provided from getDerivedStateFromProps call
117+
flattenResult: {data: [], searchIndex: {}},
118+
settings: {},
119+
yson: true,
120+
collapsedState,
121+
122+
filter: '',
123+
matchIndex: -1,
124+
matchedRows: [],
125+
};
126+
}
127+
118128
onTogglePathCollapse = (path: string) => {
119129
const {collapsedState: oldState} = this.state;
120130
const collapsedState = {...oldState};
@@ -181,17 +191,7 @@ export class StructuredYson extends React.PureComponent<Props, State> {
181191

182192
onCollapseAll = () => {
183193
const {value, yson} = this.state;
184-
const {data} = flattenUnipika(value, {isJson: !yson});
185-
const collapsedState = reduce_(
186-
data,
187-
(acc, {path}) => {
188-
if (path) {
189-
acc[path] = true;
190-
}
191-
return acc;
192-
},
193-
{} as CollapsedState,
194-
);
194+
const collapsedState = this.getFullyCollapsedState(value, !yson);
195195
this.updateState({collapsedState});
196196
};
197197

@@ -326,4 +326,18 @@ export class StructuredYson extends React.PureComponent<Props, State> {
326326
</React.Fragment>
327327
);
328328
}
329+
330+
getFullyCollapsedState(value: UnipikaValue, isJson: boolean): CollapsedState {
331+
const {data} = flattenUnipika(value, {isJson});
332+
return reduce_<UnipikaFlattenTreeItem, CollapsedState>(
333+
data,
334+
(acc, {path}) => {
335+
if (path) {
336+
acc[path] = true;
337+
}
338+
return acc;
339+
},
340+
{},
341+
);
342+
}
329343
}

0 commit comments

Comments
 (0)