Skip to content

Commit e951991

Browse files
committed
feat: add Row component. (#27)
1 parent 71b142d commit e951991

File tree

6 files changed

+113
-13
lines changed

6 files changed

+113
-13
lines changed

core/README.md

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export default function Demo() {
363363
}
364364
```
365365

366-
Supports certain partial customizations such as: `<Copied />`, `<CountInfo />`, `<CountInfoExtra />`, `<Ellipsis />`, `<KeyName />`
366+
Supports certain partial customizations such as: `<Copied />`, `<CountInfo />`, `<CountInfoExtra />`, `<Ellipsis />`, `<KeyName />`, `<Row />`
367367

368368
```tsx mdx:preview
369369
import React, { Fragment } from 'react';
@@ -580,6 +580,52 @@ Passing **as="tagName"** will automatically infer the type.
580580
/>
581581
```
582582

583+
Add a click event on the data row
584+
585+
```tsx mdx:preview
586+
import React from 'react';
587+
import JsonView from '@uiw/react-json-view';
588+
589+
export default function Demo() {
590+
return (
591+
<JsonView
592+
style={{
593+
'--w-rjv-background-color': '#ffffff',
594+
}}
595+
value={{
596+
name: 'John',
597+
age: 30,
598+
hobbies: ['reading', 'coding', 'swimming'],
599+
address: {
600+
street: '123 Main St',
601+
city: 'New York',
602+
country: {
603+
name: 'Main ',
604+
codex: '123'
605+
}
606+
}
607+
}}
608+
>
609+
<JsonView.Row
610+
as="div"
611+
render={(props, { keyName, value, parentValue }) => {
612+
return (
613+
<div
614+
{...props}
615+
onClick={() => {
616+
console.log("keyName", keyName)
617+
console.log("value", value)
618+
console.log("parentValue", parentValue)
619+
}}
620+
/>
621+
)
622+
}}
623+
/>
624+
</JsonView>
625+
)
626+
}
627+
```
628+
583629
## Highlight Updates
584630

585631
```tsx mdx:preview
@@ -777,6 +823,7 @@ import { Arrow } from './symbol/Arrow';
777823
import { Colon } from './symbol/Colon';
778824
import { Quote } from './symbol/Quote';
779825
import { ValueQuote } from './symbol/ValueQuote';
826+
780827
import { Bigint } from './types/Bigint';
781828
import { Date } from './types/Date';
782829
import { False } from './types/False';
@@ -790,13 +837,24 @@ import { StringText } from './types/String';
790837
import { True } from './types/True';
791838
import { Undefined } from './types/Undefined';
792839
import { Url } from './types/Url';
840+
793841
import { Copied } from './section/Copied';
794842
import { CountInfo } from './section/CountInfo';
795843
import { CountInfoExtra } from './section/CountInfoExtra';
796844
import { Ellipsis } from './section/Ellipsis';
797845
import { KeyName } from './section/KeyName';
846+
import { Row } from './section/Row';
798847

799848
type JsonViewComponent = React.FC<React.PropsWithRef<JsonViewProps<object>>> & {
849+
BraceLeft: typeof BraceLeft;
850+
BraceRight: typeof BraceRight;
851+
BracketsLeft: typeof BracketsLeft;
852+
BracketsRight: typeof BracketsRight;
853+
Arrow: typeof Arrow;
854+
Colon: typeof Colon;
855+
Quote: typeof Quote;
856+
ValueQuote: typeof ValueQuote;
857+
800858
Bigint: typeof Bigint;
801859
Date: typeof Date;
802860
False: typeof False;
@@ -810,19 +868,13 @@ type JsonViewComponent = React.FC<React.PropsWithRef<JsonViewProps<object>>> & {
810868
True: typeof True;
811869
Undefined: typeof Undefined;
812870
Url: typeof Url;
813-
BraceLeft: typeof BraceLeft;
814-
BraceRight: typeof BraceRight;
815-
BracketsLeft: typeof BracketsLeft;
816-
BracketsRight: typeof BracketsRight;
817-
Colon: typeof Colon;
818-
Ellipsis: typeof Ellipsis;
819-
Quote: typeof Quote;
820-
ValueQuote: typeof ValueQuote;
821-
Arrow: typeof Arrow;
871+
822872
Copied: typeof Copied;
823873
CountInfo: typeof CountInfo;
824874
CountInfoExtra: typeof CountInfoExtra;
875+
Ellipsis: typeof Ellipsis;
825876
KeyName: typeof KeyName;
877+
Row: typeof Row;
826878
};
827879
declare const JsonView: JsonViewComponent;
828880
export default JsonView;

core/src/comps/KeyValues.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useExpandsStore } from '../store/Expands';
44
import { useShowToolsDispatch } from '../store/ShowTools';
55
import { Value } from './Value';
66
import { KeyNameComp } from '../section/KeyName';
7+
import { RowComp } from '../section/Row';
78
import { Container } from '../Container';
89
import { Quote, Colon } from '../symbol';
910
import { useHighlight } from '../utils/useHighlight';
@@ -101,10 +102,10 @@ export const KeyValuesItem = <T extends object>(props: KeyValuesProps<T>) => {
101102
onMouseLeave: () => dispatch({ [subkeyid]: false }),
102103
};
103104
return (
104-
<div className="w-rjv-line" {...reset}>
105+
<RowComp className="w-rjv-line" value={value} keyName={keyName} parentValue={parentValue} {...reset}>
105106
<KayName keyName={keyName} value={value} parentValue={parentValue} />
106107
<Value keyName={keyName!} value={value} expandKey={subkeyid} />
107-
</div>
108+
</RowComp>
108109
);
109110
};
110111

core/src/editor/store.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, PropsWithChildren, createContext, useContext, useReducer } from 'react';
1+
import { createContext, useContext, useReducer } from 'react';
22

33
type InitialState = {
44
/**

core/src/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CountInfo } from './section/CountInfo';
3030
import { CountInfoExtra } from './section/CountInfoExtra';
3131
import { Ellipsis } from './section/Ellipsis';
3232
import { KeyName } from './section/KeyName';
33+
import { Row } from './section/Row';
3334

3435
export * from './store';
3536
export * from './store/Expands';
@@ -96,6 +97,7 @@ type JsonViewComponent = React.FC<React.PropsWithRef<JsonViewProps<object>>> & {
9697
CountInfo: typeof CountInfo;
9798
CountInfoExtra: typeof CountInfoExtra;
9899
KeyName: typeof KeyName;
100+
Row: typeof Row;
99101
};
100102

101103
const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<object>>((props, ref) => {
@@ -173,6 +175,7 @@ JsonView.Copied = Copied;
173175
JsonView.CountInfo = CountInfo;
174176
JsonView.CountInfoExtra = CountInfoExtra;
175177
JsonView.KeyName = KeyName;
178+
JsonView.Row = Row;
176179

177180
JsonView.displayName = 'JVR.JsonView';
178181

core/src/section/Row.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { type TagType } from '../store/Types';
2+
import { type SectionElement, useSectionStore } from '../store/Section';
3+
import { useSectionRender } from '../utils/useRender';
4+
5+
export const Row = <K extends TagType>(props: SectionElement<K>) => {
6+
const { Row: Comp = {} } = useSectionStore();
7+
useSectionRender(Comp, props, 'Row');
8+
return null;
9+
};
10+
11+
Row.displayName = 'JVR.Row';
12+
13+
export interface RowCompProps<T extends object> extends React.HTMLAttributes<HTMLDivElement> {
14+
value?: T;
15+
keyName?: string | number;
16+
parentValue?: unknown;
17+
}
18+
19+
export const RowComp = <T extends object>({
20+
children,
21+
value,
22+
keyName = '',
23+
parentValue,
24+
...other
25+
}: React.PropsWithChildren<RowCompProps<T>>) => {
26+
const { Row: Comp = {} } = useSectionStore();
27+
const { as, render, children: _, ...reset } = Comp;
28+
const Elm = as || 'div';
29+
const child =
30+
render && typeof render === 'function' && render({ ...other, ...reset, children }, { value, keyName, parentValue });
31+
if (child) return child;
32+
return (
33+
<Elm {...other} {...reset}>
34+
{children}
35+
</Elm>
36+
);
37+
};
38+
39+
RowComp.displayName = 'JVR.RowComp';

core/src/store/Section.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type InitialState<T extends TagType> = {
1616
CountInfo?: SectionElement<T>;
1717
CountInfoExtra?: SectionElement<T>;
1818
Ellipsis?: SectionElement<T>;
19+
Row?: SectionElement<T>;
1920
KeyName?: SectionElement<T>;
2021
};
2122

@@ -57,6 +58,10 @@ const initialState: InitialState<TagType> = {
5758
className: 'w-rjv-ellipsis',
5859
children: '...',
5960
},
61+
Row: {
62+
as: 'div',
63+
className: 'w-rjv-line',
64+
},
6065
KeyName: {
6166
as: 'span',
6267
className: 'w-rjv-object-key',

0 commit comments

Comments
 (0)