From 6a0d23cc036bfe5ec33fec9116653297c1a592be Mon Sep 17 00:00:00 2001 From: Semigradsky Date: Thu, 10 Sep 2015 15:26:17 +0300 Subject: [PATCH] Added `reset-values` example. Moved examples to es2015 --- examples/components/Input.js | 43 +++++++++++++++ examples/components/Select.js | 35 ++++++++++++ examples/custom-validation/app.js | 90 ++++++++++++------------------- examples/global.css | 12 ++++- examples/index.html | 3 +- examples/login/app.js | 69 ++++++------------------ examples/reset-values/app.css | 4 ++ examples/reset-values/app.js | 44 +++++++++++++++ examples/reset-values/index.html | 14 +++++ examples/webpack.config.js | 2 +- 10 files changed, 205 insertions(+), 111 deletions(-) create mode 100644 examples/components/Input.js create mode 100644 examples/components/Select.js create mode 100644 examples/reset-values/app.css create mode 100644 examples/reset-values/app.js create mode 100644 examples/reset-values/index.html diff --git a/examples/components/Input.js b/examples/components/Input.js new file mode 100644 index 00000000..a73e060a --- /dev/null +++ b/examples/components/Input.js @@ -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 ( +
+ + + {errorMessage} +
+ ); + } +}); + +export default MyInput; diff --git a/examples/components/Select.js b/examples/components/Select.js new file mode 100644 index 00000000..f8d9afa8 --- /dev/null +++ b/examples/components/Select.js @@ -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 => ( + + )); + + return ( +
+ + + {errorMessage} +
+ ); + } + +}); + +export default MySelect; diff --git a/examples/custom-validation/app.js b/examples/custom-validation/app.js index beef6293..886e583c 100644 --- a/examples/custom-validation/app.js +++ b/examples/custom-validation/app.js @@ -1,10 +1,12 @@ -var React = require('react'); -var ReactDOM = require('react-dom'); -var Formsy = require('formsy-react'); +import React from 'react'; +import ReactDOM from 'react-dom'; +import Formsy from 'formsy-react'; -var currentYear = new Date().getFullYear(); +import MyInput from './../components/Input'; -var validators = { +const currentYear = new Date().getFullYear(); + +const validators = { time: { regexp: /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/, message: 'Not valid time' @@ -19,79 +21,56 @@ var validators = { } }; -Formsy.addValidationRule('isYearOfBirth', function (values, value) { +Formsy.addValidationRule('isYearOfBirth', (values, value) => { value = parseInt(value); - if (typeof value !== 'number' || value !== value) { + if (typeof value !== 'number') { return false; } return value < currentYear && value > currentYear - 130; }); -var App = React.createClass({ - submit: function (data) { +const App = React.createClass({ + submit(data) { alert(JSON.stringify(data, null, 4)); }, - render: function () { + render() { return ( - - + + ); } }); -var MyOwnInput = React.createClass({ - mixins: [Formsy.Mixin], - changeValue: function (event) { - this.setValue(event.currentTarget.value); - }, - render: function () { - var className = this.props.className + ' ' + (this.showError() ? 'error' : null); - var errorMessage = this.getErrorMessage(); - - return ( -
- - - {errorMessage} -
- ); - } -}); - -var DynamicInput = React.createClass({ +const DynamicInput = React.createClass({ mixins: [Formsy.Mixin], - getInitialState: function() { - return { - validationType: 'time' - }; + getInitialState() { + return { validationType: 'time' }; }, - changeValue: function (event) { + changeValue(event) { this.setValue(event.currentTarget.value); }, - changeValidation: function(validationType) { - this.setState({ - validationType: validationType - }); + changeValidation(validationType) { + this.setState({ validationType: validationType }); this.setValue(this.getValue()); }, - validate: function () { - var value = this.getValue(); - return value === '' ? true : validators[this.state.validationType].regexp.test(value); + validate() { + const value = this.getValue(); + return value !== '' ? validators[this.state.validationType].regexp.test(value) : true; }, - getCustomErrorMessage: function() { + getCustomErrorMessage() { return this.showError() ? validators[this.state.validationType].message : ''; }, - render: function () { - var className = this.props.className + ' ' + (this.showError() ? 'error' : null); - var errorMessage = this.getCustomErrorMessage(); + render() { + const className = this.props.className + ' ' + (this.showError() ? 'error' : null); + const errorMessage = this.getCustomErrorMessage(); return (
- + {errorMessage}
@@ -99,22 +78,23 @@ var DynamicInput = React.createClass({ } }); -var Validations = React.createClass({ - changeValidation: function(e) { +const Validations = React.createClass({ + changeValidation(e) { this.props.changeValidation(e.target.value); }, - render: function() { + render() { + const { validationType } = this.props; return (
Validation Type
- Time + Time
- Decimal + Decimal
- Binary + Binary
); diff --git a/examples/global.css b/examples/global.css index 15a2a75d..ebdad7f2 100644 --- a/examples/global.css +++ b/examples/global.css @@ -38,7 +38,8 @@ form { .form-group input[type='text'], .form-group input[type='email'], .form-group input[type='number'], -.form-group input[type='password'] { +.form-group input[type='password'], +.form-group select { display: block; width: 100%; height: 34px; @@ -62,4 +63,11 @@ form { button { padding: 10px 15px; border-radius: 4px; -} \ No newline at end of file +} + +.buttons button { + margin-left: 10px; +} +.buttons button:first-child { + margin-left: 0; +} diff --git a/examples/index.html b/examples/index.html index d813a18f..175c07b1 100644 --- a/examples/index.html +++ b/examples/index.html @@ -9,6 +9,7 @@

Formsy React Examples

- \ No newline at end of file + diff --git a/examples/login/app.js b/examples/login/app.js index d6e0da85..1de0d0ae 100644 --- a/examples/login/app.js +++ b/examples/login/app.js @@ -1,64 +1,29 @@ -var React = require('react'); -var ReactDOM = require('react-dom'); -var Formsy = require('formsy-react'); +import React from 'react'; +import ReactDOM from 'react-dom'; +import { Form } from 'formsy-react'; -var App = React.createClass({ - getInitialState: function() { +import MyInput from './../components/Input'; + +const App = React.createClass({ + getInitialState() { return { canSubmit: false }; }, - submit: function (data) { + submit(data) { alert(JSON.stringify(data, null, 4)); }, - enableButton: function () { - this.setState({ - canSubmit: true - }); + enableButton() { + this.setState({ canSubmit: true }); }, - disableButton: function () { - this.setState({ - canSubmit: false - }); + disableButton() { + this.setState({ canSubmit: false }); }, - render: function () { + render() { return ( - - - +
+ + - - ); - } -}); - -var MyOwnInput = 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: function (event) { - this.setValue(event.currentTarget.value); - }, - render: function () { - - // 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 - var 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 - var errorMessage = this.getErrorMessage(); - - return ( -
- - - {errorMessage} -
+ ); } }); diff --git a/examples/reset-values/app.css b/examples/reset-values/app.css new file mode 100644 index 00000000..e9e02dc6 --- /dev/null +++ b/examples/reset-values/app.css @@ -0,0 +1,4 @@ +.form { + width: 400px; + margin: 0 auto; +} diff --git a/examples/reset-values/app.js b/examples/reset-values/app.js new file mode 100644 index 00000000..d5deb8f4 --- /dev/null +++ b/examples/reset-values/app.js @@ -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 ( + + + + + +
+ + +
+
+ ); + } +}); + +ReactDOM.render(, document.getElementById('example')); diff --git a/examples/reset-values/index.html b/examples/reset-values/index.html new file mode 100644 index 00000000..036a23ab --- /dev/null +++ b/examples/reset-values/index.html @@ -0,0 +1,14 @@ + + + + Reset Values + + + + +

Formsy React Examples / Reset Values

+
+ + + + diff --git a/examples/webpack.config.js b/examples/webpack.config.js index ca9992b6..06bbc10c 100644 --- a/examples/webpack.config.js +++ b/examples/webpack.config.js @@ -11,7 +11,7 @@ module.exports = { devtool: 'inline-source-map', entry: fs.readdirSync(__dirname).reduce(function (entries, dir) { - var isDraft = dir.charAt(0) === '_'; + var isDraft = dir.charAt(0) === '_' || dir.indexOf('components') >= 0; if (!isDraft && isDirectory(path.join(__dirname, dir))) { entries[dir] = path.join(__dirname, dir, 'app.js');