Skip to content

Commit 246a7a8

Browse files
Merge pull request #40 from ekonstantinidis/move-packagejson
Add Extra Field to Live API
2 parents 3ce7947 + 2fa1cc2 commit 246a7a8

File tree

7 files changed

+149
-13
lines changed

7 files changed

+149
-13
lines changed

rest_framework_docs/static/rest_framework_docs/css/style.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest_framework_docs/static/rest_framework_docs/js/components/helpers/input.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ var React = require('react');
22

33
var Input = React.createClass({
44

5+
removeField: function (fieldName, event) {
6+
event.preventDefault();
7+
this.props.removeField(fieldName);
8+
},
9+
510
handleChange: function (value) {
611
this.props.onChange(value);
712
},
@@ -12,6 +17,13 @@ var Input = React.createClass({
1217
<label
1318
htmlFor={this.props.name}
1419
className="col-sm-4 control-label">
20+
{this.props.isCustom ? (
21+
<i
22+
className='fa fa-minus-circle'
23+
title='Remove Field'
24+
onClick={this.removeField.bind(this, this.props.name)}
25+
/>
26+
) : null}
1527
{this.props.name}
1628
</label>
1729
<div className="col-sm-8">

rest_framework_docs/static/rest_framework_docs/js/components/request.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
var _ = require('underscore');
12
var React = require('react');
23

4+
var AddFieldsForm = require('./request/add-fields');
35
var Header = require('./helpers/header');
46
var Headers = require('./request/headers');
57
var FieldsData = require('./request/fields-data');
@@ -29,6 +31,42 @@ var Request = React.createClass({
2931
});
3032
},
3133

34+
addField: function (fieldName) {
35+
var endpoint = this.state.endpoint;
36+
var fields = endpoint.fields;
37+
38+
// Check if field already exists
39+
if (_.findWhere(fields, {'name': fieldName})) return;
40+
41+
fields.push({
42+
name: fieldName,
43+
required: false,
44+
type: 'Added Field',
45+
isCustom: true
46+
});
47+
48+
endpoint.fields = fields;
49+
50+
this.setState({
51+
endpoint: endpoint
52+
});
53+
},
54+
55+
removeField: function (fieldName) {
56+
var data = this.state.data;
57+
var endpoint = this.state.endpoint;
58+
var fields = endpoint.fields;
59+
60+
data = _.omit(data, fieldName);
61+
fields = _.without(fields, _.findWhere(fields, {name: fieldName}));
62+
endpoint.fields = fields;
63+
64+
this.setState({
65+
data: data,
66+
endpoint: endpoint
67+
});
68+
},
69+
3270
setSelectedMethod: function (method) {
3371
this.setState({
3472
selectedMethod: method
@@ -87,7 +125,10 @@ var Request = React.createClass({
87125
<FieldsData
88126
fields={endpoint.fields}
89127
data={this.state.data}
128+
removeCustomField={this.removeField}
90129
onChange={this.handleDataFieldChange} />
130+
131+
<AddFieldsForm onAdd={this.addField} />
91132
</div>
92133
)}
93134
</div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var React = require('react');
2+
3+
var Header = require('../helpers/header');
4+
5+
var AddFieldsForm = React.createClass({
6+
7+
getInitialState: function() {
8+
return {
9+
fieldName: ''
10+
};
11+
},
12+
13+
addField: function () {
14+
if (this.state.fieldName) {
15+
this.props.onAdd(this.state.fieldName);
16+
this.setState({
17+
fieldName: ''
18+
});
19+
}
20+
},
21+
22+
handleKeyPress: function (event) {
23+
if (event.key === 'Enter') {
24+
event.preventDefault();
25+
this.addField();
26+
}
27+
},
28+
29+
handleChange: function (event) {
30+
this.setState({
31+
fieldName: event.target.value
32+
});
33+
},
34+
35+
render: function () {
36+
return (
37+
<div>
38+
<Header title='Add Extra Fields' />
39+
40+
<div className='form-group'>
41+
<label className='col-sm-4 control-label'>Field Name</label>
42+
<div className="col-sm-6">
43+
<input
44+
type='text'
45+
className='form-control input-sm'
46+
placeholder='ie. email_address'
47+
onKeyPress = {this.handleKeyPress}
48+
onChange={this.handleChange}
49+
value={this.state.fieldName} />
50+
</div>
51+
<div className="col-sm-2">
52+
<button
53+
type="button"
54+
className='btn btn-sm btn-block btn-primary'
55+
onClick={this.addField}>
56+
Add
57+
</button>
58+
</div>
59+
</div>
60+
</div>
61+
);
62+
}
63+
});
64+
65+
module.exports = AddFieldsForm;

rest_framework_docs/static/rest_framework_docs/js/components/request/fields-data.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ var Input = require('../helpers/input');
44

55
var FieldsData = React.createClass({
66

7+
removeCustomField: function (fieldName) {
8+
this.props.removeCustomField(fieldName);
9+
},
10+
711
handleChange: function (fieldName, event) {
812
this.props.onChange(event.target.value, fieldName);
913
},
@@ -21,6 +25,8 @@ var FieldsData = React.createClass({
2125
value={value}
2226
placeholder={field.type}
2327
required={field.required ? 'required' : false}
28+
removeField={this.removeCustomField}
29+
isCustom={field.isCustom ? 'isCustom' : false}
2430
onChange={this.handleChange.bind(this, field.name)} />
2531
);
2632
}, this);

rest_framework_docs/static/rest_framework_docs/js/dist.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest_framework_docs/static/rest_framework_docs/less/style.less

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ body {
208208
&.options.active { background-color: @OptionsColor; }
209209
}
210210
}
211+
212+
.form-group {
213+
214+
label {
215+
216+
.fa {
217+
margin-right: 5px;
218+
color: @brand-danger;
219+
}
220+
}
221+
}
211222
}
212223

213224
.response {
@@ -236,7 +247,7 @@ body {
236247
/* @end Modal API */
237248

238249

239-
/* @group jQuery / JSON View */
250+
/* @group Response / JSON View */
240251

241252
.json-key {
242253
color: brown;
@@ -250,7 +261,7 @@ body {
250261
color: olive;
251262
}
252263

253-
/* @end jQuery / JSON View */
264+
/* @end Response / JSON View */
254265

255266

256267
/* @group Github Ribbon - SVG */

0 commit comments

Comments
 (0)