You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/form/demo/customized-form-controls.vue

140 lines
3.5 KiB

7 years ago
<cn>
#### 自定义表单控件
自定义或第三方的表单控件也可以与 Form 组件一起使用只要该组件遵循以下的约定
> * 提供受控属性 `value` 或其它与 [`valuePropName`](/components/form-cn/#getFieldDecorator(id,-options)-)
> * 提供 `onChange` 事件或 [`trigger`](/components/form-cn/#getFieldDecorator(id,-options)-)
7 years ago
> * 不能是函数式组件
</cn>
<us>
#### Customized Form Controls
Customized or third-party form controls can be used in Form, too. Controls must follow these conventions:
> * It has a controlled property `value` or other name which is equal to the value of [`valuePropName`](/components/form/#getFieldDecorator(id,-options)-parameters).
> * It has event `onChange` or an event which name is equal to the value of [`trigger`](/components/form/#getFieldDecorator(id,-options)-parameters).
7 years ago
> * It must be a class component.
</us>
<template>
<a-form
layout="inline"
:form="form"
@submit="handleSubmit"
>
<a-form-item label="Price">
<price-input
v-decorator="[
'price',
{
initialValue: { number: 0, currency: 'rmb' },
rules: [{ validator: checkPrice }],
}
]"
/>
</a-form-item>
<a-form-item>
<a-button
type="primary"
html-type="submit"
>
Submit
</a-button>
</a-form-item>
</a-form>
</template>
7 years ago
<script>
7 years ago
const hasProp = (instance, prop) => {
const $options = instance.$options || {};
const propsData = $options.propsData || {};
return prop in propsData;
};
7 years ago
const PriceInput = {
props: ['value'],
template: `
<span>
<a-input
type='text'
:value="number"
@change="handleNumberChange"
style="width: 63%; margin-right: 2%;"
/>
<a-select
:value="currency"
style="width: 32%"
@change="handleCurrencyChange"
>
<a-select-option value='rmb'>RMB</a-select-option>
<a-select-option value='dollar'>Dollar</a-select-option>
</a-select>
</span>
`,
7 years ago
data () {
const value = this.value || {};
7 years ago
return {
number: value.number || 0,
currency: value.currency || 'rmb',
};
7 years ago
},
watch: {
value (val = {}) {
this.number = val.number || 0;
this.currency = val.currency || 'rmb';
7 years ago
},
},
methods: {
handleNumberChange (e) {
const number = parseInt(e.target.value || 0, 10);
7 years ago
if (isNaN(number)) {
return;
7 years ago
}
if (!hasProp(this, 'value')) {
this.number = number;
7 years ago
}
this.triggerChange({ number });
7 years ago
},
handleCurrencyChange (currency) {
if (!hasProp(this, 'value')) {
this.currency = currency;
7 years ago
}
this.triggerChange({ currency });
7 years ago
},
triggerChange (changedValue) {
// Should provide an event to pass value to Form.
this.$emit('change', Object.assign({}, this.$data, changedValue));
7 years ago
},
},
};
7 years ago
export default {
components: {
PriceInput,
},
beforeCreate () {
this.form = this.$form.createForm(this);
},
7 years ago
methods: {
handleSubmit (e) {
e.preventDefault();
7 years ago
this.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
7 years ago
}
});
7 years ago
},
checkPrice (rule, value, callback) {
if (value.number > 0) {
callback();
return;
7 years ago
}
callback('Price must greater than zero!');
7 years ago
},
},
};
7 years ago
</script>
7 years ago