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

124 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-04 11:11:42 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
2019-01-12 03:33:27 +00:00
import { createForm } from '../index';
import { Select } from 'ant-design-vue';
import { regionStyle, errorStyle } from './styles';
import { mergeProps } from '../../_util/props-util';
const emailTpl = ['@gmail.com', '@outlook.com', '@qq.com'];
const { Option } = Select;
2018-05-04 11:11:42 +00:00
const CustomInput = {
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
data() {
return { data: [] };
2018-05-04 11:11:42 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
onChange(v) {
2018-05-04 11:11:42 +00:00
if (v.indexOf('@') === -1) {
2019-01-12 03:33:27 +00:00
this.data = emailTpl.map(m => v + m);
2018-05-04 11:11:42 +00:00
} else if (this.data.length) {
2019-01-12 03:33:27 +00:00
this.data = [];
2018-05-04 11:11:42 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps, getFieldError, isFieldValidating } = this.form;
const errors = getFieldError('select');
return (
<div style={regionStyle}>
<div>custom select sync validate</div>
<div>
<Select
2019-01-12 03:33:27 +00:00
{...mergeProps(
{
props: { placeholder: 'please select', mode: 'combobox', filterOption: false },
style: { width: '200px' },
},
getFieldProps('select', {
change: this.onChange,
rules: [{ type: 'email' }, { required: true }],
}),
)}
>
{this.data.map(d => {
return (
<Option key={d} value={d}>
{d}
</Option>
);
})}
</Select>
</div>
<div style={errorStyle}>
2019-01-12 03:33:27 +00:00
{errors ? (
errors.join(',')
) : (
<b
style={{
visibility: 'hidden',
}}
>
1
2019-01-12 03:33:27 +00:00
</b>
)}
</div>
<div style={errorStyle}>
2019-01-12 03:33:27 +00:00
{isFieldValidating('select') ? (
'validating'
) : (
<b
style={{
visibility: 'hidden',
}}
>
1
2019-01-12 03:33:27 +00:00
</b>
)}
</div>
2018-05-04 11:11:42 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
const Form = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
e.preventDefault();
this.form.validateFields((error, values) => {
if (!error) {
console.log('ok', values);
} else {
console.log('error', error, values);
}
});
2018-05-04 11:11:42 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { form } = this;
return (
2019-01-12 03:33:27 +00:00
<div
style={{
margin: '20px',
}}
>
<h2>suggest</h2>
<form onSubmit={this.onSubmit}>
2019-01-12 03:33:27 +00:00
<CustomInput form={form} />
2018-05-04 11:11:42 +00:00
<div style={regionStyle}>
<button>submit</button>
</div>
</form>
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);