Skip to content

Commit b37638c

Browse files
committed
feat: export useHighlight hook.
1 parent 01733f6 commit b37638c

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

core/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,9 @@ export default function Demo() {
452452
## Editor JSON
453453

454454
```tsx mdx:preview
455-
import React from 'react';
455+
import React, { useRef } from 'react';
456456
import JsonViewEditor from '@uiw/react-json-view/editor';
457+
import { type SemicolonProps, useHighlight } from '@uiw/react-json-view';
457458

458459
const object = {
459460
string: 'Lorem ipsum dolor sit amet',
@@ -472,12 +473,14 @@ const object = {
472473
nestedArray: [ [1, 2], [3, 4], { a: 1} ],
473474
}
474475

475-
const ObjectKey = ({ value, keyName, parentName, parentValue, ...reset }) => {
476+
const ObjectKey: SemicolonProps['render'] = ({ value, keyName, parentName, ...props }) => {
477+
const $edit = useRef<HTMLSpanElement & HTMLModElement>(null);
478+
useHighlight({ value, highlightUpdates: true, highlightContainer: $edit });
476479
if (keyName === 'integer' && typeof value === 'number' && value > 40) {
477-
return <del {...reset} />
480+
return <del {...props} ref={$edit} />;
478481
}
479-
return <span {...reset} />
480-
};
482+
return <span {...props} ref={$edit} />;
483+
}
481484

482485
export default function Demo() {
483486
return (

core/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { CountInfoExtraProps } from './editor/countInfoExtra';
1010

1111
export * from './node';
1212
export * from './value';
13+
export * from './semicolon';
1314

1415
export interface CountInfoProps {
1516
count: number;

www/src/example/default.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Fragment, useState, useEffect } from 'react';
2-
import JsonView, { JsonViewProps } from '@uiw/react-json-view';
1+
import { Fragment, useState, useEffect, useRef } from 'react';
2+
import JsonView, { JsonViewProps, useHighlight, SemicolonProps } from '@uiw/react-json-view';
33
import { styled } from 'styled-components';
44
import { lightTheme } from '@uiw/react-json-view/light';
55
import { darkTheme } from '@uiw/react-json-view/dark';
@@ -16,7 +16,7 @@ function aPlusB(a: number, b: number) {
1616
}
1717
const example = {
1818
avatar,
19-
string: 'Lorem ipsum dolor sit amet',
19+
string: 'Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet',
2020
integer: 42,
2121
float: 114.514,
2222
// @ts-ignore
@@ -54,6 +54,15 @@ const Options = styled.div`
5454
grid-template-columns: 50% 60%;
5555
`;
5656

57+
const ObjectKey: SemicolonProps['render'] = ({ value, keyName, parentName, ...props }) => {
58+
const $edit = useRef<HTMLSpanElement & HTMLModElement>(null);
59+
useHighlight({ value, highlightUpdates: true, highlightContainer: $edit });
60+
if (keyName === 'integer' && typeof value === 'number' && value > 40) {
61+
return <del {...props} ref={$edit} />;
62+
}
63+
return <span {...props} ref={$edit} />;
64+
};
65+
5766
export function Example() {
5867
const [indentWidth, setIndentWidth] = useState(15);
5968
const [theme, setTheme] = useState<React.CSSProperties>(lightTheme as React.CSSProperties);
@@ -91,12 +100,7 @@ export function Example() {
91100
style={{ ...theme, padding: 6, borderRadius: 6 }}
92101
collapsed={collapsed}
93102
components={{
94-
objectKey: ({ value, keyName, parentName, ...props }) => {
95-
if (keyName === 'integer' && typeof value === 'number' && value > 40) {
96-
return <del {...props} />;
97-
}
98-
return <span {...props} />;
99-
},
103+
objectKey: ObjectKey,
100104
}}
101105
/>
102106
<Options>

0 commit comments

Comments
 (0)