2018-05-04 04:16:17 +00:00
|
|
|
/* eslint react/no-multi-comp:0, no-console:0 */
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
import { createForm } from '../index';
|
|
|
|
import { regionStyle, errorStyle } from './styles';
|
|
|
|
import BaseMixin from '../../_util/BaseMixin';
|
2018-05-04 04:16:17 +00:00
|
|
|
|
|
|
|
const Email = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
checkSpecial(rule, value, callback) {
|
2018-05-04 04:16:17 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (value === 'yiminghe@gmail.com') {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback('can not be!');
|
2018-05-04 04:16:17 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback();
|
2018-05-04 04:16:17 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
}, 1000);
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
|
|
|
|
const errors = getFieldError('email');
|
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>email validate onBlur</div>
|
|
|
|
<div>
|
|
|
|
<input
|
|
|
|
{...getFieldProps('email', {
|
|
|
|
initialValue: '',
|
|
|
|
validateFirst: true,
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'email',
|
|
|
|
message: '错误的 email 格式',
|
|
|
|
},
|
|
|
|
this.checkSpecial,
|
|
|
|
],
|
|
|
|
validateTrigger: 'blur',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
|
|
|
|
<div style={errorStyle}>{isFieldValidating('email') ? 'validating' : null}</div>
|
2018-05-04 04:16:17 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 04:16:17 +00:00
|
|
|
|
|
|
|
const Form = {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-05-04 04:16:17 +00:00
|
|
|
return {
|
|
|
|
loading: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-05-04 04:16:17 +00:00
|
|
|
setTimeout(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
loading: false,
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.form.setFieldsInitialValue({
|
|
|
|
email: 'xx@gmail.com',
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}, 1000);
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.form.submit(callback => {
|
2018-05-04 04:16:17 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
this.form.validateFields((error, values) => {
|
|
|
|
if (!error) {
|
2019-01-12 03:33:27 +00:00
|
|
|
console.log('ok', values);
|
2018-05-04 04:16:17 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
console.log('error', error, values);
|
2018-05-04 04:16:17 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
});
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
reset(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.form.resetFields();
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2018-05-04 04:16:17 +00:00
|
|
|
if (this.loading) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <b>loading</b>;
|
2018-05-04 04:16:17 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const { form } = this;
|
|
|
|
const disabled = form.isFieldsValidating() || form.isSubmitting();
|
|
|
|
return (
|
|
|
|
<div style={{ margin: 20 }}>
|
|
|
|
<h2>async init field</h2>
|
|
|
|
<form onSubmit={this.onSubmit}>
|
|
|
|
<Email form={form} />
|
2018-05-04 04:16:17 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={regionStyle}>
|
|
|
|
<button disabled={disabled} type="submit">
|
|
|
|
submit
|
|
|
|
</button>
|
|
|
|
{disabled ? <span style={{ color: 'red' }}>disabled</span> : null}
|
|
|
|
<button disabled={disabled} onClick={this.reset}>
|
|
|
|
reset
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
2018-05-04 04:16:17 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 04:16:17 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default createForm()(Form);
|