From e9519910ca34225c648a2a19c8536e6c3e043ef6 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Mon, 18 Dec 2023 19:32:01 +0800 Subject: [PATCH] feat: add `Row` component. (#27) --- core/README.md | 72 +++++++++++++++++++++++++++++++----- core/src/comps/KeyValues.tsx | 5 ++- core/src/editor/store.tsx | 2 +- core/src/index.tsx | 3 ++ core/src/section/Row.tsx | 39 +++++++++++++++++++ core/src/store/Section.tsx | 5 +++ 6 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 core/src/section/Row.tsx diff --git a/core/README.md b/core/README.md index e9849419..804af762 100644 --- a/core/README.md +++ b/core/README.md @@ -363,7 +363,7 @@ export default function Demo() { } ``` -Supports certain partial customizations such as: ``, ``, ``, ``, `` +Supports certain partial customizations such as: ``, ``, ``, ``, ``, `` ```tsx mdx:preview import React, { Fragment } from 'react'; @@ -580,6 +580,52 @@ Passing **as="tagName"** will automatically infer the type. /> ``` +Add a click event on the data row + +```tsx mdx:preview +import React from 'react'; +import JsonView from '@uiw/react-json-view'; + +export default function Demo() { + return ( + + { + return ( +
{ + console.log("keyName", keyName) + console.log("value", value) + console.log("parentValue", parentValue) + }} + /> + ) + }} + /> + + ) +} +``` + ## Highlight Updates ```tsx mdx:preview @@ -777,6 +823,7 @@ import { Arrow } from './symbol/Arrow'; import { Colon } from './symbol/Colon'; import { Quote } from './symbol/Quote'; import { ValueQuote } from './symbol/ValueQuote'; + import { Bigint } from './types/Bigint'; import { Date } from './types/Date'; import { False } from './types/False'; @@ -790,13 +837,24 @@ import { StringText } from './types/String'; import { True } from './types/True'; import { Undefined } from './types/Undefined'; import { Url } from './types/Url'; + import { Copied } from './section/Copied'; import { CountInfo } from './section/CountInfo'; import { CountInfoExtra } from './section/CountInfoExtra'; import { Ellipsis } from './section/Ellipsis'; import { KeyName } from './section/KeyName'; +import { Row } from './section/Row'; type JsonViewComponent = React.FC>> & { + BraceLeft: typeof BraceLeft; + BraceRight: typeof BraceRight; + BracketsLeft: typeof BracketsLeft; + BracketsRight: typeof BracketsRight; + Arrow: typeof Arrow; + Colon: typeof Colon; + Quote: typeof Quote; + ValueQuote: typeof ValueQuote; + Bigint: typeof Bigint; Date: typeof Date; False: typeof False; @@ -810,19 +868,13 @@ type JsonViewComponent = React.FC>> & { True: typeof True; Undefined: typeof Undefined; Url: typeof Url; - BraceLeft: typeof BraceLeft; - BraceRight: typeof BraceRight; - BracketsLeft: typeof BracketsLeft; - BracketsRight: typeof BracketsRight; - Colon: typeof Colon; - Ellipsis: typeof Ellipsis; - Quote: typeof Quote; - ValueQuote: typeof ValueQuote; - Arrow: typeof Arrow; + Copied: typeof Copied; CountInfo: typeof CountInfo; CountInfoExtra: typeof CountInfoExtra; + Ellipsis: typeof Ellipsis; KeyName: typeof KeyName; + Row: typeof Row; }; declare const JsonView: JsonViewComponent; export default JsonView; diff --git a/core/src/comps/KeyValues.tsx b/core/src/comps/KeyValues.tsx index 50e0f18a..a623d360 100644 --- a/core/src/comps/KeyValues.tsx +++ b/core/src/comps/KeyValues.tsx @@ -4,6 +4,7 @@ import { useExpandsStore } from '../store/Expands'; import { useShowToolsDispatch } from '../store/ShowTools'; import { Value } from './Value'; import { KeyNameComp } from '../section/KeyName'; +import { RowComp } from '../section/Row'; import { Container } from '../Container'; import { Quote, Colon } from '../symbol'; import { useHighlight } from '../utils/useHighlight'; @@ -101,10 +102,10 @@ export const KeyValuesItem = (props: KeyValuesProps) => { onMouseLeave: () => dispatch({ [subkeyid]: false }), }; return ( -
+ -
+ ); }; diff --git a/core/src/editor/store.tsx b/core/src/editor/store.tsx index 702f36c5..ef3f65ad 100644 --- a/core/src/editor/store.tsx +++ b/core/src/editor/store.tsx @@ -1,4 +1,4 @@ -import { FC, PropsWithChildren, createContext, useContext, useReducer } from 'react'; +import { createContext, useContext, useReducer } from 'react'; type InitialState = { /** diff --git a/core/src/index.tsx b/core/src/index.tsx index e5d0ef56..8d9eb7ea 100644 --- a/core/src/index.tsx +++ b/core/src/index.tsx @@ -30,6 +30,7 @@ import { CountInfo } from './section/CountInfo'; import { CountInfoExtra } from './section/CountInfoExtra'; import { Ellipsis } from './section/Ellipsis'; import { KeyName } from './section/KeyName'; +import { Row } from './section/Row'; export * from './store'; export * from './store/Expands'; @@ -96,6 +97,7 @@ type JsonViewComponent = React.FC>> & { CountInfo: typeof CountInfo; CountInfoExtra: typeof CountInfoExtra; KeyName: typeof KeyName; + Row: typeof Row; }; const JsonView: JsonViewComponent = forwardRef>((props, ref) => { @@ -173,6 +175,7 @@ JsonView.Copied = Copied; JsonView.CountInfo = CountInfo; JsonView.CountInfoExtra = CountInfoExtra; JsonView.KeyName = KeyName; +JsonView.Row = Row; JsonView.displayName = 'JVR.JsonView'; diff --git a/core/src/section/Row.tsx b/core/src/section/Row.tsx new file mode 100644 index 00000000..ce63abf3 --- /dev/null +++ b/core/src/section/Row.tsx @@ -0,0 +1,39 @@ +import { type TagType } from '../store/Types'; +import { type SectionElement, useSectionStore } from '../store/Section'; +import { useSectionRender } from '../utils/useRender'; + +export const Row = (props: SectionElement) => { + const { Row: Comp = {} } = useSectionStore(); + useSectionRender(Comp, props, 'Row'); + return null; +}; + +Row.displayName = 'JVR.Row'; + +export interface RowCompProps extends React.HTMLAttributes { + value?: T; + keyName?: string | number; + parentValue?: unknown; +} + +export const RowComp = ({ + children, + value, + keyName = '', + parentValue, + ...other +}: React.PropsWithChildren>) => { + const { Row: Comp = {} } = useSectionStore(); + const { as, render, children: _, ...reset } = Comp; + const Elm = as || 'div'; + const child = + render && typeof render === 'function' && render({ ...other, ...reset, children }, { value, keyName, parentValue }); + if (child) return child; + return ( + + {children} + + ); +}; + +RowComp.displayName = 'JVR.RowComp'; diff --git a/core/src/store/Section.tsx b/core/src/store/Section.tsx index c83a3c84..622f2ab2 100644 --- a/core/src/store/Section.tsx +++ b/core/src/store/Section.tsx @@ -16,6 +16,7 @@ type InitialState = { CountInfo?: SectionElement; CountInfoExtra?: SectionElement; Ellipsis?: SectionElement; + Row?: SectionElement; KeyName?: SectionElement; }; @@ -57,6 +58,10 @@ const initialState: InitialState = { className: 'w-rjv-ellipsis', children: '...', }, + Row: { + as: 'div', + className: 'w-rjv-line', + }, KeyName: { as: 'span', className: 'w-rjv-object-key',