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

104 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-05-04 11:11:42 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
import { createForm } from '../index'
import { Select } from 'ant-design-vue'
2018-05-04 11:11:42 +00:00
import { regionStyle, errorStyle } from './styles'
import { mergeProps } from '../../_util/props-util'
const emailTpl = ['@gmail.com', '@outlook.com', '@qq.com']
const { Option } = Select
const CustomInput = {
props: {
form: Object,
},
data () {
return { data: [] }
2018-05-04 11:11:42 +00:00
},
methods: {
onChange (v) {
if (v.indexOf('@') === -1) {
this.data = emailTpl.map(m => v + m)
} else if (this.data.length) {
this.data = []
}
},
},
render () {
const { getFieldProps, getFieldError, isFieldValidating } = this.form
const errors = getFieldError('select')
return (
<div style={regionStyle}>
<div>custom select sync validate</div>
<div>
<Select
{ ...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}>
{(errors)
? errors.join(',')
: <b style={{
2018-05-04 11:11:42 +00:00
visibility: 'hidden',
}}>
1
</b>}
</div>
<div style={errorStyle}>
{isFieldValidating('select')
? 'validating'
: <b style={{
visibility: 'hidden',
}}>
1
</b>}
</div>
2018-05-04 11:11:42 +00:00
</div>
)
2018-05-04 11:11:42 +00:00
},
}
const Form = {
props: {
form: Object,
},
methods: {
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
},
},
render () {
const { form } = this
return (
<div style={{
margin: '20px',
}}>
<h2>suggest</h2>
<form onSubmit={this.onSubmit}>
<CustomInput form={form}/>
2018-05-04 11:11:42 +00:00
<div style={regionStyle}>
<button>submit</button>
</div>
</form>
</div>
)
2018-05-04 11:11:42 +00:00
},
}
export default createForm()(Form)