11import React from 'react' ;
2- import isEqual_ from 'lodash/isEqual' ;
32
43import type { Settings } from '@gravity-ui/react-data-table' ;
54// @ts -expect-error
6- import unipika from '@gravity-ui/unipika' ;
5+ import unipika from '@gravity-ui/unipika/lib/unipika ' ;
76
8- import { UnipikaSettings , UnipikaValue } from '../StructuredYson/types' ;
7+ import { UnipikaSettings } from '../StructuredYson/types' ;
98
109import { StructuredYson } from '../StructuredYson/StructuredYson' ;
11- import { ruCN } from '../utils/classname' ;
10+ import { cn } from '../utils/classname' ;
1211
13- const block = ruCN ( ' react-unipika') ;
12+ const block = cn ( 'g-ru- react-unipika') ;
1413
1514export interface ReactUnipikaProps {
1615 settings ?: UnipikaSettings ;
@@ -25,138 +24,99 @@ export interface ReactUnipikaProps {
2524 toolbarStickyTop ?: number ;
2625}
2726
28- interface State {
29- convertedValue : UnipikaValue ;
30- value : ReactUnipikaProps [ 'value' ] ;
31- settings : ReactUnipikaProps [ 'settings' ] ;
32- }
33-
34- const INITIAL = { } ;
35-
36- export class ReactUnipika extends React . Component < ReactUnipikaProps , State > {
37- static defaultUnipikaSettings = {
38- asHTML : true ,
39- format : 'json' ,
40- compact : false ,
41- escapeWhitespace : true ,
42- showDecoded : true ,
43- binaryAsHex : true ,
44- } ;
45-
46- static defaultProps = {
47- inline : false ,
48- virtualized : true ,
49- settings : ReactUnipika . defaultUnipikaSettings ,
50- toolbarStickyTop : 0 ,
51- } ;
52-
53- static getDerivedStateFromProps ( props : ReactUnipikaProps , state : State ) {
54- const { value : prevValue , settings : prevSettings } = state ;
55- const { value, settings = { } } = props ;
27+ const defaultUnipikaSettings = {
28+ asHTML : true ,
29+ format : 'json' ,
30+ compact : false ,
31+ escapeWhitespace : true ,
32+ showDecoded : true ,
33+ binaryAsHex : true ,
34+ } ;
35+
36+ export function ReactUnipika ( {
37+ value,
38+ settings = defaultUnipikaSettings ,
39+ inline = false ,
40+ children,
41+ virtualized = true ,
42+ extraTools,
43+ className,
44+ tableSettings,
45+ customLayout,
46+ toolbarStickyTop = 0 ,
47+ } : ReactUnipikaProps ) {
48+ const convertedValue = React . useMemo ( ( ) => {
49+ // TODO: fix me later
50+ // The call is required because unipika.format() applies default values to a passed settings inplace.
51+ // We have to leave this call without it the behaviour will be broken.
52+ if ( settings . format === 'raw-json' ) {
53+ unipika . formatRaw ( value , settings ) ;
54+ } else {
55+ unipika . formatFromYSON ( value , settings ) ;
56+ }
5657
57- if (
58- prevValue === INITIAL ||
59- ! isEqual_ ( prevValue , value ) ||
60- ! isEqual_ ( prevSettings , settings )
61- ) {
62- // TODO: fix me later
63- // The call is required because unipika.format() applies default values to a passed settings inplace.
64- // We have to leave this call without it the behaviour will be broken.
65- if ( settings . format === 'raw-json' ) {
66- unipika . formatRaw ( value , settings ) ;
67- } else {
68- unipika . formatFromYSON ( value , settings ) ;
69- }
58+ if ( value === undefined ) {
59+ return '' ;
60+ }
7061
71- return {
72- convertedValue :
73- value === undefined
74- ? ''
75- : settings ! . format === 'raw-json'
76- ? unipika . converters . raw ( value , settings )
77- : unipika . converters . yson ( value , settings ) ,
78- value : value ,
79- settings : settings ,
80- } ;
62+ if ( settings . format === 'raw-json' ) {
63+ return unipika . converters . raw ( value , settings ) ;
8164 }
82- return null ;
83- }
8465
85- state : State = {
86- convertedValue : undefined as any , // getDerivedStateFromProps should provide correct vgitalue for this field
87- value : INITIAL ,
88- settings : { format : '' } ,
89- } ;
66+ return unipika . converters . yson ( value , settings ) ;
67+ } , [ value , settings ] ) ;
68+
69+ const classes = block (
70+ {
71+ inline : inline && 'yes' ,
72+ } ,
73+ className ,
74+ ) ;
9075
91- getFormattedTitle ( ) {
92- const { inline} = this . props ;
76+ function getFormattedTitle ( ) {
9377 if ( ! inline ) {
9478 return undefined ;
9579 }
9680
97- const { convertedValue, settings} = this . state ;
9881 const titleSettings = Object . assign ( { } , settings , { asHTML : false } ) ;
99-
10082 return unipika . format ( convertedValue , titleSettings ) ;
10183 }
10284
103- getFormattedValue ( ) {
104- const { convertedValue, settings} = this . state ;
85+ function getFormattedValue ( ) {
10586 return unipika . format ( convertedValue , settings ) ;
10687 }
10788
108- render ( ) {
109- const {
110- inline,
111- children,
112- virtualized,
113- extraTools,
114- className,
115- tableSettings,
116- customLayout,
117- toolbarStickyTop,
118- } = this . props ;
119- const { convertedValue, settings} = this . state ;
120-
121- const classes = block (
122- {
123- inline : inline && 'yes' ,
124- } ,
125- className ,
126- ) ;
127-
128- return settings ! . asHTML ? (
129- < div className = { classes } title = { this . getFormattedTitle ( ) } dir = "auto" >
130- { virtualized ? (
131- < StructuredYson
132- tableSettings = { tableSettings }
133- value = { convertedValue }
134- settings = { settings ! }
135- extraTools = { extraTools }
136- customLayout = { customLayout }
137- toolbarStickyTop = { toolbarStickyTop }
138- />
139- ) : (
140- < pre
141- className = "unipika"
142- dangerouslySetInnerHTML = { {
143- __html : this . getFormattedValue ( ) ,
144- } }
145- />
146- ) }
147- { children }
148- </ div >
149- ) : (
150- < div
151- className = { classes }
152- title = { this . getFormattedTitle ( ) }
153- dangerouslySetInnerHTML = { {
154- __html : this . getFormattedValue ( ) ,
155- } }
156- dir = "auto"
157- >
158- { children }
159- </ div >
160- ) ;
161- }
89+ return settings . asHTML ? (
90+ < div className = { classes } title = { getFormattedTitle ( ) } dir = "auto" >
91+ { virtualized ? (
92+ < StructuredYson
93+ tableSettings = { tableSettings }
94+ value = { convertedValue }
95+ settings = { settings }
96+ extraTools = { extraTools }
97+ customLayout = { customLayout }
98+ toolbarStickyTop = { toolbarStickyTop }
99+ />
100+ ) : (
101+ < pre
102+ className = "unipika"
103+ dangerouslySetInnerHTML = { {
104+ __html : getFormattedValue ( ) ,
105+ } }
106+ />
107+ ) }
108+ { children }
109+ </ div >
110+ ) : (
111+ < div
112+ className = { classes }
113+ title = { getFormattedTitle ( ) }
114+ dangerouslySetInnerHTML = { {
115+ __html : getFormattedValue ( ) ,
116+ } }
117+ dir = "auto"
118+ >
119+ { children }
120+ </ div >
121+ ) ;
162122}
0 commit comments