[new feature] Search: support input native events (#451)

This commit is contained in:
neverland
2017-12-19 10:24:56 +08:00
committed by GitHub
parent 48b2026e33
commit 79a2b13b63
7 changed files with 53 additions and 30 deletions

View File

@@ -6,14 +6,12 @@
<div class="van-search__input-wrap" v-clickoutside="onClickoutside">
<icon name="search" />
<input
v-bind="$attrs"
v-on="listeners"
v-refocus="focusStatus"
type="search"
class="van-search__input"
v-bind="$attrs"
v-refocus="focusStatus"
:value="value"
@input="onInput"
@focus="onFocus"
@keypress.enter.prevent="onSearch"
>
<icon name="clear" v-show="isFocus && value" @click="onClean" />
</div>
@@ -59,15 +57,36 @@ export default create({
}
},
computed: {
listeners() {
return {
...this.$listeners,
focus: this.onFocus,
input: this.onInput,
keypress: this.onKeypress
};
}
},
methods: {
onFocus() {
this.isFocus = true;
this.$emit('focus');
},
onInput(event) {
this.$emit('input', event.target.value);
},
onKeypress(event) {
// press enter
if (event.keyCode === 13) {
event.preventDefault();
this.$emit('search', this.value);
}
this.$emit('keypress', event);
},
// refocus after click close icon
onClean() {
this.$emit('input', '');
@@ -84,12 +103,6 @@ export default create({
this.$emit('cancel');
},
onSearch(e) {
e.preventDefault();
this.$emit('search', this.value);
return false;
},
onClickoutside() {
this.isFocus = false;
this.focusStatus = false;