perf: form
parent
f686674dfc
commit
c924f4ca82
|
@ -149,6 +149,11 @@ const Form = {
|
||||||
this.$forceUpdate();
|
this.$forceUpdate();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
updated(){
|
||||||
|
if(this.autoFormCreate || this.form) {
|
||||||
|
this.form.cleanUpUselessFields();
|
||||||
|
}
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
onSubmit(e) {
|
onSubmit(e) {
|
||||||
const { $listeners } = this;
|
const { $listeners } = this;
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { mount } from '@vue/test-utils';
|
import { mount } from '@vue/test-utils';
|
||||||
|
import { asyncExpect } from '@/tests/utils';
|
||||||
import Form from '..';
|
import Form from '..';
|
||||||
|
|
||||||
describe('Form', () => {
|
describe('Form', () => {
|
||||||
it('should display two message', () => {
|
it('should display two message', async () => {
|
||||||
const rules = [
|
const rules = [
|
||||||
{
|
{
|
||||||
pattern: /^\w+$/,
|
pattern: /^\w+$/,
|
||||||
|
@ -16,6 +17,7 @@ describe('Form', () => {
|
||||||
let myForm;
|
let myForm;
|
||||||
const Form1 = Form.create()({
|
const Form1 = Form.create()({
|
||||||
render() {
|
render() {
|
||||||
|
myForm = this.form;
|
||||||
return (
|
return (
|
||||||
<Form>
|
<Form>
|
||||||
<Form.Item label="Account">
|
<Form.Item label="Account">
|
||||||
|
@ -27,13 +29,11 @@ describe('Form', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const wrapper = mount(Form1, {
|
const wrapper = mount(Form1, {
|
||||||
propsData: {
|
sync: false,
|
||||||
wrappedComponentRef: inst => {
|
|
||||||
myForm = inst.form;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
await asyncExpect(()=>{
|
||||||
myForm.validateFields();
|
myForm.validateFields();
|
||||||
|
});
|
||||||
|
|
||||||
wrapper.vm.$forceUpdate();
|
wrapper.vm.$forceUpdate();
|
||||||
expect(wrapper.html()).toMatchSnapshot();
|
expect(wrapper.html()).toMatchSnapshot();
|
||||||
|
|
|
@ -200,9 +200,78 @@ const Form3 = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Form4 = {
|
||||||
|
mixins: [BaseMixin],
|
||||||
|
props: {
|
||||||
|
form: Object,
|
||||||
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
useInput: true,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
onSubmit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
this.form.validateFields((error, values) => {
|
||||||
|
if (!error) {
|
||||||
|
console.log('ok', values);
|
||||||
|
} else {
|
||||||
|
console.log('error', error, values);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
changeUseInput(e) {
|
||||||
|
this.setState({
|
||||||
|
useInput: e.target.checked,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { getFieldError, getFieldDecorator } = this.form;
|
||||||
|
return (
|
||||||
|
<form onSubmit={this.onSubmit}>
|
||||||
|
<h2>situation 4</h2>
|
||||||
|
{this.useInput
|
||||||
|
? getFieldDecorator('name', {
|
||||||
|
initialValue: '',
|
||||||
|
trigger: 'input',
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
message: "What's your name 1?",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})(<input />)
|
||||||
|
: getFieldDecorator('name2', {
|
||||||
|
initialValue: '',
|
||||||
|
trigger: 'input',
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
message: "What's your name 2?",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})(<input />)}
|
||||||
|
<div>
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
|
||||||
|
toggle decorator name
|
||||||
|
</label>
|
||||||
|
{(getFieldError('name') || []).join(', ')}
|
||||||
|
{(getFieldError('name2') || []).join(', ')}
|
||||||
|
</div>
|
||||||
|
<button>Submit</button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const WrappedForm1 = createForm()(Form1);
|
const WrappedForm1 = createForm()(Form1);
|
||||||
const WrappedForm2 = createForm()(Form2);
|
const WrappedForm2 = createForm()(Form2);
|
||||||
const WrappedForm3 = createForm()(Form3);
|
const WrappedForm3 = createForm()(Form3);
|
||||||
|
const WrappedForm4 = createForm()(Form4);
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
render() {
|
render() {
|
||||||
|
@ -211,6 +280,7 @@ export default {
|
||||||
<WrappedForm1 />
|
<WrappedForm1 />
|
||||||
<WrappedForm2 />
|
<WrappedForm2 />
|
||||||
<WrappedForm3 />
|
<WrappedForm3 />
|
||||||
|
<WrappedForm4 />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
@ -98,16 +98,12 @@ function createBaseForm(option = {}, mixins = []) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.wrappedComponentRef(this.$refs.WrappedComponent);
|
|
||||||
this.cleanUpUselessFields();
|
this.cleanUpUselessFields();
|
||||||
},
|
},
|
||||||
updated() {
|
updated() {
|
||||||
this.wrappedComponentRef(this.$refs.WrappedComponent);
|
// form updated add for template v-decorator
|
||||||
this.cleanUpUselessFields();
|
this.cleanUpUselessFields();
|
||||||
},
|
},
|
||||||
destroyed() {
|
|
||||||
this.wrappedComponentRef(null);
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
updateFields(fields = {}) {
|
updateFields(fields = {}) {
|
||||||
this.fieldsStore.updateFields(mapPropsToFields(fields));
|
this.fieldsStore.updateFields(mapPropsToFields(fields));
|
||||||
|
@ -636,14 +632,20 @@ function createBaseForm(option = {}, mixins = []) {
|
||||||
const formProps = {
|
const formProps = {
|
||||||
[formPropName]: this.getForm(),
|
[formPropName]: this.getForm(),
|
||||||
};
|
};
|
||||||
const props = getOptionProps(this);
|
const {wrappedComponentRef, ...restProps} = getOptionProps(this);
|
||||||
const wrappedComponentProps = {
|
const wrappedComponentProps = {
|
||||||
props: mapProps.call(this, {
|
props: mapProps.call(this, {
|
||||||
...formProps,
|
...formProps,
|
||||||
...props,
|
...restProps,
|
||||||
}),
|
}),
|
||||||
on: $listeners,
|
on: $listeners,
|
||||||
ref: 'WrappedComponent',
|
ref: 'WrappedComponent',
|
||||||
|
directives: [
|
||||||
|
{
|
||||||
|
name: 'ant-ref',
|
||||||
|
value: wrappedComponentRef,
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
return WrappedComponent ? (
|
return WrappedComponent ? (
|
||||||
|
|
|
@ -188,7 +188,7 @@
|
||||||
"resize-observer-polyfill": "^1.5.1",
|
"resize-observer-polyfill": "^1.5.1",
|
||||||
"shallow-equal": "^1.0.0",
|
"shallow-equal": "^1.0.0",
|
||||||
"shallowequal": "^1.0.2",
|
"shallowequal": "^1.0.2",
|
||||||
"vue-ref": "^1.0.3",
|
"vue-ref": "^1.0.4",
|
||||||
"warning": "^3.0.0"
|
"warning": "^3.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue