Skip to content
This repository was archived by the owner on Feb 7, 2022. It is now read-only.

Added Root Options #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ on initial render. Receives two arguments: `keypath` and `value`. Defaults to
Optional parameters for filterer (search). Must be an object.

- `ignoreCase`, Set to `true` to enable case insensitivity in search. Defaults to `false`.

#### props.rootExpanded

Optional boolean to expand the root object. Defaults to true.
Copy link
Owner

Choose a reason for hiding this comment

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

Not sure about this option, since there already exists a isExpanded predicate, I would like to rework it to be applied to root node as well. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Hi,

That sounds great. It didn't occur to me to update the existing isExpanded predicate.


#### props.showRootLabel

Optional boolean to show the root label. Defaults to true.

#### props.rootLabel

Optional string for the root label. Defaults to root.
5 changes: 4 additions & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ document.addEventListener('DOMContentLoaded', function() {
inspector({
data: data,
onClick: console.log.bind(console),
interactiveLabel: interactiveSelection
interactiveLabel: interactiveSelection,
rootExpanded: false,
showRootLabel: true,
rootLabel: 'root label'
}),
document.getElementById('inspector')
);
Expand Down
14 changes: 11 additions & 3 deletions json-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ module.exports = React.createClass({
onClick: React.PropTypes.func,
validateQuery: React.PropTypes.func,
isExpanded: React.PropTypes.func,
filterOptions: React.PropTypes.object
filterOptions: React.PropTypes.object,
rootExpanded: React.PropTypes.bool,
showRootLabel: React.PropTypes.bool,
rootLabel: React.PropTypes.string
},

getDefaultProps: function() {
Expand All @@ -48,7 +51,10 @@ module.exports = React.createClass({
*/
isExpanded: function(keypath, value) {
return false;
}
},
rootExpanded: true,
showRootLabel: true,
rootLabel: 'root'
};
},
getInitialState: function() {
Expand All @@ -68,8 +74,10 @@ module.exports = React.createClass({
id: p.id,
getOriginal: this.getOriginal,
query: s.query,
label: 'root',
label: p.rootLabel,
root: true,
rootExpanded: p.rootExpanded,
showRootLabel: p.showRootLabel,
isExpanded: p.isExpanded,
interactiveLabel: p.interactiveLabel
});
Expand Down
23 changes: 16 additions & 7 deletions lib/leaf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ var Leaf = React.createClass({
getDefaultProps: function() {
return {
root: false,
//rootExpanded: true,
prefix: ''
};
},
render: function() {
console.log(this.props);
var id = 'id_' + uid();
var p = this.props;

Expand All @@ -34,20 +36,28 @@ var Leaf = React.createClass({
};

var onLabelClick = this._onClick.bind(this, d);

return D.div({ className: this.getClassName(), id: 'leaf-' + this._rootPath() },
D.input({ className: 'json-inspector__radio', type: 'radio', name: p.id, id: id, tabIndex: -1 }),
D.label({ className: 'json-inspector__line', htmlFor: id, onClick: onLabelClick },
D.div({ className: 'json-inspector__flatpath' },
d.path),
D.span({ className: 'json-inspector__key' },
this.format(d.key),
':',
this.renderInteractiveLabel(d.key, true)),
this.renderLabel(d.key),
this.renderTitle(),
this.renderShowOriginalButton()),
this.renderChildren());
},
renderLabel: function(key){
var leafLabel = D.span({ className: 'json-inspector__key' },
this.format(key),
':',
this.renderInteractiveLabel(key, true));
if(this.props.root && !this.props.showRootLabel){
leafLabel = D.span({ className: 'json-inspector__key' },
this.renderInteractiveLabel(key, true));
}
return leafLabel;
},
Copy link
Owner

Choose a reason for hiding this comment

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

I propose to split this to two methods, one would render static label (e.g. this.format(key) + ':'), the second one – interactive label. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds great. Thanks for looking at my changes and thanks for the great library :-)

renderTitle: function() {
var data = this.data();
var t = type(data);
Expand Down Expand Up @@ -184,9 +194,8 @@ var Leaf = React.createClass({
},
_isInitiallyExpanded: function(p) {
var keypath = this.keypath();

if (p.root) {
return true;
return p.rootExpanded;
}

if (p.query === '') {
Expand Down