Skip to content

Commit 295f179

Browse files
committed
feat: support for the URL of value.
1 parent 1430002 commit 295f179

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

core/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,62 @@ export default function Demo() {
322322
}
323323
```
324324

325+
**Support for the URL(opens in a new tab) API.**
326+
327+
```tsx mdx:preview
328+
import React from 'react';
329+
import JsonView from '@uiw/react-json-view';
330+
331+
export default function Demo() {
332+
return (
333+
<JsonView
334+
value={{
335+
url: new URL('https://example.com?t=12'),
336+
urlStr: "https://example.com",
337+
github: "https://example.com",
338+
}}
339+
style={{
340+
'--w-rjv-background-color': '#ffffff',
341+
}}
342+
/>
343+
)
344+
}
345+
```
346+
347+
```tsx mdx:preview
348+
import React from 'react';
349+
import JsonView from '@uiw/react-json-view';
350+
351+
function value({ type, children, value, ...props }) {
352+
if (value instanceof URL) {
353+
return (
354+
<span {...props}>
355+
<a href={value.href} target="_blank" rel="noopener noreferrer">
356+
{children}
357+
</a>
358+
&nbsp;Open URL
359+
</span>
360+
);
361+
}
362+
}
363+
364+
export default function Demo() {
365+
return (
366+
<JsonView
367+
value={{
368+
url: new URL('https://example.com?t=12'),
369+
urlStr: "https://example.com",
370+
github: "https://example.com",
371+
}}
372+
components={{ value }}
373+
style={{
374+
'--w-rjv-background-color': '#ffffff',
375+
}}
376+
/>
377+
)
378+
}
379+
```
380+
325381
## Highlight Updates
326382

327383
```tsx mdx:preview

core/src/value.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export interface ValueViewProps<T>
6363
indentWidth: number;
6464
renderKey?: JSX.Element;
6565
renderBraces?: MetaProps['render'];
66-
renderValue?: (props: React.HTMLAttributes<HTMLSpanElement> & { type: TypeProps['type'] }) => JSX.Element;
66+
renderValue?: (props: React.HTMLAttributes<HTMLSpanElement> & { type: TypeProps['type']; value?: T; }) => JSX.Element;
6767
}
6868

6969
export function ValueView<T = object>(props: ValueViewProps<T>) {
@@ -101,6 +101,11 @@ export function ValueView<T = object>(props: ValueViewProps<T>) {
101101
type = 'bigint';
102102
content = `${value}n`;
103103
}
104+
const isURL = value instanceof URL;
105+
if (isURL) {
106+
type = 'string';
107+
content = `"${value.href}"`;
108+
}
104109
if (typeof value === 'string') {
105110
content = `"${value}"`;
106111
}
@@ -126,12 +131,13 @@ export function ValueView<T = object>(props: ValueViewProps<T>) {
126131
className: 'w-rjv-value',
127132
style: { color, ...style },
128133
type,
134+
value,
129135
content,
130-
children: (value as string),
136+
children: content,
131137
});
132138
const valueView = reView ? reView : (
133139
<Label color={color} style={style} className="w-rjv-value">
134-
{content}
140+
{isURL ? <a href={value.href} target="_blank" rel="noopener noreferrer">{content}</a> : content}
135141
</Label>
136142
);
137143
return (

0 commit comments

Comments
 (0)