Skip to content

Commit

Permalink
support Form
Browse files Browse the repository at this point in the history
support Form
  • Loading branch information
icarusion committed Mar 8, 2017
1 parent 531cd16 commit 257f80f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- [x] InputNumber
- [x] Rate
- [x] Upload
- [ ] Form
- [x] Form
- [x] Alert
- [x] Card
- [ ] Message
Expand Down
1 change: 1 addition & 0 deletions examples/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
14 changes: 9 additions & 5 deletions examples/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -156,7 +156,11 @@ const router = new VueRouter({
{
path: '/date',
component: require('./routers/date.vue')
}
},
{
path: '/form',
component: require('./routers/form.vue')
},
]
});

Expand Down
49 changes: 24 additions & 25 deletions examples/routers/form.vue
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
<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>
<script>
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();
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/components/form/form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
<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>
<script>
// 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';
Expand Down Expand Up @@ -38,6 +41,8 @@
}
export default {
name: 'FormItem',
mixins: [ Emitter ],
props: {
label: {
type: String,
Expand Down Expand Up @@ -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
Expand All @@ -229,7 +234,7 @@
}
},
beforeDestroy () {
this.$dispatch('on-form-item-remove', this);
this.dispatch('iForm', 'on-form-item-remove', this);
}
};
</script>
10 changes: 5 additions & 5 deletions src/components/form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 257f80f

Please sign in to comment.