2018-05-04 08:02:31 +00:00
|
|
|
/* eslint react/no-multi-comp:0, no-console:0 */
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
import createDOMForm from '../src/createDOMForm';
|
|
|
|
import { DatePicker, Select } from 'ant-design-vue';
|
|
|
|
import { regionStyle, errorStyle } from './styles';
|
|
|
|
const { Option } = Select;
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
const Email = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
|
|
|
|
const errors = getFieldError('email');
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>email sync validate</div>
|
|
|
|
<div>
|
|
|
|
<input
|
2019-01-12 03:33:27 +00:00
|
|
|
{...getFieldProps('email', {
|
|
|
|
initialValue: '',
|
|
|
|
rules: [{ type: 'email', message: <b key="err">错误的 email 格式</b> }],
|
|
|
|
})}
|
|
|
|
/>
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={errorStyle}>{errors}</div>
|
|
|
|
<div style={errorStyle}>{isFieldValidating('email') ? 'validating' : null}</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
const User = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
userExists(rule, value, callback) {
|
2018-05-04 08:02:31 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (value === '1') {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback([new Error('are you kidding?')]);
|
2018-05-04 08:02:31 +00:00
|
|
|
} else if (value === 'yiminghe') {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback([new Error('forbid yiminghe')]);
|
2018-05-04 08:02:31 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback();
|
2018-05-04 08:02:31 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
}, 300);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
|
|
|
|
const errors = getFieldError('user');
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
color: 'red',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
*
|
|
|
|
</span>
|
|
|
|
user async validate
|
|
|
|
</div>
|
2018-08-04 09:52:11 +00:00
|
|
|
<div>
|
|
|
|
<input
|
2019-01-12 03:33:27 +00:00
|
|
|
{...getFieldProps('user', {
|
|
|
|
initialValue: '',
|
|
|
|
validateFirst: true,
|
|
|
|
rules: [{ required: true }, { validator: this.userExists }],
|
|
|
|
})}
|
|
|
|
/>
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
|
|
|
|
<div style={errorStyle}>{isFieldValidating('user') ? 'validating' : null}</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
const CustomInput = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
|
|
|
|
const errors = getFieldError('select');
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
color: 'red',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
*
|
|
|
|
</span>
|
|
|
|
custom select sync validate
|
|
|
|
</div>
|
2018-08-04 09:52:11 +00:00
|
|
|
<div>
|
|
|
|
<Select
|
2019-01-12 03:33:27 +00:00
|
|
|
placeholder="please select"
|
2018-08-04 09:52:11 +00:00
|
|
|
style={{
|
|
|
|
width: '200px',
|
|
|
|
}}
|
2019-01-12 03:33:27 +00:00
|
|
|
{...getFieldProps('select', { rules: [{ required: true }] })}
|
|
|
|
>
|
|
|
|
<Option value="1">1</Option>
|
|
|
|
<Option value="2">2</Option>
|
2018-08-04 09:52:11 +00:00
|
|
|
</Select>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
|
|
|
|
<div style={errorStyle}>{isFieldValidating('select') ? 'validating' : null}</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
const DateInput = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError } = this.form;
|
|
|
|
const errors = getFieldError('date');
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
color: 'red',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
*
|
|
|
|
</span>
|
|
|
|
DateInput sync validate
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
width: '200px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<DatePicker
|
|
|
|
placeholder="please select"
|
|
|
|
{...getFieldProps('date', { rules: [{ required: true, type: 'object' }] })}
|
|
|
|
/>
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function toNumber(v) {
|
2018-05-04 08:02:31 +00:00
|
|
|
if (v === undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return v;
|
2018-05-04 08:02:31 +00:00
|
|
|
}
|
|
|
|
if (v === '') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return undefined;
|
2018-05-04 08:02:31 +00:00
|
|
|
}
|
|
|
|
if (v && v.trim() === '') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return NaN;
|
2018-05-04 08:02:31 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return Number(v);
|
2018-05-04 08:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const NumberInput = {
|
|
|
|
props: {
|
|
|
|
form: Object,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { getFieldProps, getFieldError } = this.form;
|
|
|
|
const errors = getFieldError('number');
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>number input</div>
|
|
|
|
<div>
|
|
|
|
<input
|
2019-01-12 03:33:27 +00:00
|
|
|
{...getFieldProps('number', {
|
|
|
|
initialValue: '1',
|
|
|
|
rules: [{ transform: toNumber, type: 'number' }],
|
|
|
|
})}
|
|
|
|
/>
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
const Form = {
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onSubmit(e) {
|
|
|
|
console.log('submit');
|
|
|
|
e.preventDefault();
|
|
|
|
this.form.validateFieldsAndScroll(
|
|
|
|
{
|
2018-08-04 09:52:11 +00:00
|
|
|
scroll: {
|
|
|
|
offsetTop: 20,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
(error, values) => {
|
2018-08-04 09:52:11 +00:00
|
|
|
if (!error) {
|
2019-01-12 03:33:27 +00:00
|
|
|
console.log('ok', values);
|
2018-08-04 09:52:11 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
console.log('error', error, values);
|
2018-08-04 09:52:11 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
reset(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.form.resetFields();
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { form } = this;
|
|
|
|
const { getFieldProps, getFieldError } = form;
|
2018-08-04 09:52:11 +00:00
|
|
|
return (
|
2019-01-12 03:33:27 +00:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
margin: '20px',
|
|
|
|
}}
|
|
|
|
>
|
2018-08-04 09:52:11 +00:00
|
|
|
<h2>overview</h2>
|
|
|
|
<form onSubmit={this.onSubmit}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<User form={form} saveRef={this.saveRef} />
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<NumberInput form={form} />
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<Email form={form} />
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<CustomInput form={form} />
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<DateInput form={form} />
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2018-08-04 09:52:11 +00:00
|
|
|
<div style={regionStyle}>
|
|
|
|
<div>normal required input</div>
|
|
|
|
<div>
|
|
|
|
<input
|
2019-01-12 03:33:27 +00:00
|
|
|
{...getFieldProps('normal', { initialValue: '', rules: [{ required: true }] })}
|
|
|
|
/>
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
|
|
|
<div style={errorStyle}>
|
2019-01-12 03:33:27 +00:00
|
|
|
{getFieldError('normal') ? getFieldError('normal').join(',') : null}
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
2018-05-04 08:02:31 +00:00
|
|
|
</div>
|
|
|
|
|
2018-08-04 09:52:11 +00:00
|
|
|
<div style={regionStyle}>
|
|
|
|
<button onClick={this.reset}>reset</button>
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
<input type="submit" value="submit" />
|
2018-08-04 09:52:11 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-04 08:02:31 +00:00
|
|
|
|
|
|
|
export default createDOMForm({
|
|
|
|
validateMessages: {
|
2019-01-12 03:33:27 +00:00
|
|
|
required(field) {
|
|
|
|
return `${field} 必填`;
|
2018-05-04 08:02:31 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
})(Form);
|