Skip to content

Commit e261c35

Browse files
authored
Merge pull request mac-s-g#68 from mac-s-g/add-delete-nodes
Add delete nodes
2 parents 66e0d23 + 23976bc commit e261c35

File tree

24 files changed

+1186
-189
lines changed

24 files changed

+1186
-189
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ Name|Type|Default|Description
5050
`enableClipboard`|`boolean`|`true`|When set to `true`, the user can copy objects and arrays to clipboard
5151
`displayObjectSize`|`boolean`|`true`|When set to `true`, objects and arrays are labeled with size
5252
`displayDataTypes`|`boolean`|`true`|When set to `true`, data type labels prefix values
53-
`onEdit`|`(edit) => {}`|`false`|When a callback function is passed in, value edits are enabled. The callback is invoked when edits are made. [see: onEdit docs](#onedit-interaction)
53+
`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.
54+
`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.
55+
`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-interaction)
5456

5557
### Features
5658
* Object and array nodes can be collapsed and expanded
@@ -144,5 +146,6 @@ I drew a ton of design ideas from [react-json-tree](https://github.com/alexkuz/r
144146
I'm also inspired by users who come up with interesting feature requests. Reach out to me with ideas for this project or other projects you want to collaborate on. My email address is listed on my [github user page](https://github.com/mac-s-g).
145147

146148
### To-Do's
147-
1. Support "delete" capability when `onEdit` is enabled
148-
2. Support "add" capability when `onEdit` is enabled
149+
1. Improve documentation for `onAdd` and `onDelete` props
150+
2. Improve style organization
151+
3. Support integer input to `collapsed` prop to collapse at a particular depth.

docker/debug.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ rm -rf ./debug
99

1010
mkdir debug
1111

12-
# run the workbench container
12+
# run the container
1313
docker run \
1414
-v $(pwd)/src:/react/src \
1515
-v $(pwd)/debug:/react/debug \

example/example.html

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
.react-json-view {
2828
padding: 4px 6px;
2929
border-radius: 3px;
30-
display: inline-block;
30+
display: block;
3131
}
3232
.example {
3333
margin-bottom: 25px;
@@ -60,6 +60,8 @@
6060
<div class="example" id="example3"></div>
6161
<div class="code-comment">// example 5: JSON with "chalk" theme</div>
6262
<div class="example" id="example4"></div>
63+
<div class="code-comment">// example 6: Using a custom theme</div>
64+
<div class="example" id="example5"></div>
6365
</div>
6466
<script src="https://unpkg.com/react@15/dist/react.js"></script>
6567
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
@@ -186,6 +188,36 @@
186188
jsonView4.render(),
187189
document.getElementById('example4')
188190
);
191+
192+
/*-----------EXAMPLE 5-----------*/
193+
// custom theme
194+
var jsonView4 = new reactJsonView.default({
195+
src: test_json,
196+
enableClipboard: false,
197+
theme: {
198+
base00: 'white',
199+
base01: '#ddd',
200+
base02: '#ddd',
201+
base03: '#444',
202+
base04: 'purple',
203+
base05: '#444',
204+
base06: '#444',
205+
base07: '#444',
206+
base08: '#444',
207+
base09: 'rgba(70, 70, 230, 1)',
208+
base0A: 'rgba(70, 70, 230, 1)',
209+
base0B: 'rgba(70, 70, 230, 1)',
210+
base0C: 'rgba(70, 70, 230, 1)',
211+
base0D: 'rgba(70, 70, 230, 1)',
212+
base0E: 'rgba(70, 70, 230, 1)',
213+
base0F: 'rgba(70, 70, 230, 1)'
214+
}
215+
});
216+
ReactDOM.render(
217+
jsonView4.render(),
218+
document.getElementById('example5')
219+
);
220+
189221
</script>
190222
</body>
191223
</html>

example/example.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ ReactDom.render(
1414
<JsonViewer
1515
src={getExampleJson1()}
1616
collapseStringsAfterLength={12}
17-
onEdit={(edit)=>{console.log(edit)}} />
17+
onEdit={(e)=>{console.log(e)}}
18+
onDelete={(e)=>{console.log(e)}}
19+
onAdd={(e)=>{console.log(e)}}
20+
displayObjectSize={false}
21+
enableClipboard={true} />
1822

1923
<br />
2024

@@ -23,7 +27,9 @@ ReactDom.render(
2327
src={getExampleJson1()}
2428
theme='railscasts'
2529
collapseStringsAfterLength={15}
26-
onEdit={(e)=>{console.log(e)}} />
30+
onEdit={(e)=>{console.log(e)}}
31+
onDelete={(e)=>{console.log(e)}}
32+
onAdd={(e)=>{console.log(e)}} />
2733

2834
<br />
2935

@@ -61,24 +67,25 @@ ReactDom.render(
6167

6268
{/* custom theme example */}
6369
<JsonViewer
70+
enableClipboard={false}
6471
src={getExampleJson1()}
6572
theme={{
6673
base00: 'white',
6774
base01: '#ddd',
6875
base02: '#ddd',
69-
base03: 'black',
70-
base04: 'black',
71-
base05: 'black',
72-
base06: 'black',
73-
base07: 'black',
74-
base08: 'black',
75-
base09: 'rgba(20, 150, 20, 1)',
76-
base0A: 'black',
77-
base0B: 'black',
78-
base0C: 'black',
79-
base0D: 'black',
80-
base0E: 'black',
81-
base0F: 'black'
76+
base03: '#444',
77+
base04: 'purple',
78+
base05: '#444',
79+
base06: '#444',
80+
base07: '#444',
81+
base08: '#444',
82+
base09: 'rgba(70, 70, 230, 1)',
83+
base0A: 'rgba(70, 70, 230, 1)',
84+
base0B: 'rgba(70, 70, 230, 1)',
85+
base0C: 'rgba(70, 70, 230, 1)',
86+
base0D: 'rgba(70, 70, 230, 1)',
87+
base0E: 'rgba(70, 70, 230, 1)',
88+
base0F: 'rgba(70, 70, 230, 1)'
8289
}}
8390
/>
8491

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"react-hot-loader": "^3.0.0-beta.6",
3838
"react-icons": "2.2.5",
3939
"react-test-renderer": "^15.5.4",
40-
"react-tooltip": "^3.2.10",
40+
"react-tooltip": "^3.3.0",
4141
"sinon": "^2.2.0",
4242
"webpack": "^2.2.1",
4343
"webpack-bundle-size-analyzer": "^2.7.0",

src/html/index.html.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
.react-json-view {
2020
padding: 4px 6px;
2121
border-radius: 3px;
22-
display: inline-block;
22+
/*display: inline-block;*/
2323
}
2424
</style>
2525
</head>

src/js/components/AddKeyRequest.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import React from 'react';
2+
import dispatcher from './../helpers/dispatcher';
3+
import ObjectAttributes from './../stores/ObjectAttributes';
4+
5+
import Cancel from 'react-icons/lib/md/add';
6+
import Add from 'react-icons/lib/fa/check-circle';
7+
8+
//global theme
9+
import Theme from './../themes/getStyle';
10+
11+
12+
//this input appears when adding a new value to an object
13+
export default class extends React.Component {
14+
15+
state = {
16+
input: ''
17+
}
18+
19+
render() {
20+
const {active, theme, rjvId} = this.props;
21+
const {input} = this.state;
22+
const valid = this.isValid(rjvId, input);
23+
24+
return active ? (
25+
<div
26+
class="add-key-request"
27+
{...Theme(theme, 'add-key-request')}
28+
onClick={()=>{
29+
dispatcher.dispatch({
30+
rjvId: rjvId,
31+
name: 'RESET'
32+
});
33+
this.state.input = '';
34+
}}
35+
>
36+
<div {...Theme(theme, 'add-key-modal')}
37+
onClick={(e)=>{e.stopPropagation();}}>
38+
<div {...Theme(theme, 'add-key-label')}>Key Name:</div>
39+
<div style={{position: 'relative'}}>
40+
<input {...Theme(theme, 'add-key-input')}
41+
class="add-key-input"
42+
ref={input => input && input.focus()}
43+
spellCheck={false}
44+
value={input}
45+
placeholder="..."
46+
onChange={(e)=>{
47+
this.setState({
48+
input: e.target.value
49+
})
50+
}}
51+
onKeyPress={(e)=>{
52+
if (valid && e.key === 'Enter') {
53+
this.submit();
54+
}
55+
}}
56+
/>
57+
{valid
58+
? <Add {...Theme(theme, 'add-key-submit')}
59+
class="add-key-submit"
60+
onClick={e => this.submit()}
61+
/>
62+
: null}
63+
</div>
64+
<span {...Theme(theme, 'add-key-cancel')}>
65+
<Cancel {...Theme(theme, 'add-key-cancel-icon')}
66+
class="add-key-cancel"
67+
onClick={()=>{
68+
dispatcher.dispatch({
69+
rjvId: rjvId,
70+
name: 'RESET'
71+
});
72+
this.state.input = '';
73+
}} />
74+
</span>
75+
</div>
76+
</div>
77+
) : null;
78+
}
79+
80+
81+
isValid = (rjvId, input) => {
82+
const request = ObjectAttributes.get(
83+
rjvId, 'action', 'new-key-request'
84+
);
85+
return (
86+
input != ''
87+
&& Object.keys(request.existing_value).indexOf(input) === -1
88+
);
89+
}
90+
91+
submit = () => {
92+
const {input} = this.state;
93+
const {rjvId} = this.props;
94+
let request = ObjectAttributes.get(
95+
rjvId, 'action', 'new-key-request'
96+
);
97+
request.new_value = {...request.existing_value};
98+
request.new_value[input] = null;
99+
dispatcher.dispatch({
100+
name: 'VARIABLE_ADDED',
101+
rjvId: rjvId,
102+
data: {...request, key_name: input}
103+
});
104+
this.state.input = '';
105+
}
106+
107+
}

src/js/components/DataTypes/Date.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export default class extends React.Component {
99
render() {
1010
const type_name = 'date';
1111
const {props} = this;
12-
const {value} = props.variable;
1312
const display_options = {
1413
weekday: "short",
1514
year: "numeric",
@@ -18,11 +17,12 @@ export default class extends React.Component {
1817
hour: "2-digit",
1918
minute: "2-digit"
2019
};
20+
console.log(props);
2121
return (
2222
<div {...Theme(props.theme, 'date')}>
2323
<DataTypeLabel type_name={type_name} {...props} />
2424
<span class="date-value" {...Theme(props.theme, 'date-value')}>
25-
{value.toLocaleTimeString(
25+
{props.value.toLocaleTimeString(
2626
'en-us', display_options
2727
)}
2828
</span>

src/js/components/DataTypes/Object.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class rjvObject extends React.Component {
5252
object_type: (props.type == 'array' ? 'array' : 'object'),
5353
parent_type: (props.type == 'array' ? 'array' : 'object'),
5454
display_name: (props.name ? props.name : ''),
55-
hover: false
55+
hover: this.state.hover ? this.state.hover : false
5656
}
5757

5858
this.state = {...this.state, ...state};
@@ -84,9 +84,9 @@ class rjvObject extends React.Component {
8484
</div>);
8585
}
8686

87-
getElipsis = () => {
87+
getEllipsis = () => {
8888
return (
89-
<div {...Theme(this.props.theme, 'elipsis')}
89+
<div {...Theme(this.props.theme, 'ellipsis')}
9090
onClick={this.toggleCollapsed}>
9191
...
9292
</div>
@@ -168,7 +168,7 @@ class rjvObject extends React.Component {
168168
</span>
169169
{expanded
170170
? this.getObjectContent(depth, src, {theme, ...rest})
171-
: this.getElipsis()
171+
: this.getEllipsis()
172172
}
173173
<span class="brace-row">
174174
<span style={{

src/js/components/DataTypes/String.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class extends React.Component {
4040
const type_name = 'string';
4141
const {collapsed} = this.state;
4242
const {props} = this;
43-
const {collapseStringsAfterLength} = props;
43+
const {collapseStringsAfterLength, theme} = props;
4444
let {value} = props;
4545
let collapsible = toType(collapseStringsAfterLength) == 'integer';
4646
let style = {style: {cursor: 'default'}};
@@ -50,16 +50,19 @@ export default class extends React.Component {
5050
) {
5151
style.style.cursor = 'pointer';
5252
if (this.state.collapsed) {
53-
value = value.substring(0, collapseStringsAfterLength)
54-
+ "...";
53+
value = (<span>
54+
{value.substring(0, collapseStringsAfterLength)}
55+
<span {...Theme(theme, 'ellipsis')}
56+
> ...</span>
57+
</span>);
5558
}
5659
}
5760

5861
return (
59-
<div {...Theme(props.theme, 'string')} >
62+
<div {...Theme(theme, 'string')} >
6063
<DataTypeLabel type_name={type_name} {...props} />
61-
<span class="string-value"
62-
{...style} onClick={this.toggleCollapsed}>
64+
<span class="string-value" {...style}
65+
onClick={this.toggleCollapsed}>
6366
"{value}"
6467
</span>
6568
</div>

0 commit comments

Comments
 (0)