vant/packages/search/index.vue
Yao f02048a3dc [breaking change] Search: 修改原有结构 (#198)
* 修改文档 注释

* [breaking change] change type showcase to simple

* [breaking change] search 组件传入参数更改

* search 文档更改

* [breaking change] delete type && add background && showSearch

* showCancel 更名为 showAction && 增加 slot action

* add search test code
2017-10-12 20:56:03 +08:00

118 lines
2.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div
class="van-search"
:class="{ 'van-search--show-action': showAction }"
:style="{ 'background-color': background }">
<div class="van-search__input-wrap" v-clickoutside="handleClickoutside">
<van-icon name="search"></van-icon>
<input
type="search"
class="van-search__input"
v-refocus="focusStatus"
:value="value"
:placeholder="placeholder"
@input="handleInput"
@focus="handleFocus"
@keypress.enter.prevent="handleSearch">
<van-icon name="clear" @click="handleClean" v-show="isFocus"></van-icon>
</div>
<div class="van-search__action" v-if="showAction">
<slot name="action">
<p class="van-search__action-text" @click="handleBack">取消</p>
</slot>
</div>
</div>
</template>
<script>
import Icon from '../icon';
import Clickoutside from '../utils/clickoutside';
export default {
name: 'van-search',
components: {
[Icon.name]: Icon
},
props: {
placeholder: String,
value: String,
showAction: {
type: Boolean,
default: false
},
background: {
type: String,
default: '#f2f2f2'
}
},
data() {
return {
focusStatus: false,
isFocus: false
};
},
directives: {
Clickoutside,
refocus: {
update: function(el, state) {
if (state.value) {
el.focus();
}
}
}
},
methods: {
/**
* 进入input焦点出现close和取消
*/
handleFocus() {
this.isFocus = true;
},
handleInput(event) {
this.$emit('input', event.target.value);
},
/**
* 点击close后清空value后再聚焦input框
*/
handleClean() {
this.$emit('input', '');
this.focusStatus = true;
// 保证多次点击 clean 后,仍能触发 refocus
this.$nextTick(() => {
this.focusStatus = false;
});
},
/**
* 点击取消后,清空所有回复最初状态
*/
handleBack() {
this.$emit('input', '');
this.$emit('cancel');
},
/**
* input输入回车后发送回调
*/
handleSearch(e) {
e.preventDefault();
this.$emit('search');
return false;
},
handleClickoutside() {
this.isFocus = false;
this.focusStatus = false;
}
}
};
</script>