-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved examples to es2015
- Loading branch information
Semigradsky
committed
Sep 10, 2015
1 parent
7ab78f9
commit 6a0d23c
Showing
10 changed files
with
205 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import Formsy from 'formsy-react'; | ||
|
||
const MyInput = React.createClass({ | ||
|
||
// Add the Formsy Mixin | ||
mixins: [Formsy.Mixin], | ||
|
||
// setValue() will set the value of the component, which in | ||
// turn will validate it and the rest of the form | ||
changeValue(event) { | ||
this.setValue(event.currentTarget[this.props.type === 'checkbox' ? 'checked' : 'value']); | ||
}, | ||
render() { | ||
|
||
// Set a specific className based on the validation | ||
// state of this component. showRequired() is true | ||
// when the value is empty and the required prop is | ||
// passed to the input. showError() is true when the | ||
// value typed is invalid | ||
const className = this.props.className + ' ' + (this.showRequired() ? 'required' : this.showError() ? 'error' : null); | ||
|
||
// An error message is returned ONLY if the component is invalid | ||
// or the server has returned an error message | ||
const errorMessage = this.getErrorMessage(); | ||
|
||
return ( | ||
<div className='form-group'> | ||
<label htmlFor={this.props.name}>{this.props.title}</label> | ||
<input | ||
type={this.props.type || 'text'} | ||
name={this.props.name} | ||
onChange={this.changeValue} | ||
value={this.getValue()} | ||
checked={this.props.type === 'checkbox' && this.getValue() ? 'checked' : null} | ||
/> | ||
<span className='validation-error'>{errorMessage}</span> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
export default MyInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import Formsy from 'formsy-react'; | ||
|
||
const MySelect = React.createClass({ | ||
mixins: [Formsy.Mixin], | ||
|
||
changeValue(event) { | ||
this.setValue(event.currentTarget.value); | ||
}, | ||
|
||
render() { | ||
const className = this.props.className + ' ' + (this.showRequired() ? 'required' : this.showError() ? 'error' : null); | ||
const errorMessage = this.getErrorMessage(); | ||
|
||
const value = this.getValue(); | ||
const options = this.props.options.map(option => ( | ||
<option value={option.value} selected={value === option.value ? 'selected' : null}> | ||
{option.title} | ||
</option> | ||
)); | ||
|
||
return ( | ||
<div className='form-group'> | ||
<label htmlFor={this.props.name}>{this.props.title}</label> | ||
<select name={this.props.name} onChange={this.changeValue}> | ||
{options} | ||
</select> | ||
<span className='validation-error'>{errorMessage}</span> | ||
</div> | ||
); | ||
} | ||
|
||
}); | ||
|
||
export default MySelect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.form { | ||
width: 400px; | ||
margin: 0 auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Form } from 'formsy-react'; | ||
|
||
import MyInput from './../components/Input'; | ||
import MySelect from './../components/Select'; | ||
|
||
const user = { | ||
name: 'Sam', | ||
free: true, | ||
hair: 'brown' | ||
}; | ||
|
||
const App = React.createClass({ | ||
submit(data) { | ||
alert(JSON.stringify(data, null, 4)); | ||
}, | ||
resetForm() { | ||
this.refs.form.reset(); | ||
}, | ||
render() { | ||
return ( | ||
<Formsy.Form ref="form" onSubmit={this.submit} className="form"> | ||
<MyInput name="name" title="Name" value={user.name} /> | ||
<MyInput name="free" title="Free to hire" type="checkbox" value={user.free} /> | ||
<MySelect name="hair" title="Hair" value={user.hair} | ||
options={[ | ||
{ value: "black", title: "Black" }, | ||
{ value: "brown", title: "Brown" }, | ||
{ value: "blonde", title: "Blonde" }, | ||
{ value: "red", title: "Red" } | ||
]} | ||
/> | ||
|
||
<div className="buttons"> | ||
<button type="reset" onClick={this.resetForm}>Reset</button> | ||
<button type="submit">Submit</button> | ||
</div> | ||
</Formsy.Form> | ||
); | ||
} | ||
}); | ||
|
||
ReactDOM.render(<App/>, document.getElementById('example')); |
Oops, something went wrong.