Skip to content

Commit bfa7e92

Browse files
committed
feat(StructuredYson/Cell): add configurable collapse indicators
1 parent 76561c2 commit bfa7e92

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

src/ReactUnipika/ReactUnipika.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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, UnipikaSettings} from '../StructuredYson/types';
77

88
import {StructuredYson, ToolbarProps} from '../StructuredYson/StructuredYson';
99
import {cn} from '../utils/classname';
@@ -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 CollapseIconType.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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 {CollapseIconType, UnipikaSettings, UnipikaValue} from '../StructuredYson/types';
1010
import {
1111
CollapsedState,
1212
FlattenUnipikaResult,
@@ -40,6 +40,7 @@ interface Props {
4040
customLayout?: (args: {toolbar: React.ReactNode; content: React.ReactNode}) => React.ReactNode;
4141
toolbarStickyTop?: number;
4242
renderToolbar?: (props: ToolbarProps) => React.ReactNode;
43+
collapseIconType?: CollapseIconType;
4344
}
4445

4546
interface State {
@@ -156,6 +157,7 @@ export class StructuredYson extends React.PureComponent<Props, State> {
156157
settings,
157158
filter,
158159
} = this.state;
160+
const {collapseIconType} = this.props;
159161

160162
return (
161163
<Table
@@ -167,6 +169,7 @@ export class StructuredYson extends React.PureComponent<Props, State> {
167169
onToggleCollapse={this.onTogglePathCollapse}
168170
onShowFullText={this.onShowFullText}
169171
scrollToRef={this.tableRef}
172+
collapseIconType={collapseIconType}
170173
/>
171174
);
172175
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export type UnipikaSettings = {
1919
normalizeUrl?: (url?: string) => string;
2020
};
2121

22+
export enum CollapseIconType {
23+
Chevron = 'chevron',
24+
Text = 'text',
25+
}
26+
2227
interface BaseUnipikaValue {
2328
$attributes?: UnipikaMap['$value'];
2429
}

0 commit comments

Comments
 (0)