diff --git a/package.json b/package.json index 53ca7f4..726e921 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,15 @@ "module": "build/esm/index.js", "types": "types/src/index.d.ts", "exports": { - ".": { - "types": "./types/src/index.d.ts", - "require": "./build/cjs/index.js", - "import": "./build/esm/index.js" + "./window-scroll": { + "types": "./types/src/window-scroll.d.ts", + "require": "./build/cjs/window-scroll.js", + "import": "./build/esm/window-scroll.js" + }, + "./container-scroll": { + "types": "./types/src/container-scroll.d.ts", + "require": "./build/cjs/container-scroll.js", + "import": "./build/esm/container-scroll.js" } }, "files": [ diff --git a/src/ReactUnipika/ReactUnipika.tsx b/src/ReactUnipika/ReactUnipika.tsx deleted file mode 100644 index 4fe6460..0000000 --- a/src/ReactUnipika/ReactUnipika.tsx +++ /dev/null @@ -1,143 +0,0 @@ -import React from 'react'; - -// @ts-expect-error -import unipika from '@gravity-ui/unipika/lib/unipika'; - -import {CollapseIconType, ToolbarProps, UnipikaSettings} from '../StructuredYson/types'; - -import {StructuredYson} from '../StructuredYson/StructuredYson'; -import {cn} from '../utils/classname'; - -const block = cn('g-ru-react-unipika'); - -export interface ReactUnipikaProps { - settings?: UnipikaSettings; - value: any; - inline?: boolean; - children?: React.ReactNode; - extraTools?: React.ReactNode; - virtualized?: boolean; - className?: string; - customLayout?: (args: {toolbar: React.ReactNode; content: React.ReactNode}) => React.ReactNode; - toolbarStickyTop?: number; - renderToolbar?: (props: ToolbarProps) => React.ReactNode; - collapseIconType?: CollapseIconType; - showContainerSize?: boolean; - initiallyCollapsed?: boolean; - caseInsensitiveSearch?: boolean; - renderError?: (error: unknown) => React.ReactNode; -} - -const defaultUnipikaSettings = { - asHTML: true, - format: 'json', - compact: false, - escapeWhitespace: true, - showDecoded: true, - binaryAsHex: true, -}; - -export function ReactUnipika({ - value, - settings = defaultUnipikaSettings, - inline = false, - children, - virtualized = true, - extraTools, - className, - customLayout, - toolbarStickyTop = 0, - renderToolbar, - collapseIconType, - showContainerSize, - initiallyCollapsed, - caseInsensitiveSearch, - renderError, -}: ReactUnipikaProps) { - const {convertedValue, error} = React.useMemo(() => { - try { - // TODO: fix me later - // The call is required because unipika.format() applies default values to a passed settings inplace. - // We have to leave this call without it the behaviour will be broken. - if (settings.format === 'raw-json') { - unipika.formatRaw(value, settings); - } else { - unipika.formatFromYSON(value, settings); - } - - if (value === undefined) { - return ''; - } - - if (settings.format === 'raw-json') { - return unipika.converters.raw(value, settings); - } - - return {convertedValue: unipika.converters.yson(value, settings)}; - } catch (error) { - return {error}; - } - }, [value, settings]); - - const classes = block( - { - inline: inline && 'yes', - }, - className, - ); - - if (error) { - return renderError?.(error); - } - - function getFormattedTitle() { - if (!inline) { - return undefined; - } - - const titleSettings = Object.assign({}, settings, {asHTML: false}); - return unipika.format(convertedValue, titleSettings); - } - - function getFormattedValue() { - return unipika.format(convertedValue, settings); - } - - return settings.asHTML ? ( -
{ + renderVirtualized: (props: P, convertedValue: any) => React.ReactNode; +} + +export function withReactUnipikaBase
( + config: WithReactUnipikaBaseProps
, +) { + return function ReactUnipikaBaseComponent(props: P) { + const { + value, + settings = defaultUnipikaSettings, + inline = false, + children, + virtualized = true, + className, + renderError, + } = props; + + const {convertedValue, error} = React.useMemo( + () => convertValue(value, settings), + [value, settings], + ); + + const getFormattedTitle = React.useCallback(() => { + if (!inline) { + return undefined; + } + + const titleSettings = Object.assign({}, settings, {asHTML: false}); + return unipika.format(convertedValue, titleSettings); + }, [inline, settings, convertedValue]); + + const getFormattedValue = React.useCallback(() => { + return unipika.format(convertedValue, settings); + }, [convertedValue, settings]); + + const classes = block( + { + inline: inline && 'yes', + }, + className, + ); + + if (error) { + return renderError?.(error); + } + + return settings.asHTML ? ( +
{
+ static getDerivedStateFromProps(props: StructuredYsonBaseProps, state: State) {
const {
value: prevValue,
settings: prevSettings,
@@ -212,30 +214,8 @@ export class StructuredYson extends React.PureComponent
- );
- }
onExpandAll = () => {
this.updateState({collapsedState: {}}, () => {
this.onNextMatch(null, 0);
diff --git a/src/StructuredYson/StructuredYsonContainerScroll.tsx b/src/StructuredYson/StructuredYsonContainerScroll.tsx
new file mode 100644
index 0000000..1c90ed8
--- /dev/null
+++ b/src/StructuredYson/StructuredYsonContainerScroll.tsx
@@ -0,0 +1,36 @@
+import React from 'react';
+
+import {StructuredYsonBase, StructuredYsonBaseProps} from './StructuredYsonBase';
+import {Table} from './Table/TableContainerScroll';
+
+export interface StructuredYsonContainerScrollProps extends StructuredYsonBaseProps {
+ scrollContainerRef: React.RefObject
+ );
+ }
+}
diff --git a/src/StructuredYson/StructuredYsonWindowScroll.tsx b/src/StructuredYson/StructuredYsonWindowScroll.tsx
new file mode 100644
index 0000000..60acc90
--- /dev/null
+++ b/src/StructuredYson/StructuredYsonWindowScroll.tsx
@@ -0,0 +1,31 @@
+import React from 'react';
+
+import {StructuredYsonBase, StructuredYsonBaseProps} from './StructuredYsonBase';
+import {Table} from './Table/TableWindowScroll';
+
+export class StructuredYsonWindowScroll extends StructuredYsonBase
+ );
+ }
+}
diff --git a/src/StructuredYson/Table.tsx b/src/StructuredYson/Table.tsx
deleted file mode 100644
index 5a2ba34..0000000
--- a/src/StructuredYson/Table.tsx
+++ /dev/null
@@ -1,99 +0,0 @@
-import React from 'react';
-import {Table as GravityTable, useTable, useWindowRowVirtualizer} from '@gravity-ui/table';
-import type {ColumnDef, Row} from '@gravity-ui/table/tanstack';
-import {UnipikaFlattenTreeItem, SearchInfo} from '../utils/flattenUnipika';
-import {CollapseIconType, UnipikaSettings} from '../StructuredYson/types';
-import {asModifier, Cell} from './Cell';
-import {cn} from '../utils/classname';
-
-import './Table.scss';
-
-const block = cn('g-ru-table');
-
-export interface TableProps {
- data: UnipikaFlattenTreeItem[];
- searchIndex: Record