Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Extra Field to Live API #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
manosim merged 6 commits into master from move-packagejson
Dec 28, 2015
Merged
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
View file Open in desktop

Large diffs are not rendered by default.

View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var React = require('react');

var Input = React.createClass({

removeField: function (fieldName, event) {
event.preventDefault();
Copy link

@JakeSidSmith JakeSidSmith Dec 28, 2015

Choose a reason for hiding this comment

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

Why preventDefault

Copy link
Owner Author

Choose a reason for hiding this comment

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

It is placed within a <label> so on Click it focuses to the input which is about to get removed.

Copy link

@JakeSidSmith JakeSidSmith Dec 28, 2015

Choose a reason for hiding this comment

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

👍

this.props.removeField(fieldName);
},

handleChange: function (value) {
this.props.onChange(value);
},
Expand All @@ -12,6 +17,13 @@ var Input = React.createClass({
<label
htmlFor={this.props.name}
className="col-sm-4 control-label">
{this.props.isCustom ? (
<i
className='fa fa-minus-circle'
title='Remove Field'
onClick={this.removeField.bind(this, this.props.name)}
/>
) : null}
{this.props.name}
</label>
<div className="col-sm-8">
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var _ = require('underscore');
var React = require('react');

var AddFieldsForm = require('./request/add-fields');
var Header = require('./helpers/header');
var Headers = require('./request/headers');
var FieldsData = require('./request/fields-data');
Expand Down Expand Up @@ -29,6 +31,42 @@ var Request = React.createClass({
});
},

addField: function (fieldName) {
var endpoint = this.state.endpoint;
var fields = endpoint.fields;

// Check if field already exists
if (_.findWhere(fields, {'name': fieldName})) return;

fields.push({
name: fieldName,
required: false,
type: 'Added Field',
isCustom: true
});

endpoint.fields = fields;
Copy link

@JakeSidSmith JakeSidSmith Dec 28, 2015

Choose a reason for hiding this comment

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

I would _.extend({}, here to avoid mutation.

Copy link

@JakeSidSmith JakeSidSmith Dec 28, 2015

Choose a reason for hiding this comment

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

Wait. No need to do this... Push mutates the array anyway.

Copy link
Owner Author

Choose a reason for hiding this comment

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

👍


this.setState({
endpoint: endpoint
});
},

removeField: function (fieldName) {
var data = this.state.data;
var endpoint = this.state.endpoint;
var fields = endpoint.fields;

data = _.omit(data, fieldName);
fields = _.without(fields, _.findWhere(fields, {name: fieldName}));
endpoint.fields = fields;

this.setState({
data: data,
endpoint: endpoint
});
},

setSelectedMethod: function (method) {
this.setState({
selectedMethod: method
Expand Down Expand Up @@ -87,7 +125,10 @@ var Request = React.createClass({
<FieldsData
fields={endpoint.fields}
data={this.state.data}
removeCustomField={this.removeField}
onChange={this.handleDataFieldChange} />

<AddFieldsForm onAdd={this.addField} />
</div>
)}
</div>
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var React = require('react');

var Header = require('../helpers/header');

var AddFieldsForm = React.createClass({

getInitialState: function() {
return {
fieldName: ''
};
},

addField: function () {
if (this.state.fieldName) {
this.props.onAdd(this.state.fieldName);
this.setState({
fieldName: ''
});
}
},

handleKeyPress: function (event) {
if (event.key === 'Enter') {
event.preventDefault();
this.addField();
}
},

handleChange: function (event) {
this.setState({
fieldName: event.target.value
});
},

render: function () {
return (
<div>
<Header title='Add Extra Fields' />

<div className='form-group'>
<label className='col-sm-4 control-label'>Field Name</label>
<div className="col-sm-6">
<input
type='text'
className='form-control input-sm'
placeholder='ie. email_address'
onKeyPress = {this.handleKeyPress}
onChange={this.handleChange}
value={this.state.fieldName} />
</div>
<div className="col-sm-2">
<button
type="button"
className='btn btn-sm btn-block btn-primary'
onClick={this.addField}>
Add
</button>
</div>
</div>
</div>
);
}
});

module.exports = AddFieldsForm;
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var Input = require('../helpers/input');

var FieldsData = React.createClass({

removeCustomField: function (fieldName) {
this.props.removeCustomField(fieldName);
},

handleChange: function (fieldName, event) {
this.props.onChange(event.target.value, fieldName);
},
Expand All @@ -21,6 +25,8 @@ var FieldsData = React.createClass({
value={value}
placeholder={field.type}
required={field.required ? 'required' : false}
removeField={this.removeCustomField}
isCustom={field.isCustom ? 'isCustom' : false}
onChange={this.handleChange.bind(this, field.name)} />
);
}, this);
Expand Down
21 changes: 11 additions & 10 deletions rest_framework_docs/static/rest_framework_docs/js/dist.js
View file Open in desktop

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions rest_framework_docs/static/rest_framework_docs/less/style.less
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ body {
&.options.active { background-color: @OptionsColor; }
}
}

.form-group {

label {

.fa {
margin-right: 5px;
color: @brand-danger;
}
}
}
}

.response {
Expand Down Expand Up @@ -236,7 +247,7 @@ body {
/* @end Modal API */


/* @group jQuery / JSON View */
/* @group Response / JSON View */

.json-key {
color: brown;
Expand All @@ -250,7 +261,7 @@ body {
color: olive;
}

/* @end jQuery / JSON View */
/* @end Response / JSON View */


/* @group Github Ribbon - SVG */
Expand Down

AltStyle によって変換されたページ (->オリジナル) /