Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for root name as jsx element #26

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Or add to your package.json config file:
Name|Type|Default|Description
|:---|:---|:---|:---
`src`|`JSON Object`|None|This property contains your input JSON
`name`|`string` or `false`|"root"|Contains the name of your root node. Use `null` or `false` for no name.
`name`|`string`|`JSX.Element` or `false`|"root"|Contains the name of your root node. Use `null` or `false` for no name.
`theme`|`string`|"rjv-default"|RJV supports base-16 themes. Check out the list of supported themes [in the demo](https://mac-s-g.github.io/react-json-view/demo/dist/). A custom "rjv-default" theme applies by default.
`style`|`object`|`{}`|Style attributes for react-json-view container. Explicit style attributes will override attributes provided by a theme.
`iconStyle`|`string`|"circle"| Style of expand/collapse icons. Accepted values are "circle", triangle" or "square".
Expand Down
7 changes: 7 additions & 0 deletions dev-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ ReactDom.render(
groupArraysAfterLength={50}
src={getExampleJson4()}
/>

{/* Name as colored react component */}
<JsonViewer
collapsed
name={<span style={{color: "red", fontWeight: 800}}>React Element as name</span>}
src={getExampleJson2()}
/>
</div>,
document.getElementById('app-container')
);
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ReactJsonViewProps {
*
* Default: "root"
*/
name?: string | null | false;
name?: React.JSX.Element | string | null | false;
/**
* RJV supports base-16 themes. Check out the list of supported themes in the demo.
* A custom "rjv-default" theme applies by default.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
"@babel/plugin-syntax-class-properties": "~7.12.1",
"@babel/plugin-syntax-jsx": "~7.12.1",
"@babel/register": "7.12.10",
"@types/react": "^18.2.20",
"babel-plugin-react-html-attrs": "~2.1.0",
"chai": "~4.2.0",
"css-loader": "~4.3.0",
Expand Down
5 changes: 4 additions & 1 deletion src/js/components/JsonViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import ArrayGroup from './ArrayGroup';
export default class extends React.PureComponent {
render = () => {
const { props } = this;
const namespace = [props.name];
let namespace = [props.name];
let ObjectComponent = JsonObject;
if (typeof props.name === 'object' && !Array.isArray(props.name)) {
namespace = ['ReactElement'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if namespace = props.displayName ? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Added it.

Also handled If component is unnamed It will use Anonymous exactly the same as most of the react debug tools

}

if (
Array.isArray(props.src) &&
Expand Down