From 2c7f63177928da3e27e393ecaa18b88a8038546d Mon Sep 17 00:00:00 2001 From: Tyson Warner Date: Wed, 21 Jun 2017 14:56:50 -0400 Subject: [PATCH] Fix issue #15 --- docs/lib/Components/FormPage.js | 14 ++++++ docs/lib/examples/MobxForm.js | 36 ++++++++++++++++ docs/lib/examples/MobxFormComponent.js | 60 ++++++++++++++++++++++++++ package.json | 2 + src/AvFeedback.js | 5 ++- src/AvField.js | 5 ++- src/AvForm.js | 5 ++- src/AvGroup.js | 5 ++- src/AvInput.js | 6 ++- src/AvRadio.js | 5 ++- src/AvRadioGroup.js | 5 ++- 11 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 docs/lib/examples/MobxForm.js create mode 100644 docs/lib/examples/MobxFormComponent.js diff --git a/docs/lib/Components/FormPage.js b/docs/lib/Components/FormPage.js index 3d9b8ef..260e353 100644 --- a/docs/lib/Components/FormPage.js +++ b/docs/lib/Components/FormPage.js @@ -5,6 +5,8 @@ import Helmet from 'react-helmet'; import FormExample from '../examples/Form'; const FormExampleSource = require('!!raw!../examples/Form.js'); +import MobxFormExample from '../examples/MobxForm'; +const MobxFormExampleSource = require('!!raw!../examples/MobxForm.js'); import FormOnSubmitExample from '../examples/FormOnSubmit'; const FormOnSubmitExampleSource = require('!!raw!../examples/FormOnSubmit.js'); import FormOnValidSubmitExample from '../examples/FormOnValidSubmit'; @@ -21,6 +23,7 @@ export default class FormPage extends React.Component {

AvForm

The AvForm component wraps reactstrap's form to add context that the other Av components know about to help share validation state

+
@@ -30,6 +33,17 @@ export default class FormPage extends React.Component { +

AvForm with Mobx

+

The AvForm component wrapped by Mobx's observer

+
+ +
+
+          
+            {MobxFormExampleSource}
+          
+        
+

OnSubmit

This callback is called with every submission of the form

diff --git a/docs/lib/examples/MobxForm.js b/docs/lib/examples/MobxForm.js new file mode 100644 index 0000000..cc0a755 --- /dev/null +++ b/docs/lib/examples/MobxForm.js @@ -0,0 +1,36 @@ +import React, { Component } from 'react'; +import { extendObservable } from 'mobx'; +import { observer } from 'mobx-react'; +import { AvForm } from 'availity-reactstrap-validation'; +import MobxFormComponent from './MobxFormComponent'; + +export class Pokemon { + constructor(json) { + const _json = json || {}; + + extendObservable(this, { name: _json.name || ''}); + extendObservable(this, { rank: _json.rank || ''}); + extendObservable(this, { type: _json.type || ''}); + } +} + +let pokemon; +class MobxForm extends Component { + constructor() { + super(); + } + + componentWillMount() { + pokemon = new Pokemon(); + } + + render() { + return ( + + + + ); + } +} + +export default observer(MobxForm); diff --git a/docs/lib/examples/MobxFormComponent.js b/docs/lib/examples/MobxFormComponent.js new file mode 100644 index 0000000..16cadf2 --- /dev/null +++ b/docs/lib/examples/MobxFormComponent.js @@ -0,0 +1,60 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import mobx from 'mobx'; +import { observer } from 'mobx-react'; +import { AvField, AvGroup, AvInput, AvFeedback, AvRadio, AvRadioGroup } from 'availity-reactstrap-validation'; +import { Button, Label, FormGroup } from 'reactstrap'; +import { Pokemon } from './MobxForm'; + +export class MobxFormComponent extends Component { + constructor(props) { + super(props); + + this.updateProperty = ::this.updateProperty; + } + + updateProperty(event) { + const { pokemon } = this.props; + const { name, value } = event.target; + + pokemon[name] = value; + } + + render() { + const { pokemon } = this.props; + + return ( +
+ {/* With AvField */} + + {/* With AvGroup AvInput and AvFeedback to build your own */} + + + + This is an error! + + {/* Radios */} + + + + + + + + + + +
+ ); + } +} + +MobxFormComponent.propTypes = { + pokemon: PropTypes.shape({ + name: PropTypes.string, + rank: PropTypes.string, + type: PropTypes.string + }).isRequired +}; + +export default observer(MobxFormComponent); diff --git a/package.json b/package.json index 99abe85..7fd3661 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,8 @@ "babel-polyfill": "^6.13.0", "classnames": "^2.2.3", "lodash": "^4.17.4", + "mobx": "^3.1.16", + "mobx-react": "^4.2.1", "moment": "^2.14.1" }, "peerDependencies": { diff --git a/src/AvFeedback.js b/src/AvFeedback.js index b7fc6cd..1811ab9 100644 --- a/src/AvFeedback.js +++ b/src/AvFeedback.js @@ -1,8 +1,9 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; import { FormFeedback } from 'reactstrap'; +import {observer} from 'mobx-react'; -export default class AvFeedback extends Component { +export class AvFeedback extends Component { static propTypes = Object.assign({}, FormFeedback.propTypes); static contextTypes = { @@ -15,3 +16,5 @@ export default class AvFeedback extends Component { return validation.color ? : null; } } + +export default observer(AvFeedback); diff --git a/src/AvField.js b/src/AvField.js index 8838935..98c15a8 100644 --- a/src/AvField.js +++ b/src/AvField.js @@ -4,10 +4,11 @@ import AvInput from './AvInput'; import AvGroup from './AvGroup'; import AvFeedback from './AvFeedback'; import {Col, FormText, Label} from 'reactstrap'; +import {observer} from 'mobx-react'; const colSizes = ['xs', 'sm', 'md', 'lg', 'xl']; -export default class AvField extends Component { +export class AvField extends Component { static propTypes = Object.assign({}, AvInput.propTypes, { label: PropTypes.node, labelHidden: PropTypes.bool, @@ -101,3 +102,5 @@ export default class AvField extends Component { ); } } + +export default observer(AvField); diff --git a/src/AvForm.js b/src/AvForm.js index c6ca842..42af528 100644 --- a/src/AvForm.js +++ b/src/AvForm.js @@ -5,6 +5,7 @@ import AvValidator from './AvValidator'; import { Form } from 'reactstrap'; import classNames from 'classnames'; import {get as _get, set as _set, isString} from 'lodash'; +import {observer} from 'mobx-react'; const getInputErrorMessage = (input, ruleName) => { const errorMessage = input.props.errorMessage; @@ -15,7 +16,7 @@ const getInputErrorMessage = (input, ruleName) => { return errorMessage; }; -export default class AvForm extends InputContainer { +export class AvForm extends InputContainer { static childContextTypes = { FormCtrl: PropTypes.object.isRequired, }; @@ -447,3 +448,5 @@ export default class AvForm extends InputContainer { return input.getValue(); } } + +export default observer(AvForm); diff --git a/src/AvGroup.js b/src/AvGroup.js index 4e2af8f..ae66d28 100644 --- a/src/AvGroup.js +++ b/src/AvGroup.js @@ -1,8 +1,9 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; import { FormGroup } from 'reactstrap'; +import {observer} from 'mobx-react'; -export default class AvGroup extends Component { +export class AvGroup extends Component { static propTypes = Object.assign({}, FormGroup.propTypes); static contextTypes = { @@ -47,3 +48,5 @@ export default class AvGroup extends Component { ); } } + +export default observer(AvGroup); diff --git a/src/AvInput.js b/src/AvInput.js index 7d0e1c6..a16d567 100644 --- a/src/AvInput.js +++ b/src/AvInput.js @@ -2,9 +2,9 @@ import React from 'react'; import classNames from 'classnames'; import {Input} from 'reactstrap'; import AvBaseInput from './AvBaseInput'; +import {observer} from 'mobx-react'; - -export default class AvInput extends AvBaseInput { +export class AvInput extends AvBaseInput { static defaultProps = Object.assign({}, AvBaseInput.defaultProps, { tag: Input, }); @@ -41,3 +41,5 @@ export default class AvInput extends AvBaseInput { ); } } + +export default observer(AvInput); diff --git a/src/AvRadio.js b/src/AvRadio.js index 7b81cda..4345b7a 100644 --- a/src/AvRadio.js +++ b/src/AvRadio.js @@ -3,11 +3,12 @@ import PropTypes from 'prop-types'; import classNames from 'classnames'; import {Input, FormGroup, Label} from 'reactstrap'; import AvInput from './AvInput'; +import {observer} from 'mobx-react'; const radioPropTypes = Object.assign({}, AvInput.propTypes); delete radioPropTypes.name; -export default class AvRadio extends Component { +export class AvRadio extends Component { static contextTypes = Object.assign({}, AvInput.contextTypes, { Group: PropTypes.object.isRequired, @@ -57,3 +58,5 @@ export default class AvRadio extends Component { ); } } + +export default observer(AvRadio); diff --git a/src/AvRadioGroup.js b/src/AvRadioGroup.js index dbffdd5..ef863e4 100644 --- a/src/AvRadioGroup.js +++ b/src/AvRadioGroup.js @@ -4,12 +4,13 @@ import InputContainer from './AvInputContainer'; import AvFeedback from './AvFeedback'; import { isEqual } from 'lodash'; import { FormGroup } from 'reactstrap'; +import {observer} from 'mobx-react'; const htmlValidationAttrs = ['required']; const noop = () => {}; -export default class AvRadioGroup extends InputContainer { +export class AvRadioGroup extends InputContainer { static propTypes = Object.assign({}, FormGroup.propTypes, { name: PropTypes.string.isRequired, }); @@ -167,3 +168,5 @@ export default class AvRadioGroup extends InputContainer { ); } } + +export default observer(AvRadioGroup);