diff --git a/snippets/js-mode/react/modal-validation.yasnippet b/snippets/js-mode/react/modal-validation.yasnippet new file mode 100644 index 0000000000..a1beb320ef --- /dev/null +++ b/snippets/js-mode/react/modal-validation.yasnippet @@ -0,0 +1,156 @@ +# -*- coding: utf-8; mode: snippet -*- +# name: modal with form validation (redux-form required) +# expand-env: ((yas-indent-line 'fixed)) +# key: modal +# contributor: Chen Bin +# -- +// add `import { reducer as formReducer } from 'redux-form'; combineReducers({form: formReducer})` into index.js +import React from 'react'; +import {connect} from 'react-redux'; +import { Field, reduxForm } from 'redux-form'; + +function validatorRequired(val) { + return !val && 'Required'; +} + +function validatorAlphaNumeric(val) { + return !/^[a-zA-Z0-9]+$/.test(val) && 'Please input alphanumberica characters'; +} + +import { + Button, + Input, + Modal, + FormGroup, + ControlLabel, + FormControl, + HelpBlock, + Form, + Col, +} from 'react-bootstrap'; + +class InputText extends React.Component { + + getlabelWidth() { + return this.props.labelWidth? this.props.labelWidth: 2; + } + + getFormControlWidth() { + return 12 - this.getlabelWidth(); + } + + getFormControl() { + var input = this.props.input; + return ( + + ); + } + + getValidationMessage(meta) { + if(!meta) return null; + return ( + + {(meta.dirty || meta.touched) && ((meta.error && {meta.error}) || (meta.warning && {meta.warning}))} + + ); + } + + render () { + // (meta.dirty === !meta.pristine) => if user has modify content of the control yet + // meta.touched => if the control has lost focus yet + const { placeholder, type, input, meta} = this.props; + + // horizontal layout + if(this.props.horizontal) { + return( + + {this.props.children} + + { this.getFormControl() } + + { this.getValidationMessage(meta) } + + + ); + } + + // normal layout + return ( + + {this.props.children} + { this.getFormControl() } + + { this.getValidationMessage() } + + ); + } +} + +export class `(file-name-base buffer-file-name)` extends React.Component { + constructor(props) { + super(props); + this.close = this.close.bind(this); + this.save = this.save.bind(this); + } + + close() { + this.props.reset(); + this.props.dispatch({ + type: 'EVT_SHOW_`(upcase (file-name-base buffer-file-name) )`', + show`(file-name-base buffer-file-name)`: false + }); + } + + save(values) { + console.log('`(file-name-base buffer-file-name)`.js: `(file-name-base buffer-file-name)`.save called => ', 'values=', values); + this.close(); + } + + render() { + const { error, handleSubmit, pristine, submitting } = this.props; + return ( + +
+ + Add bookmark + + + + Name * + +
+
+ + + + +
+
+ ); + } +} + +export default connect( + function (storeState) { + // store state to props + return { + show`(file-name-base buffer-file-name)`: storeState.app.show`(file-name-base buffer-file-name)` + }; + } +)(reduxForm({ + form: '`(file-name-base buffer-file-name)`' // a unique name for this form +})(`(file-name-base buffer-file-name)`)); \ No newline at end of file