[improvement] Search: jsx (#2621)

This commit is contained in:
neverland 2019-01-25 21:52:21 +08:00 committed by GitHub
parent eeb6893892
commit 0f1cb12211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 87 deletions

76
packages/search/index.js Normal file
View File

@ -0,0 +1,76 @@
import { use } from '../utils';
import Field from '../field';
const [sfc, bem, t] = use('search');
export default sfc({
inheritAttrs: false,
props: {
value: String,
showAction: Boolean,
background: {
type: String,
default: '#f2f2f2'
}
},
computed: {
listeners() {
return {
...this.$listeners,
input: this.onInput,
keypress: this.onKeypress
};
}
},
methods: {
onInput(value) {
this.$emit('input', value);
},
onKeypress(event) {
// press enter
if (event.keyCode === 13) {
event.preventDefault();
this.$emit('search', this.value);
}
this.$emit('keypress', event);
},
onBack() {
this.$emit('input', '');
this.$emit('cancel');
}
},
render(h) {
const { showAction } = this;
const props = {
attrs: this.$attrs,
on: this.listeners
};
return (
<div class={bem({ 'show-action': showAction })} style={{ background: this.background }}>
<Field
clearable
type="search"
value={this.value}
border={false}
leftIcon="search"
{...props}
>
{h('template', { slot: 'left-icon' }, this.$slots['left-icon'])}
</Field>
{showAction && (
<div class={bem('action')}>
{this.$slots.action || <div onClick={this.onBack}>{t('cancel')}</div>}
</div>
)}
</div>
);
}
});

View File

@ -1,86 +0,0 @@
<template>
<div
:class="b({ 'show-action': showAction })"
:style="{ background }"
>
<field
v-bind="$attrs"
v-on="listeners"
clearable
type="search"
:value="value"
:border="false"
left-icon="search"
>
<slot
name="left-icon"
slot="left-icon"
/>
</field>
<div
v-if="showAction"
:class="b('action')"
>
<slot name="action">
<div
v-text="$t('cancel')"
@click="onBack"
/>
</slot>
</div>
</div>
</template>
<script>
import Field from '../field';
import create from '../utils/create';
export default create({
name: 'search',
inheritAttrs: false,
components: {
Field
},
props: {
value: String,
showAction: Boolean,
background: {
type: String,
default: '#f2f2f2'
}
},
computed: {
listeners() {
return {
...this.$listeners,
input: this.onInput,
keypress: this.onKeypress
};
}
},
methods: {
onInput(value) {
this.$emit('input', value);
},
onKeypress(event) {
// press enter
if (event.keyCode === 13) {
event.preventDefault();
this.$emit('search', this.value);
}
this.$emit('keypress', event);
},
onBack() {
this.$emit('input', '');
this.$emit('cancel');
}
}
});
</script>

View File

@ -11,7 +11,6 @@ exports[`renders demo correctly 1`] = `
<div class="van-field__body"><input type="search" placeholder="请输入搜索关键词" value="" class="van-field__control"></div>
</div>
</div>
<!---->
</div>
</div>
<div>