Skip to content

Commit 39f7b6b

Browse files
authored
Merge pull request mac-s-g#205 from cainaleaouk/master
Add a way to set the default value when adding a key to json
2 parents 8461ae1 + 2616eee commit 39f7b6b

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![npm](https://img.shields.io/npm/v/react-json-view.svg)](https://www.npmjs.com/package/react-json-view) [![npm](https://img.shields.io/npm/l/react-json-view.svg)](https://github.com/mac-s-g/react-json-view/blob/master/LISCENSE) [![Build Status](https://travis-ci.org/mac-s-g/react-json-view.svg)](https://travis-ci.org/mac-s-g/react-json-view) [![Coverage Status](https://coveralls.io/repos/github/mac-s-g/react-json-view/badge.svg?branch=master)](https://coveralls.io/github/mac-s-g/react-json-view?branch=master)
44

55
# react-json-view
6-
RJV is a react component for displaying and editing javascript **arrays** and **JSON objects**.
6+
RJV is a React component for displaying and editing javascript **arrays** and **JSON objects**.
77

88
This component provides a responsive interface for displaying arrays or JSON in a web browser. NPM offers a distribution of the source that's transpiled to ES5; so you can include this component with *any web-based javascript application*.
99

@@ -55,6 +55,7 @@ Name|Type|Default|Description
5555
`displayDataTypes`|`boolean`|`true`|When set to `true`, data type labels prefix values
5656
`onEdit`|`(edit)=>{}`|`false`|When a callback function is passed in, `edit` functionality is enabled. The callback is invoked before edits are completed. Returning `false` from `onEdit` will prevent the change from being made. [see: onEdit docs](#onedit-onadd-and-ondelete-interaction)
5757
`onAdd`|`(add)=>{}`|`false`|When a callback function is passed in, `add` functionality is enabled. The callback is invoked before additions are completed. Returning `false` from `onAdd` will prevent the change from being made. [see: onAdd docs](#onedit-onadd-and-ondelete-interaction)
58+
`defaultValue`|`string \|number \|boolean \|array \|object`|`null`|Sets the default value to be used when adding an item to json
5859
`onDelete`|`(delete)=>{}`|`false`|When a callback function is passed in, `delete` functionality is enabled. The callback is invoked before deletions are completed. Returning `false` from `onDelete` will prevent the change from being made. [see: onDelete docs](#onedit-onadd-and-ondelete-interaction)
5960
`onSelect`|`(select)=>{}`|`false`|When a function is passed in, clicking a value triggers the `onSelect` method to be called.
6061
`sortKeys`|`boolean`|`false`|set to true to sort object keys

dev-server/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ReactDom.render(
5050
}
5151
return false
5252
}}
53+
defaultValue=""
5354
/>
5455

5556
<br />

index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ export interface ReactJsonViewProps {
129129
* Default: false
130130
*/
131131
sortKeys?: boolean;
132+
/**
133+
* Set to a value to be used as defaultValue when adding new key to json
134+
*
135+
* Default: null
136+
*/
137+
defaultValue?: TypeDefaultValue | TypeDefaultValue[] | null;
132138
}
133139

134140
export interface OnCopyProps {
@@ -213,6 +219,8 @@ export interface OnSelectProps {
213219

214220
}
215221

222+
export type TypeDefaultValue = string | number | boolean | object;
223+
216224
export interface ThemeObject {
217225
base00: string;
218226
base01: string;

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"name": "react-json-view",
3-
"description":
4-
"Interactive react component for displaying javascript arrays and JSON objects.",
5-
"version": "1.18.3",
3+
"description": "Interactive react component for displaying javascript arrays and JSON objects.",
4+
"version": "1.19.0",
65
"main": "dist/main.js",
76
"dependencies": {
87
"flux": "^3.1.3",

src/js/components/ObjectKeyModal/AddKeyRequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class extends React.PureComponent {
4040
rjvId, 'action', 'new-key-request'
4141
);
4242
request.new_value = {...request.existing_value};
43-
request.new_value[input] = null;
43+
request.new_value[input] = this.props.defaultValue;
4444
dispatcher.dispatch({
4545
name: 'VARIABLE_ADDED',
4646
rjvId: rjvId,

src/js/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class ReactJsonView extends React.PureComponent {
5757
onSelect: false,
5858
iconStyle: 'triangle',
5959
style: {},
60-
validationMessage: 'Validation Error'
60+
validationMessage: 'Validation Error',
61+
defaultValue: null,
6162
}
6263

6364
// will trigger whenever setState() is called, or parent passes in new props.
@@ -178,7 +179,7 @@ class ReactJsonView extends React.PureComponent {
178179
name
179180
} = this.state;
180181

181-
const { style } = this.props;
182+
const { style, defaultValue } = this.props;
182183

183184
return (
184185
<div
@@ -200,7 +201,8 @@ class ReactJsonView extends React.PureComponent {
200201
<AddKeyRequest
201202
active={addKeyRequest}
202203
theme={theme}
203-
rjvId={this.rjvId} />
204+
rjvId={this.rjvId}
205+
defaultValue={defaultValue} />
204206
</div>
205207
);
206208
}

0 commit comments

Comments
 (0)