vant/packages/search/index.vue
neverland 0da517e004 [Doc] add demo pages (#237)
* [bugfix] Checkbox border render error in weixin browser

* [bugfix] TreeSelect dependency path error

* [bugfix] Swipe should clear autoplay timer when destroyed

* [bugfix] Optimize component dependency analyze when build style entry

* merge

* update yarn.lock

* update README.md

* update README.md

* update README.md

* update README.md

* update README.md

* [Doc] add more badges in README.md

* update README.md

* [bugfix] Address & Contact list style

* fix: contact test cases

* [bugfix] popup style missing when build style entry

* [bugfix] Search: onSearch event arguments missing

* [Doc] add demo pages

* update zan-doc@0.3.7

* fix: build entry error
2017-10-24 02:27:04 -05: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">
<div class="van-search__action-text" @click="handleBack">取消</div>
</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', this.value);
return false;
},
handleClickoutside() {
this.isFocus = false;
this.focusStatus = false;
}
}
};
</script>