-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Add dynamic update rule for MobxFormData
- Loading branch information
Showing
12 changed files
with
135 additions
and
57 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
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 |
---|---|---|
@@ -1,9 +1,5 @@ | ||
.dynamic-form { | ||
max-width: 600px; | ||
|
||
.ant-form-item { | ||
margin-bottom: 10px; | ||
} | ||
} | ||
|
||
.dynamic-delete-button { | ||
|
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,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); |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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)); |
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