diff --git a/packages/react-json-tree/README.md b/packages/react-json-tree/README.md index 678a5b6a..07964b4c 100644 --- a/packages/react-json-tree/README.md +++ b/packages/react-json-tree/README.md @@ -137,6 +137,34 @@ Their full signatures are: - `labelRenderer: function(keyPath, nodeType, expanded, expandable)` - `valueRenderer: function(valueAsString, value, ...keyPath)` +Additionally, it is possible to override the arrows for expanding, for example with a `+` and `-` button. + +```tsx +const ArrowOverride = ({ expanded, ...rest }: JSONArrowProps) => { + if (expanded) { + return + } + return +} + + +``` + +The default `JSONArrow` component will literally check if an `ArrowComponentOverride` exists and pass it's props there. The typescript for these props is as follows. + +```ts +interface JSONArrowProps { + styling: StylingFunction; + arrowStyle?: 'single' | 'double'; + expanded: boolean; + nodeType: string; + onClick: React.MouseEventHandler; + ariaControls?: string; + ariaLabel?: string + OverrideComponent?: ComponentType; +} +``` + #### More Options - `shouldExpandNodeInitially: function(keyPath, data, level)` - determines if node should be expanded when it first renders (root is expanded by default) diff --git a/packages/react-json-tree/examples/src/App.tsx b/packages/react-json-tree/examples/src/App.tsx index a972ce58..a47d8e90 100644 --- a/packages/react-json-tree/examples/src/App.tsx +++ b/packages/react-json-tree/examples/src/App.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { Map } from 'immutable'; -import { JSONTree, StylingValue } from 'react-json-tree'; +import { JSONArrowProps, JSONTree, StylingValue } from 'react-json-tree'; const getLabelStyle: StylingValue = ({ style }, nodeType, expanded) => ({ style: { @@ -125,6 +125,13 @@ const theme = { base0F: '#cc6633', }; +const ArrowOverride = ({ expanded, ...rest }: JSONArrowProps) => { + if (expanded) { + return + } + return +} + const App = () => (
@@ -168,10 +175,12 @@ const App = () => (

Pass labelRenderer or valueRenderer.

+

Additionally, you may pass an ArrowComponentOverride

(({raw})):} valueRenderer={(raw) => ( diff --git a/packages/react-json-tree/src/ItemRange.tsx b/packages/react-json-tree/src/ItemRange.tsx index ac17679d..9b40c361 100644 --- a/packages/react-json-tree/src/ItemRange.tsx +++ b/packages/react-json-tree/src/ItemRange.tsx @@ -34,6 +34,7 @@ export default function ItemRange(props: Props) { onClick={handleClick} arrowStyle="double" ariaLabel={`Expand Array from ${from} to ${to}`} + OverrideComponent={props.ArrowComponentOverride} /> {`${from} ... ${to}`}
diff --git a/packages/react-json-tree/src/JSONArrow.tsx b/packages/react-json-tree/src/JSONArrow.tsx index e04f6c95..f0e7aaaf 100644 --- a/packages/react-json-tree/src/JSONArrow.tsx +++ b/packages/react-json-tree/src/JSONArrow.tsx @@ -1,7 +1,7 @@ -import React from 'react'; +import React, { ComponentType } from 'react'; import type { StylingFunction } from 'react-base16-styling'; -interface Props { +export interface JSONArrowProps { styling: StylingFunction; arrowStyle?: 'single' | 'double'; expanded: boolean; @@ -9,17 +9,23 @@ interface Props { onClick: React.MouseEventHandler; ariaControls?: string; ariaLabel?: string + OverrideComponent?: ComponentType; } -export default function JSONArrow({ - styling, - arrowStyle = 'single', - expanded, - nodeType, - onClick, - ariaControls, - ariaLabel -}: Props) { +export default function JSONArrow({OverrideComponent, ...props}: JSONArrowProps) { + const { + styling, + arrowStyle = 'single', + expanded, + nodeType, + onClick, + ariaControls, + ariaLabel + } = props + if(OverrideComponent) { + return + } + return (