support Form

support Form
This commit is contained in:
梁灏 2017-03-08 15:31:59 +08:00
parent 531cd1654b
commit 257f80f1c2
7 changed files with 52 additions and 43 deletions

View file

@ -34,7 +34,7 @@
- [x] InputNumber
- [x] Rate
- [x] Upload
- [ ] Form
- [x] Form
- [x] Alert
- [x] Card
- [ ] Message

View file

@ -48,6 +48,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; }
<li><router-link to="/page">Page</router-link></li>
<li><router-link to="/transfer">Transfer</router-link></li>
<li><router-link to="/date">Date</router-link></li>
<li><router-link to="/form">Form</router-link></li>
</ul>
</nav>
<router-view></router-view>

View file

@ -42,12 +42,12 @@ const router = new VueRouter({
component: require('./routers/checkbox.vue')
},
{
path: '/steps',
component: require('./routers/steps.vue')
path: '/steps',
component: require('./routers/steps.vue')
},
{
path: '/timeline',
component: require('./routers/timeline.vue')
path: '/timeline',
component: require('./routers/timeline.vue')
},
{
path: '/switch',
@ -156,7 +156,11 @@ const router = new VueRouter({
{
path: '/date',
component: require('./routers/date.vue')
}
},
{
path: '/form',
component: require('./routers/form.vue')
},
]
});

View file

@ -1,21 +1,17 @@
<template>
<i-form v-ref:form-validate :model="formValidate" :rules="ruleValidate" :label-width="100">
<Form-item label="输入框" prop="input">
<i-input :value.sync="formValidate.input" placeholder="请输入"></i-input>
<i-form ref="formInline" :model="formInline" :rules="ruleInline" inline>
<Form-item prop="user">
<Input type="text" v-model="formInline.user" placeholder="Username">
<Icon type="ios-person-outline" slot="prepend"></Icon>
</Input>
</Form-item>
<Form-item label="Ajax" prop="ajax">
<div slot="label">
<span>Ajax</span>
<Tooltip content="基于 axios">
<Icon type="ios-help" size="14" color="#3399ff"></Icon>
</Tooltip>
<span></span>
</div>
<Switch :checked.sync="formValidate.ajax"></Switch>
<Form-item prop="password">
<Input type="password" v-model="formInline.password" placeholder="Password">
<Icon type="ios-locked-outline" slot="prepend"></Icon>
</Input>
</Form-item>
<Form-item>
<i-button type="primary" @click="handleSubmit('formValidate')">提交</i-button>
<i-button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</i-button>
<Button type="primary" @click.native="handleSubmit('formInline')">登录</Button>
</Form-item>
</i-form>
</template>
@ -23,27 +19,30 @@
export default {
data () {
return {
formValidate: {
input: '123',
ajax: true
formInline: {
user: '',
password: ''
},
ruleValidate: {
ruleInline: {
user: [
{ required: true, message: '请填写用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请填写密码', trigger: 'blur' },
{ type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
]
}
}
},
methods: {
handleSubmit (name) {
handleSubmit(name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.$Message.success('提交成功!');
console.log('success');
} else {
this.$Message.error('表单验证失败!');
console.log('fail')
}
})
},
handleReset (name) {
this.$refs[name].resetFields();
}
}
}

View file

@ -3,7 +3,9 @@
<label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label"><slot name="label">{{ label }}</slot></label>
<div :class="[prefixCls + '-content']" :style="contentStyles">
<slot></slot>
<div transition="fade" :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div>
<transition name="fade">
<div :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div>
</transition>
</div>
</div>
</template>
@ -11,6 +13,7 @@
// https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue
import AsyncValidator from 'async-validator';
import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-form-item';
@ -38,6 +41,8 @@
}
export default {
name: 'FormItem',
mixins: [ Emitter ],
props: {
label: {
type: String,
@ -206,9 +211,9 @@
this.validate('change');
}
},
ready () {
mounted () {
if (this.prop) {
this.$dispatch('on-form-item-add', this);
this.dispatch('iForm', 'on-form-item-add', this);
Object.defineProperty(this, 'initialValue', {
value: this.fieldValue
@ -229,7 +234,7 @@
}
},
beforeDestroy () {
this.$dispatch('on-form-item-remove', this);
this.dispatch('iForm', 'on-form-item-remove', this);
}
};
</script>

View file

@ -82,15 +82,15 @@
this.validate();
}
},
events: {
'on-form-item-add' (field) {
created () {
this.$on('on-form-item-add', (field) => {
if (field) this.fields.push(field);
return false;
},
'on-form-item-remove' (field) {
});
this.$on('on-form-item-remove', (field) => {
if (field.prop) this.fields.splice(this.fields.indexOf(field), 1);
return false;
}
});
}
};
</script>

View file

@ -15,7 +15,7 @@ import Circle from './components/circle';
import Collapse from './components/collapse';
import DatePicker from './components/date-picker';
import Dropdown from './components/dropdown';
// import Form from './components/form';
import Form from './components/form';
import Icon from './components/icon';
import Input from './components/input';
import InputNumber from './components/input-number';
@ -67,8 +67,8 @@ const iview = {
Dropdown,
DropdownItem: Dropdown.Item,
DropdownMenu: Dropdown.Menu,
// iForm: Form,
// FormItem: Form.Item,
iForm: Form,
FormItem: Form.Item,
iCol: Col,
Collapse,
Icon,