Skip to content

Commit 6967a87

Browse files
committed
initial commit
0 parents  commit 6967a87

26 files changed

Lines changed: 1892 additions & 0 deletions

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["react", "es2015", "stage-0"]
3+
}

.eslintrc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "airbnb",
4+
"globals": {
5+
"React": true
6+
},
7+
"rules": {
8+
"react/jsx-boolean-value": [2, "always"],
9+
"react/sort-comp": [0],
10+
"react/no-multi-comp": [0],
11+
"react/no-did-mount-set-state": [0],
12+
"react/jsx-no-duplicate-props": [2],
13+
"react/jsx-filename-extension": [0],
14+
"no-param-reassign": [0],
15+
"no-else-return": [0],
16+
"no-unused-vars": [2, {
17+
"vars": "all",
18+
"args": "none"
19+
}],
20+
"no-use-before-define": [2, "nofunc"],
21+
"indent": [2, 2, {
22+
"SwitchCase": 1
23+
}],
24+
"jsx-quotes": [0],
25+
"new-cap": [2, {
26+
"capIsNewExceptions": ["Line", "Bar"]
27+
}],
28+
"consistent-return": [0],
29+
"no-dupe-keys": [2],
30+
"max-len": [0],
31+
"no-return-assign": [0],
32+
"import/no-unresolved": [2, {
33+
"ignore": ["react"]
34+
}],
35+
"import/no-named-as-default": [0]
36+
}
37+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Hexa1 LLC, Vito LaVilla
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# React JSON Editor
2+
3+
## Peer Dependencies
4+
You must have these packages installed in your project to use this component:
5+
6+
- `react ^0.14 || ^15.0.0-rc || ^15.0`
7+
- `react-dom ^0.14 || ^15.0.0-rc || ^15.0`
8+
- `react-bootstrap ^0.29.3`
9+
- `react-select ^0.7.0`
10+
11+
## Usage
12+
```js
13+
import JSONEditor from 'react-json-editor';
14+
15+
// in your component
16+
<JSONEditor json={{ foo: 'bar' }} onChange={json => console.log(json)} />
17+
```
18+
19+
You'll also need to include the css which is located in `dist/jsonEditor.css'.
20+
21+
### Props
22+
23+
|Prop | Type | Default | Description
24+
|----------|------------------|-------------|-------------
25+
|`json` | object or string | `{}` | the json object you want to edit. if you pass a string, the editor will attempt to parse it with `JSON.parse()`.
26+
|`onChange`| function | `noop` | called whenever a change occurs. the updated json is passed as the first and only argument to the function.
27+
28+
## Development
29+
30+
### Building
31+
`npm run build` will run the js through babel and the scss through node-sass and output into `dist/`.
32+
33+
The `dist` folder should be committed to the repo.
34+
35+
### Linting
36+
`npm run lint` - please lint before committing any code
37+
38+
## Known Issues
39+
- Typing a decimal as the last character in a number field doesn't work because of the way we coerce the type - if the field is a number type, we try to `parseFloat()` it, which parses a value like `1.` to `1`. Adding the decimal anywhere but the last character will work, so a workaround to trying to type `1.1` would be to type `11`, then insert the decimal between the `1`s.
40+
41+
## TODO
42+
- Remove react-bootstrap dependency (used for tooltips/popovers)
43+
- Remove react-select dependency (used for dropdown)
44+
- Allow passing input & select factories instead of using bootstrap/react-select components
45+
- Add AMD/UMD module support via webpack
46+
- Tests
47+
- Examples
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
9+
var _react = require('react');
10+
11+
var _OverlayTrigger = require('react-bootstrap/lib/OverlayTrigger');
12+
13+
var _OverlayTrigger2 = _interopRequireDefault(_OverlayTrigger);
14+
15+
var _Popover = require('react-bootstrap/lib/Popover');
16+
17+
var _Popover2 = _interopRequireDefault(_Popover);
18+
19+
var _lib = require('../lib');
20+
21+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22+
23+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24+
25+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
26+
27+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
28+
29+
var AddElementButton = function (_Component) {
30+
_inherits(AddElementButton, _Component);
31+
32+
function AddElementButton(props) {
33+
_classCallCheck(this, AddElementButton);
34+
35+
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AddElementButton).call(this, props));
36+
37+
_this.state = {
38+
newElementName: '',
39+
newElementNameInvalid: false
40+
};
41+
42+
_this.addElement = _this.addElement.bind(_this);
43+
return _this;
44+
}
45+
46+
_createClass(AddElementButton, [{
47+
key: 'addElement',
48+
value: function addElement(e) {
49+
if (e) {
50+
e.preventDefault();
51+
}
52+
53+
var _props = this.props;
54+
var fieldValue = _props.fieldValue;
55+
var path = _props.path;
56+
var newElementName = this.state.newElementName;
57+
var _context$jsonEditor = this.context.jsonEditor;
58+
var addArrayElement = _context$jsonEditor.addArrayElement;
59+
var addMapElement = _context$jsonEditor.addMapElement;
60+
61+
62+
if ((0, _lib.getValueType)(fieldValue) === 'array') {
63+
return addArrayElement(path, null);
64+
}
65+
66+
var added = addMapElement(path, newElementName, null);
67+
68+
if (added) {
69+
this.newElementOverlay.hide();
70+
this.setState({
71+
newElementInvalidName: false
72+
});
73+
} else {
74+
this.setState({
75+
newElementInvalidName: true
76+
});
77+
}
78+
}
79+
}, {
80+
key: 'render',
81+
value: function render() {
82+
var _this2 = this;
83+
84+
var fieldValue = this.props.fieldValue;
85+
var _state = this.state;
86+
var newElementName = _state.newElementName;
87+
var newElementInvalidName = _state.newElementInvalidName;
88+
89+
var isArray = (0, _lib.getValueType)(fieldValue) === 'array';
90+
var onBtnClick = isArray ? this.addElement.bind(this) : null;
91+
92+
var button = React.createElement(
93+
'button',
94+
{ className: 'btn default btn-xs add-element-btn', type: 'button', onClick: onBtnClick },
95+
React.createElement('i', { className: 'fa fa-plus' }),
96+
'Add element'
97+
);
98+
99+
// new array elements don't need a name, so just render the button and add the element when it's clicked
100+
if (isArray) {
101+
return button;
102+
}
103+
104+
var newElementNameOverlay = React.createElement(
105+
_Popover2.default,
106+
{ id: 'json-editor-new-element-name' },
107+
React.createElement(
108+
'form',
109+
{ onSubmit: this.addElement },
110+
React.createElement('input', {
111+
type: 'text',
112+
className: 'form-control',
113+
placeholder: 'Element Name',
114+
value: newElementName,
115+
onChange: function onChange(e) {
116+
return _this2.setState({ newElementName: e.target.value });
117+
},
118+
ref: function ref(i) {
119+
return setTimeout(function () {
120+
return i && i.focus();
121+
});
122+
}
123+
}),
124+
React.createElement('i', { className: 'fa fa-check add-new-element', onClick: this.addElement }),
125+
React.createElement('i', { className: 'fa fa-times cancel-new-element', onClick: function onClick() {
126+
return _this2.newElementOverlay.hide();
127+
} }),
128+
newElementInvalidName && React.createElement(
129+
'div',
130+
{ className: 'invalid-element-name' },
131+
' Map element keys must be unique '
132+
)
133+
)
134+
);
135+
136+
return React.createElement(
137+
_OverlayTrigger2.default,
138+
{
139+
overlay: newElementNameOverlay,
140+
trigger: ['click'],
141+
placement: 'top',
142+
rootClose: true,
143+
onEnter: function onEnter() {
144+
return _this2.setState({ newElementInvalidName: false });
145+
},
146+
onExited: function onExited() {
147+
return _this2.setState({ newElementName: '' });
148+
},
149+
ref: function ref(ot) {
150+
return _this2.newElementOverlay = ot;
151+
}
152+
},
153+
button
154+
);
155+
}
156+
}]);
157+
158+
return AddElementButton;
159+
}(_react.Component);
160+
161+
AddElementButton.propTypes = {
162+
fieldValue: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.array, _react.PropTypes.object, _react.PropTypes.number, _react.PropTypes.bool]),
163+
path: _react.PropTypes.string
164+
};
165+
AddElementButton.contextTypes = {
166+
jsonEditor: _react.PropTypes.object
167+
};
168+
exports.default = AddElementButton;

0 commit comments

Comments
 (0)