Skip to content

Commit 1106e05

Browse files
imgeorgiusma-efremoff
authored andcommitted
feat(StructuredYson/Cell): add configurable collapse indicators
1 parent eb55175 commit 1106e05

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

src/ReactUnipika/ReactUnipika.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import React from 'react';
33
// @ts-expect-error
44
import unipika from '@gravity-ui/unipika/lib/unipika';
55

6-
import {UnipikaSettings} from '../StructuredYson/types';
6+
import {CollapseIconType, ToolbarProps, UnipikaSettings} from '../StructuredYson/types';
77

8-
import {StructuredYson, ToolbarProps} from '../StructuredYson/StructuredYson';
8+
import {StructuredYson} from '../StructuredYson/StructuredYson';
99
import {cn} from '../utils/classname';
1010

1111
const block = cn('g-ru-react-unipika');
@@ -21,6 +21,7 @@ export interface ReactUnipikaProps {
2121
customLayout?: (args: {toolbar: React.ReactNode; content: React.ReactNode}) => React.ReactNode;
2222
toolbarStickyTop?: number;
2323
renderToolbar?: (props: ToolbarProps) => React.ReactNode;
24+
collapseIconType?: CollapseIconType;
2425
}
2526

2627
const defaultUnipikaSettings = {
@@ -43,6 +44,7 @@ export function ReactUnipika({
4344
customLayout,
4445
toolbarStickyTop = 0,
4546
renderToolbar,
47+
collapseIconType,
4648
}: ReactUnipikaProps) {
4749
const convertedValue = React.useMemo(() => {
4850
// TODO: fix me later
@@ -95,6 +97,7 @@ export function ReactUnipika({
9597
customLayout={customLayout}
9698
toolbarStickyTop={toolbarStickyTop}
9799
renderToolbar={renderToolbar}
100+
collapseIconType={collapseIconType}
98101
/>
99102
) : (
100103
<pre

src/StructuredYson/Cell.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {Button, Icon} from '@gravity-ui/uikit';
44
// @ts-ignore
55
import unipika from '@gravity-ui/unipika/lib/unipika';
66

7-
import {ArrowUpRightFromSquare} from '@gravity-ui/icons';
7+
import {ArrowUpRightFromSquare, ChevronUp, ChevronRight} from '@gravity-ui/icons';
88

9-
import {UnipikaSettings} from './types';
9+
import {CollapseIconType, UnipikaSettings} from './types';
1010
import {BlockType, SearchInfo, UnipikaFlattenTreeItem} from '../utils/flattenUnipika';
1111

1212
import {MultiHighlightedText, MultiHighlightedTextProps} from '../HighlightedText/HighlightedText';
@@ -28,6 +28,7 @@ export interface CellProps {
2828
filter?: string;
2929
index: number;
3030
showFullText: (index: number) => void;
31+
collapseIconType?: CollapseIconType;
3132
}
3233

3334
export const JSON_VALUE_KEY = {
@@ -69,6 +70,7 @@ export function Cell(props: CellProps) {
6970
filter,
7071
showFullText,
7172
index,
73+
collapseIconType,
7274
} = props;
7375

7476
const handleToggleCollapse = React.useCallback(() => {
@@ -90,6 +92,7 @@ export function Cell(props: CellProps) {
9092
collapsed={collapsed}
9193
path={path}
9294
onToggle={handleToggleCollapse}
95+
collapseIconType={collapseIconType}
9396
/>
9497
)}
9598
<Key
@@ -287,14 +290,27 @@ interface ToggleCollapseProps {
287290
collapsed?: boolean;
288291
path?: UnipikaFlattenTreeItem['path'];
289292
onToggle: () => void;
293+
collapseIconType?: CollapseIconType;
290294
}
291295

292296
function ToggleCollapseButton(props: ToggleCollapseProps) {
293-
const {collapsed, onToggle, path} = props;
297+
const {collapsed, onToggle, path, collapseIconType} = props;
298+
299+
// Function to render the appropriate collapse indicator based on type
300+
const renderCollapseIndicator = () => {
301+
switch (collapseIconType) {
302+
case 'chevron':
303+
return <Icon className={'unipika'} data={collapsed ? ChevronRight : ChevronUp} />;
304+
// Future icon types can be added here as new cases
305+
default:
306+
return <span className={'unipika'}>{collapsed ? '[+]' : '[-]'}</span>;
307+
}
308+
};
309+
294310
return (
295311
<span title={path} className={block('collapse')}>
296312
<Button onClick={onToggle} view="flat-secondary" size={'s'}>
297-
<span className={'unipika'}>{collapsed ? '[+]' : '[-]'}</span>
313+
{renderCollapseIndicator()}
298314
</Button>
299315
</span>
300316
);

src/StructuredYson/StructuredYson.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import reduce_ from 'lodash/reduce';
66
// @ts-ignore
77
import unipika from '@gravity-ui/unipika/lib/unipika';
88

9-
import {UnipikaSettings, UnipikaValue} from '../StructuredYson/types';
9+
import {
10+
CollapseIconType,
11+
ToolbarProps,
12+
UnipikaSettings,
13+
UnipikaValue,
14+
} from '../StructuredYson/types';
1015
import {
1116
CollapsedState,
1217
FlattenUnipikaResult,
@@ -26,20 +31,14 @@ import './StructuredYson.scss';
2631

2732
const block = cn('g-ru-structured-yson');
2833

29-
export interface ToolbarProps {
30-
onFilterChange: (filter: string) => void;
31-
onExpandAll: () => void;
32-
onCollapseAll: () => void;
33-
isCollapsed: boolean;
34-
}
35-
3634
interface Props {
3735
value: UnipikaValue;
3836
settings: UnipikaSettings;
3937
extraTools?: React.ReactNode;
4038
customLayout?: (args: {toolbar: React.ReactNode; content: React.ReactNode}) => React.ReactNode;
4139
toolbarStickyTop?: number;
4240
renderToolbar?: (props: ToolbarProps) => React.ReactNode;
41+
collapseIconType?: CollapseIconType;
4342
}
4443

4544
interface State {
@@ -156,6 +155,7 @@ export class StructuredYson extends React.PureComponent<Props, State> {
156155
settings,
157156
filter,
158157
} = this.state;
158+
const {collapseIconType} = this.props;
159159

160160
return (
161161
<Table
@@ -167,6 +167,7 @@ export class StructuredYson extends React.PureComponent<Props, State> {
167167
onToggleCollapse={this.onTogglePathCollapse}
168168
onShowFullText={this.onShowFullText}
169169
scrollToRef={this.tableRef}
170+
collapseIconType={collapseIconType}
170171
/>
171172
);
172173
}

src/StructuredYson/Table.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import {Table as GravityTable, useTable, useWindowRowVirtualizer} from '@gravity-ui/table';
33
import type {ColumnDef, Row} from '@gravity-ui/table/tanstack';
44
import {UnipikaFlattenTreeItem, SearchInfo} from '../utils/flattenUnipika';
5-
import {UnipikaSettings} from '../StructuredYson/types';
5+
import {CollapseIconType, UnipikaSettings} from '../StructuredYson/types';
66
import {asModifier, Cell} from './Cell';
77
import {cn} from '../utils/classname';
88

@@ -19,6 +19,7 @@ export interface TableProps {
1919
onToggleCollapse: (path: string) => void;
2020
onShowFullText: (index: number) => void;
2121
scrollToRef: React.RefObject<null | {scrollToIndex(index: number): void}>;
22+
collapseIconType?: CollapseIconType;
2223
}
2324

2425
export const Table: React.FC<TableProps> = ({
@@ -30,6 +31,7 @@ export const Table: React.FC<TableProps> = ({
3031
onToggleCollapse,
3132
onShowFullText,
3233
scrollToRef,
34+
collapseIconType,
3335
}) => {
3436
const renderCell: ColumnDef<UnipikaFlattenTreeItem>['cell'] = ({row}) => {
3537
const {original, index} = row;
@@ -43,6 +45,7 @@ export const Table: React.FC<TableProps> = ({
4345
filter={filter}
4446
showFullText={onShowFullText}
4547
index={index}
48+
collapseIconType={collapseIconType}
4649
/>
4750
);
4851
};

src/StructuredYson/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export type UnipikaSettings = {
1919
normalizeUrl?: (url?: string) => string;
2020
};
2121

22+
export type CollapseIconType = 'chevron';
23+
24+
export interface ToolbarProps {
25+
onFilterChange: (filter: string) => void;
26+
onExpandAll: () => void;
27+
onCollapseAll: () => void;
28+
isCollapsed: boolean;
29+
}
30+
2231
interface BaseUnipikaValue {
2332
$attributes?: UnipikaMap['$value'];
2433
}

0 commit comments

Comments
 (0)