Skip to content

Commit

Permalink
feat: ✨ Add dynamic update rule for MobxFormData
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-sky committed Jun 5, 2020
1 parent bf02c13 commit bd17660
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 57 deletions.
26 changes: 12 additions & 14 deletions docs/mobxFormData/demo/Demo4/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { Form, Input, Button, Icon } from 'antd';
import { Form, Input, Button } from 'antd';
import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { useLocalStore, useObserver } from 'mobx-react-lite';
import './style.less';
import nj from 'nornj';

const formItemLayout = {
labelCol: {
Expand Down Expand Up @@ -48,20 +48,18 @@ export default () => {
<each of={formData.fieldDatas} $key="fieldName">
<Form.Item
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
n-mobxField={`formData[fieldName]`}
label={index === 0 ? 'Passengers' : ''}
required={false}
key={fieldName}>
<Form.Item n-mobxField={`formData[fieldName]`}>
<Input placeholder="passenger name" style={{ width: '60%' }} />
<if condition={formData.fieldDatas.size > 1}>
<Icon
type="minus-circle"
className="dynamic-delete-button"
style={{ margin: '0 8px' }}
onClick={() => formData.delete(fieldName)}
/>
</if>
</Form.Item>
<Input placeholder="passenger name" style={{ width: '60%' }} />
<if condition={formData.fieldDatas.size > 1}>
<MinusCircleOutlined
className="dynamic-delete-button"
style={{ margin: '0 8px' }}
onClick={() => formData.delete(fieldName)}
/>
</if>
</Form.Item>
</each>
<Form.Item>
Expand All @@ -80,7 +78,7 @@ export default () => {
count++;
}}
style={{ width: '60%' }}>
<Icon type="plus" /> Add field
<PlusCircleOutlined /> Add field
</Button>
</Form.Item>
</div>
Expand Down
4 changes: 0 additions & 4 deletions docs/mobxFormData/demo/Demo4/style.less
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
.dynamic-form {
max-width: 600px;

.ant-form-item {
margin-bottom: 10px;
}
}

.dynamic-delete-button {
Expand Down
67 changes: 67 additions & 0 deletions docs/mobxFormData/demo/Demo6.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useEffect } from 'react';
import { Form, Input, Button, Checkbox } from 'antd';
import { useLocalStore } from 'mobx-react-lite';
import { observer } from 'mobx-react';
import { reaction, autorun } from 'mobx';

const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 }
};
const tailLayout = {
wrapperCol: { offset: 8, span: 16 }
};

const Demo = props => {
const state = useLocalStore(() => ({
rememberMe: false
}));

const { formData } = useLocalStore(() => (
<MobxFormData>
<MobxFieldData name="userName" required message="Please input your username!" />
<MobxFieldData name="password" message="Please input your password!" />
</MobxFormData>
));

const onSubmit = (e: React.MouseEvent<HTMLElement, MouseEvent>) =>
formData
.validate()
.then(values => {
console.log(values);
})
.catch(errorInfo => {
console.log(errorInfo);
});

const onRememberMeChange = () => {
formData.fieldDatas.get('password').rules[0].required = state.rememberMe;
formData.validate('password');
};

return (
<Form {...layout} n-style="max-width:600">
<Form.Item n-mobxField={formData.userName} label="Username">
<Input />
</Form.Item>

<Form.Item n-mobxField={formData.password} label="Password">
<Input.Password />
</Form.Item>

<Form.Item {...tailLayout}>
<Checkbox n-mobxBind={state.rememberMe} onChange={onRememberMeChange}>
Remember me
</Checkbox>
</Form.Item>

<Form.Item {...tailLayout}>
<Button type="primary" onClick={onSubmit}>
Submit
</Button>
</Form.Item>
</Form>
);
};

export default observer(Demo);
2 changes: 2 additions & 0 deletions docs/mobxFormData/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ toc: menu
通过`validateMessages`自定义校验信息模板,模板内容可参考[此处](https://github.com/yiminghe/async-validator)

<code src="./demo/Demo5" />

<code src="./demo/Demo6" />
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"@types/jest": "^24.0.22",
"@types/jsdom": "^12.2.4",
"@types/lodash": "^4.14.148",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.5.0",
"babel-jest": "^26.0.1",
Expand Down
4 changes: 1 addition & 3 deletions packages/babel-plugin-nornj-in-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ module.exports = function(babel) {
}

if (state.hasMobxWithNj && !state.hasImportNjrMobx) {
path.node.body.unshift(
types.importDeclaration([], types.stringLiteral(`nornj-react/mobx${state.opts.rn ? '/native' : ''}`))
);
path.node.body.unshift(types.importDeclaration([], types.stringLiteral('nornj-react/mobx')));
}
if (state.hasMobxFormDataWithNj && !state.hasImportNjrMobxFormData) {
path.node.body.unshift(types.importDeclaration([], types.stringLiteral('nornj-react/mobx/formData')));
Expand Down
1 change: 0 additions & 1 deletion packages/nornj-react/mobx/base.native.js

This file was deleted.

6 changes: 3 additions & 3 deletions packages/nornj-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"umd:router": "cross-env NODE_ENV=development WITH_EX=Router rollup -c -o dist/nornj-react-router.js",
"min:router": "cross-env NODE_ENV=production WITH_EX=Router rollup -c -o dist/nornj-react-router.min.js",
"lib": "babel src --out-dir lib --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
"lib:mobx": "babel mobx/base.js --out-file mobx/index.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\" && babel mobx/base.native.js --out-file mobx/native.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
"lib:mobx": "babel mobx/base.js --out-file mobx/index.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
"lib:mobxFormData": "babel mobx/formData/base.js --out-file mobx/formData/index.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
"lib:redux": "babel redux/base.js --out-file redux/index.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
"lib:router": "babel router/base.js --out-file router/index.js --extensions \".js,.jsx,.ts,.tsx\" --config-file \"../../.babelrc\"",
Expand Down Expand Up @@ -69,8 +69,8 @@
"babel-plugin-module-resolver": "^3.2.0",
"babel-plugin-nornj-in-jsx": "^5.2.0-rc.3",
"echarts-for-react": "^2.0.15-beta.0",
"mobx": "^5.9.0",
"mobx-react": "^5.4.3",
"mobx": "^5.15.4",
"mobx-react": "^6.2.2",
"mobx-react-lite": "^1.2.0",
"nornj": "^5.2.0-rc.3",
"react-redux": "^7.1.3",
Expand Down
8 changes: 0 additions & 8 deletions packages/nornj-react/src/extension/mobx/base.native.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/nornj-react/src/extension/mobx/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { registerComponent, registerFilter } from 'nornj';
import { registerFilter } from 'nornj';
import { toJS } from 'mobx';
import { Provider } from 'mobx-react';
import './mobxBind';
import './mobxObserver';

registerComponent('mobx-Provider', Provider);
registerFilter('toJS', v => toJS(v));
22 changes: 17 additions & 5 deletions packages/nornj-react/src/extension/mobx/mobxFormData.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nj, { registerExtension } from 'nornj';
import { observable, runInAction, reaction, extendObservable, isComputedProp } from 'mobx';
import { observable, runInAction, reaction, extendObservable, isComputedProp, observe } from 'mobx';
import schema, { RuleItem } from 'async-validator';
import extensionConfigs from '../../../mobx/formData/extensionConfig';
import { MobxFormDataProps, MobxFormDataInstance, MobxFieldDataProps, MobxFieldDataInstance } from '../../interface';
Expand Down Expand Up @@ -104,7 +104,15 @@ const createFormData = (
const { name, value, trigger = 'valueChange', rules, ...ruleOptions } = fieldData;
const fd: MobxFieldDataInstance = { name, value, trigger, rules, ...ruleOptions };
const _rules = rules ? rules : [ruleOptions];
fd.rules = _rules;
fd.rules = _rules.map((rule, i) => {
const oRule = observable(rule);
observe(oRule, change => {
const schemaRules: RuleItem[] = (fd.validatorSchema as any).rules[name];
Object.assign(schemaRules[i], oRule);
});

return oRule;
});

fd.setDefaultRule = rule => {
const schemaRules: RuleItem[] = (fd.validatorSchema as any).rules[name];
Expand Down Expand Up @@ -140,9 +148,10 @@ const createFormData = (
});

fd.reset = function() {
this._resetting = true;
if (this.value !== value) {
this._resetting = true;
}
this.value = value;
this._resetting = false;
};

const oFd = observable(fd);
Expand Down Expand Up @@ -177,6 +186,7 @@ const createFormData = (
if (!oFd._resetting) {
this.validate(name).catch(nj.noop);
}
oFd._resetting = false;
}
);
}
Expand Down Expand Up @@ -242,7 +252,9 @@ registerExtension(

tagProps.validateStatus = oFd.validateStatus;
tagProps.help = oFd.help;
tagProps.required = oFd.rules.find(rule => rule.required);
if (tagProps.required == null) {
tagProps.required = oFd.rules.find(rule => rule.required);
}
if (tagProps.label) {
oFd.label = tagProps.label;
} else if (oFd.label) {
Expand Down
46 changes: 30 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3892,6 +3892,13 @@
dependencies:
"@types/react" "*"

"@types/react-dom@^16.9.8":
version "16.9.8"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.9.8.tgz#fe4c1e11dfc67155733dfa6aa65108b4971cb423"
integrity sha512-ykkPQ+5nFknnlU6lDd947WbQ6TE3NNzbQAkInC2EKY1qeYdTKp7onFusmYZb+ityzx2YviqT6BXSu+LyWWJwcA==
dependencies:
"@types/react" "*"

"@types/react-native@^0.60.22":
version "0.60.22"
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.60.22.tgz#ba199a441cb0612514244ffb1d0fe6f04c878575"
Expand Down Expand Up @@ -3969,6 +3976,14 @@
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/react@^16.9.35":
version "16.9.35"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.35.tgz#a0830d172e8aadd9bd41709ba2281a3124bbd368"
integrity sha512-q0n0SsWcGc8nDqH2GJfWQWUOmZSJhXV64CjVN5SvcNti3TdEaA3AH0D8DwNmMdzjMAC/78tB8nAZIlV8yTz+zQ==
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/resolve@0.0.8":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
Expand Down Expand Up @@ -10055,7 +10070,7 @@ hoek@2.x.x:
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"
integrity sha1-ILt0A9POo5jpHcRxCo/xuCdKJe0=

hoist-non-react-statics@^3.0.0, hoist-non-react-statics@^3.1.0, hoist-non-react-statics@^3.3.0:
hoist-non-react-statics@^3.1.0, hoist-non-react-statics@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.0.tgz#b09178f0122184fb95acf525daaecb4d8f45958b"
integrity sha512-0XsbTXxgiaCDYDIWFcwkmerZPSwywfUqYmwT4jzewKTQSWoE6FCMoUVOeBJWK3E/CrWbxRG3m5GzY4lnIwGRBA==
Expand Down Expand Up @@ -13013,23 +13028,27 @@ mkdirp@^0.5.3, mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"

mobx-react-lite@2:
version "2.0.7"
resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-2.0.7.tgz#1bfb3b4272668e288047cf0c7940b14e91cba284"
integrity sha512-YKAh2gThC6WooPnVZCoC+rV1bODAKFwkhxikzgH18wpBjkgTkkR9Sb0IesQAH5QrAEH/JQVmy47jcpQkf2Au3Q==

mobx-react-lite@^1.2.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-1.5.0.tgz#a5867fa1114b19056cf8159e8d64766596ae85a0"
integrity sha512-Ss8RLKKGn+QhKbfCHvQ4+RPEVKR8AnPW1wNyWzZAS3wYw7UP4FX6GdRn64sdOhrP646o/JtXbLuDuc4RH3Bqyg==

mobx-react@^5.4.3:
version "5.4.4"
resolved "https://registry.yarnpkg.com/mobx-react/-/mobx-react-5.4.4.tgz#b3de9c6eabcd0ed8a40036888cb0221ab9568b80"
integrity sha512-2mTzpyEjVB/RGk2i6KbcmP4HWcAUFox5ZRCrGvSyz49w20I4C4qql63grPpYrS9E9GKwgydBHQlA4y665LuRCQ==
mobx-react@^6.2.2:
version "6.2.2"
resolved "https://registry.yarnpkg.com/mobx-react/-/mobx-react-6.2.2.tgz#45e8e7c4894cac8399bba0a91060d7cfb8ea084b"
integrity sha512-Us6V4ng/iKIRJ8pWxdbdysC6bnS53ZKLKlVGBqzHx6J+gYPYbOotWvhHZnzh/W5mhpYXxlXif4kL2cxoWJOplQ==
dependencies:
hoist-non-react-statics "^3.0.0"
react-lifecycles-compat "^3.0.2"
mobx-react-lite "2"

mobx@^5.9.0:
version "5.14.2"
resolved "https://registry.yarnpkg.com/mobx/-/mobx-5.14.2.tgz#608b8ee9bc9f9e406b48da676e677b150e901eba"
integrity sha512-yx5Xe6o2WSYFgeytzZt6jGaYghJdQbd1ElR7S2s93x7/+5SYfJBfutvZF1O5gPEsUyTAFZ5IMYGu1KyhkPk+oQ==
mobx@^5.15.4:
version "5.15.4"
resolved "https://registry.yarnpkg.com/mobx/-/mobx-5.15.4.tgz#9da1a84e97ba624622f4e55a0bf3300fb931c2ab"
integrity sha512-xRFJxSU2Im3nrGCdjSuOTFmxVDGeqOHL+TyADCGbT0k4HHqGmx5u2yaHNryvoORpI4DfbzjJ5jPmuv+d7sioFw==

mocha@^3.0.2:
version "3.5.3"
Expand Down Expand Up @@ -15722,11 +15741,6 @@ react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4, react-is
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.11.0.tgz#b85dfecd48ad1ce469ff558a882ca8e8313928fa"
integrity sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==

react-lifecycles-compat@^3.0.2:
version "3.0.4"
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==

react-redux@^7.1.3:
version "7.1.3"
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-7.1.3.tgz#717a3d7bbe3a1b2d535c94885ce04cdc5a33fc79"
Expand Down

0 comments on commit bd17660

Please sign in to comment.