@@ -73,6 +73,7 @@ export interface ValueViewProps<T extends object>
73
73
enableClipboard : boolean ;
74
74
indentWidth : number ;
75
75
level ?: number ;
76
+ shortenTextAfterLength ?: JsonViewProps < T > [ 'shortenTextAfterLength' ] ;
76
77
namespace ?: Array < string | number > ;
77
78
quotes ?: JsonViewProps < T > [ 'quotes' ] ;
78
79
components ?: JsonViewProps < T > [ 'components' ] ;
@@ -119,6 +120,58 @@ export function getValueString<T>(value: T) {
119
120
return { type, content } ;
120
121
}
121
122
123
+ interface RenderStringValueProps extends React . LabelHTMLAttributes < HTMLLabelElement > {
124
+ color ?: string ;
125
+ href ?: string ;
126
+ style ?: React . CSSProperties ;
127
+ isURL ?: boolean ;
128
+ length ?: number ;
129
+ }
130
+
131
+ const RenderShortenTextValue : FC < PropsWithChildren < RenderStringValueProps > > = ( {
132
+ children,
133
+ length,
134
+ style,
135
+ ...rest
136
+ } ) => {
137
+ const childrenStr = children as string ;
138
+ const [ shorten , setShorten ] = useState ( length && childrenStr . length >= length ) ;
139
+ const click = ( ) => {
140
+ console . log ( shorten ) ;
141
+ if ( length && childrenStr . length <= length ) return setShorten ( false ) ;
142
+ setShorten ( ! shorten ) ;
143
+ } ;
144
+ const text = shorten ? `${ childrenStr . slice ( 0 , length ) } ...` : childrenStr ;
145
+ return (
146
+ < RenderStringValue { ...rest } style = { { ...style , cursor : 'pointer' } } onClick = { click } >
147
+ { text }
148
+ </ RenderStringValue >
149
+ ) ;
150
+ } ;
151
+ RenderShortenTextValue . displayName = 'JVR.RenderShortenTextValue' ;
152
+
153
+ const RenderStringValue : FC < PropsWithChildren < RenderStringValueProps > > = ( {
154
+ color,
155
+ style,
156
+ isURL,
157
+ href,
158
+ children,
159
+ ...rest
160
+ } ) => {
161
+ return (
162
+ < Label color = { color } style = { style } { ...rest } className = "w-rjv-value" >
163
+ { isURL && (
164
+ < a href = { href } style = { { color } } target = "_blank" rel = "noopener noreferrer" >
165
+ { children }
166
+ </ a >
167
+ ) }
168
+ { ! isURL && children }
169
+ </ Label >
170
+ ) ;
171
+ } ;
172
+
173
+ RenderStringValue . displayName = 'JVR.RenderStringValue' ;
174
+
122
175
export function ValueView < T extends object > ( props : ValueViewProps < T > ) {
123
176
const {
124
177
value,
@@ -136,6 +189,7 @@ export function ValueView<T extends object>(props: ValueViewProps<T>) {
136
189
enableClipboard,
137
190
displayObjectSize,
138
191
displayDataTypes,
192
+ shortenTextAfterLength,
139
193
...reset
140
194
} = props ;
141
195
@@ -192,26 +246,30 @@ export function ValueView<T extends object>(props: ValueViewProps<T>) {
192
246
content,
193
247
children : content ,
194
248
} ) ;
195
- const valueView = reView ? (
196
- reView
197
- ) : (
198
- < Label color = { color } style = { style } className = "w-rjv-value" >
199
- { isURL ? (
200
- < a href = { value . href } style = { { color } } target = "_blank" rel = "noopener noreferrer" >
201
- { content }
202
- </ a >
203
- ) : (
204
- content
205
- ) }
206
- </ Label >
207
- ) ;
249
+
250
+ const valueView =
251
+ shortenTextAfterLength && type === 'string' ? (
252
+ < RenderShortenTextValue
253
+ color = { color }
254
+ href = { isURL ? value . href : '' }
255
+ style = { style }
256
+ isURL = { isURL }
257
+ length = { shortenTextAfterLength }
258
+ >
259
+ { content }
260
+ </ RenderShortenTextValue >
261
+ ) : (
262
+ < RenderStringValue color = { color } href = { isURL ? value . href : '' } style = { style } isURL = { isURL } >
263
+ { content }
264
+ </ RenderStringValue >
265
+ ) ;
208
266
return (
209
267
< Line { ...eventProps } >
210
268
< Label { ...reset } ref = { null } >
211
269
{ renderKey }
212
270
< Colon />
213
271
{ typeView }
214
- { valueView }
272
+ { reView ? reView : valueView }
215
273
{ tools }
216
274
</ Label >
217
275
</ Line >
0 commit comments