Skip to content

Commit bed6c7e

Browse files
author
Yell0wflash
committed
feat: add className prop
Fixes: mac-s-g/react-json-view#377
1 parent 211c7da commit bed6c7e

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const Component = () => (<ReactJson src={json}/>)
3030
| `src` | `JSON Object` | None | This property contains your input JSON |
3131
| `name` | `string` or `false` | "root" | Contains the name of your root node. Use `null` or `false` for no name. |
3232
| `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. |
33+
| `className` | `string` | `undefined` | Additional `className` string to append to the `className` of react-json-view's container. |
3334
| `style` | `object` | `{}` | Style attributes for react-json-view container. Explicit style attributes will override attributes provided by a theme. |
3435
| `iconStyle` | `string` | "circle" | Style of expand/collapse icons. Accepted values are "circle", triangle" or "square". |
3536
| `indentWidth` | `integer` | 4 | Set the indent-width for nested objects |

src/index.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import React from 'react'
22
import { polyfill } from 'react-lifecycles-compat'
3+
34
import JsonViewer from './components/JsonViewer'
45
import AddKeyRequest from './components/ObjectKeyModal/AddKeyRequest'
56
import ValidationFailure from './components/ValidationFailure'
6-
import { toType, isTheme } from './helpers/util'
7+
import { isTheme, toType } from './helpers/util'
78
import ObjectAttributes from './stores/ObjectAttributes'
8-
9-
//global theme
9+
// global theme
1010
import Theme from './themes/getStyle'
1111
import type { ReactJsonViewProps } from './type'
1212

13-
//forward src through to JsonObject component
13+
// forward src through to JsonObject component
1414
class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
1515
constructor (props: ReactJsonViewProps) {
1616
super(props)
1717
this.state = {
18-
//listen to request to add/edit a key to an object
18+
// listen to request to add/edit a key to an object
1919
addKeyRequest: false,
2020
editKeyRequest: false,
2121
validationFailure: false,
@@ -31,10 +31,10 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
3131
}
3232
}
3333

34-
//reference id for this instance
34+
// reference id for this instance
3535
rjvId = Date.now().toString()
3636

37-
//all acceptable props and default values
37+
// all acceptable props and default values
3838
static defaultProps = {
3939
src: {},
4040
name: 'root',
@@ -91,15 +91,15 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
9191
// @ts-ignore
9292
ObjectAttributes.on(i + '-' + this.rjvId, listeners[i])
9393
}
94-
//reset key request to false once it's observed
94+
// reset key request to false once it's observed
9595
this.setState({
9696
addKeyRequest: false,
9797
editKeyRequest: false
9898
})
9999
}
100100

101101
override componentDidUpdate (prevProps: any, prevState: any) {
102-
//reset key request to false once it's observed
102+
// reset key request to false once it's observed
103103
if (prevState.addKeyRequest !== false) {
104104
this.setState({
105105
addKeyRequest: false
@@ -130,10 +130,11 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
130130
'add-key-request': this.addKeyRequest
131131
}
132132
}
133-
//make sure props are passed in as expected
133+
134+
// make sure props are passed in as expected
134135
static validateState = (state: any) => {
135136
const validatedState = {}
136-
//make sure theme is valid
137+
// make sure theme is valid
137138
if (toType(state.theme) === 'object' && !isTheme(state.theme)) {
138139
console.error(
139140
'react-json-view error:',
@@ -143,7 +144,7 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
143144
// @ts-ignore
144145
validatedState.theme = 'rjv-default'
145146
}
146-
//make sure `src` prop is valid
147+
// make sure `src` prop is valid
147148
if (toType(state.src) !== 'object' && toType(state.src) !== 'array') {
148149
console.error(
149150
'react-json-view error:',
@@ -174,11 +175,11 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
174175
name
175176
} = this.state
176177

177-
const { style, defaultValue } = this.props
178+
const { className, style, defaultValue } = this.props
178179

179180
return (
180181
<div
181-
className="react-json-view"
182+
className={'react-json-view' + `${className ? ' ' : ''}${className ?? ''}`}
182183
// @ts-ignore
183184
style={{ ...Theme(theme, 'app-container').style, ...style }}
184185
>
@@ -212,7 +213,6 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
212213
namespace,
213214
new_value,
214215
existing_value,
215-
variable_removed,
216216
updated_src,
217217
type
218218
} = ObjectAttributes.get(this.rjvId, 'action', 'variable-update')
@@ -224,11 +224,11 @@ class ReactJsonView extends React.PureComponent<ReactJsonViewProps, any> {
224224

225225
const on_edit_payload = {
226226
existing_src: src,
227-
new_value: new_value,
228-
updated_src: updated_src,
229-
name: name,
230-
namespace: namespace,
231-
existing_value: existing_value
227+
new_value,
228+
updated_src,
229+
name,
230+
namespace,
231+
existing_value
232232
}
233233

234234
switch (type) {

src/type.d.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import * as React from 'react'
22

33
export interface ReactJsonViewProps {
44
/**
@@ -20,6 +20,13 @@ export interface ReactJsonViewProps {
2020
* Default: "rjv-default"
2121
*/
2222
theme?: ThemeKeys | ThemeObject;
23+
24+
/**
25+
* Additional `className` string to append to the `className` of react-json-view's container.
26+
*
27+
* @default undefined
28+
*/
29+
className?: string
2330
/**
2431
* Style attributes for react-json-view container.
2532
* Explicit style attributes will override attributes provided by a theme.
@@ -285,5 +292,5 @@ export type ThemeKeys =
285292
| 'tube'
286293
| 'twilight';
287294

288-
declare const JsonViewer: React.ComponentType<ReactJsonViewProps>;
289-
export default JsonViewer;
295+
declare const JsonViewer: React.ComponentType<ReactJsonViewProps>
296+
export default JsonViewer

0 commit comments

Comments
 (0)