ant-design-vue/components/vc-form/demo/overview.js

270 lines
6.1 KiB
JavaScript
Raw Normal View History

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');
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> }],
})}
/>
</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');
return (
<div style={regionStyle}>
<div>
2019-01-12 03:33:27 +00:00
<span
style={{
color: 'red',
}}
>
*
</span>
user async validate
</div>
<div>
<input
2019-01-12 03:33:27 +00:00
{...getFieldProps('user', {
initialValue: '',
validateFirst: true,
rules: [{ required: true }, { validator: this.userExists }],
})}
/>
</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');
return (
<div style={regionStyle}>
<div>
2019-01-12 03:33:27 +00:00
<span
style={{
color: 'red',
}}
>
*
</span>
custom select sync validate
</div>
<div>
<Select
2019-01-12 03:33:27 +00:00
placeholder="please select"
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>
</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');
return (
<div style={regionStyle}>
<div>
2019-01-12 03:33:27 +00:00
<span
style={{
color: 'red',
}}
>
*
</span>
DateInput sync validate
</div>
2019-01-12 03:33:27 +00:00
<div
style={{
width: '200px',
}}
>
<DatePicker
placeholder="please select"
{...getFieldProps('date', { rules: [{ required: true, type: 'object' }] })}
/>
</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');
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' }],
})}
/>
</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(
{
scroll: {
offsetTop: 20,
},
2019-01-12 03:33:27 +00:00
},
(error, values) => {
if (!error) {
2019-01-12 03:33:27 +00:00
console.log('ok', values);
} else {
2019-01-12 03:33:27 +00:00
console.log('error', error, values);
}
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;
return (
2019-01-12 03:33:27 +00:00
<div
style={{
margin: '20px',
}}
>
<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
<div style={regionStyle}>
<div>normal required input</div>
<div>
<input
2019-01-12 03:33:27 +00:00
{...getFieldProps('normal', { initialValue: '', rules: [{ required: true }] })}
/>
</div>
<div style={errorStyle}>
2019-01-12 03:33:27 +00:00
{getFieldError('normal') ? getFieldError('normal').join(',') : null}
</div>
2018-05-04 08:02:31 +00:00
</div>
<div style={regionStyle}>
<button onClick={this.reset}>reset</button>
&nbsp;
2019-01-12 03:33:27 +00:00
<input type="submit" value="submit" />
</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);