field 增加icon slot支持 (#76)

* field 增加icon slot支持
This commit is contained in:
Yao 2017-08-18 11:15:48 +08:00 committed by GitHub
parent 14b5c0a9e4
commit 686b497c58
3 changed files with 39 additions and 3 deletions

View File

@ -47,7 +47,15 @@ export default {
:on-icon-click="onIconClick" :on-icon-click="onIconClick"
@blur="onFieldBlur" @blur="onFieldBlur"
required></van-field> required></van-field>
<van-field type="password" label="密码:" placeholder="请输入密码" required></van-field> <van-field
type="password"
label="密码:"
placeholder="请输入密码"
required>
<template slot="icon">
<van-icon name="search"></van-icon>
</template>
</van-field>
<van-field type="textarea" label="个人介绍:" placeholder="请输入个人介绍" required></van-field> <van-field type="textarea" label="个人介绍:" placeholder="请输入个人介绍" required></van-field>
</van-cell-group> </van-cell-group>
``` ```
@ -132,3 +140,9 @@ export default {
| icon | 输入框尾部图标 | `string` | | icon中支持的类型 | | icon | 输入框尾部图标 | `string` | | icon中支持的类型 |
| onIconClick | 点击图标的回调函数 | `function` | | | | onIconClick | 点击图标的回调函数 | `function` | | |
### Slot
| name | 描述 |
|-----------|-----------|
| icon | 自定义icon |

View File

@ -9,7 +9,8 @@
'van-field--disabled': disabled, 'van-field--disabled': disabled,
'van-field--error': error, 'van-field--error': error,
'van-field--border': border, 'van-field--border': border,
'van-field--autosize': autosize 'van-field--autosize': autosize,
'van-field--has-icon': showIcon
}"> }">
<textarea <textarea
v-if="type === 'textarea'" v-if="type === 'textarea'"
@ -37,7 +38,11 @@
:maxlength="maxlength" :maxlength="maxlength"
:disabled="disabled" :disabled="disabled"
:readonly="readonly"> :readonly="readonly">
<van-icon v-if="icon && currentValue" :name="icon" class="van-field__icon" @click="onIconClick"></van-icon> <div v-if="showIcon" class="van-field__icon" @click="onIconClick">
<slot name="icon">
<van-icon :name="icon"></van-icon>
</slot>
</div>
</van-cell> </van-cell>
</template> </template>
@ -101,6 +106,17 @@ export default {
} }
}, },
computed: {
showIcon() {
// iconslot
if (this.$slots.icon) {
return true;
}
return this.icon && this.currentValue;
}
},
methods: { methods: {
handleInput(event) { handleInput(event) {
this.currentValue = event.target.value; this.currentValue = event.target.value;

View File

@ -78,6 +78,12 @@
} }
} }
&--has-icon {
.van-field__control {
width: 90%;
}
}
&__control { &__control {
border: 0; border: 0;
font-size: 14px; font-size: 14px;