From 735acabcec0c9c78c3f67f7d31aa150d52bdbdb8 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Sun, 16 Aug 2020 15:08:28 +0800 Subject: [PATCH] feat: migrate Search component --- components.js | 1 + src/search/index.js | 115 +++++++++++++++++++++++++++++++++ src/search/index.tsx | 149 ------------------------------------------- vant.config.js | 16 ++--- 4 files changed, 124 insertions(+), 157 deletions(-) create mode 100644 src/search/index.js delete mode 100644 src/search/index.tsx diff --git a/components.js b/components.js index 367d3a8bb..1e669fd16 100644 --- a/components.js +++ b/components.js @@ -54,4 +54,5 @@ module.exports = [ 'datetime-picker', 'number-keyboard', 'password-input', + 'search', ]; diff --git a/src/search/index.js b/src/search/index.js new file mode 100644 index 000000000..8fb1035c7 --- /dev/null +++ b/src/search/index.js @@ -0,0 +1,115 @@ +// Utils +import { createNamespace } from '../utils'; +import { preventDefault } from '../utils/dom/event'; + +// Components +import Field from '../field'; + +const [createComponent, bem, t] = createNamespace('search'); + +export default createComponent({ + inheritAttrs: false, + + props: { + label: String, + rightIcon: String, + modelValue: String, + actionText: String, + background: String, + showAction: Boolean, + clearTrigger: String, + shape: { + type: String, + default: 'square', + }, + clearable: { + type: Boolean, + default: true, + }, + leftIcon: { + type: String, + default: 'search', + }, + }, + + emits: ['search', 'cancel', 'keypress'], + + setup(props, { emit, slots }) { + return function () { + function Label() { + if (slots.label || props.label) { + return ( +
+ {slots.label ? slots.label() : props.label} +
+ ); + } + } + + function Action() { + if (!props.showAction) { + return; + } + + function onCancel() { + if (slots.action) { + return; + } + + emit('update:modelValue', ''); + emit('cancel'); + } + + return ( +
+ {slots.action ? slots.action() : props.actionText || t('cancel')} +
+ ); + } + + const fieldData = { + ...this.$attrs, + onKeypress(event) { + // press enter + if (event.keyCode === 13) { + preventDefault(event); + emit('search', props.modelValue); + } + emit('keypress', event); + }, + }; + + return ( +
+ {slots.left?.()} +
+ {Label()} + +
+ {Action()} +
+ ); + }; + }, +}); diff --git a/src/search/index.tsx b/src/search/index.tsx deleted file mode 100644 index 2068174cc..000000000 --- a/src/search/index.tsx +++ /dev/null @@ -1,149 +0,0 @@ -// Utils -import { createNamespace } from '../utils'; -import { inherit, emit } from '../utils/functional'; -import { preventDefault } from '../utils/dom/event'; - -// Components -import Field from '../field'; - -// Types -import { CreateElement, RenderContext } from 'vue/types'; -import { DefaultSlots, ScopedSlot } from '../utils/types'; - -const [createComponent, bem, t] = createNamespace('search'); - -export type SearchProps = { - shape: 'sqaure' | 'round'; - value?: string; - label?: string; - leftIcon: string; - rightIcon?: string; - clearable: boolean; - background: string; - actionText?: string; - showAction?: boolean; - clearTrigger?: string; -}; - -export type SearchSlots = DefaultSlots & { - left?: ScopedSlot; - label?: ScopedSlot; - action?: ScopedSlot; - 'left-icon'?: ScopedSlot; - 'right-icon'?: ScopedSlot; -}; - -export type SearchEvents = { - onCancel?(): void; - onInput?(value: string): void; - onSearch?(value: string): void; - onKeypress?(event: KeyboardEvent): void; -}; - -function Search( - h: CreateElement, - props: SearchProps, - slots: SearchSlots, - ctx: RenderContext -) { - function Label() { - if (slots.label || props.label) { - return ( -
- {slots.label ? slots.label() : props.label} -
- ); - } - } - - function Action() { - if (!props.showAction) { - return; - } - - function onCancel() { - if (slots.action) { - return; - } - - emit(ctx, 'input', ''); - emit(ctx, 'cancel'); - } - - return ( -
- {slots.action ? slots.action() : props.actionText || t('cancel')} -
- ); - } - - const fieldData = { - attrs: ctx.data.attrs, - on: { - ...ctx.listeners, - keypress(event: KeyboardEvent) { - // press enter - if (event.keyCode === 13) { - preventDefault(event); - emit(ctx, 'search', props.value); - } - emit(ctx, 'keypress', event); - }, - }, - }; - - const inheritData = inherit(ctx); - inheritData.attrs = undefined; - - return ( -
- {slots.left?.()} -
- {Label()} - -
- {Action()} -
- ); -} - -Search.props = { - value: String, - label: String, - rightIcon: String, - actionText: String, - background: String, - showAction: Boolean, - clearTrigger: String, - shape: { - type: String, - default: 'square', - }, - clearable: { - type: Boolean, - default: true, - }, - leftIcon: { - type: String, - default: 'search', - }, -}; - -export default createComponent(Search); diff --git a/vant.config.js b/vant.config.js index 3dfb2f8ef..4b55b09cd 100644 --- a/vant.config.js +++ b/vant.config.js @@ -155,10 +155,10 @@ module.exports = { path: 'rate', title: 'Rate 评分', }, - // { - // path: 'search', - // title: 'Search 搜索', - // }, + { + path: 'search', + title: 'Search 搜索', + }, { path: 'slider', title: 'Slider 滑块', @@ -489,10 +489,10 @@ module.exports = { path: 'rate', title: 'Rate', }, - // { - // path: 'search', - // title: 'Search', - // }, + { + path: 'search', + title: 'Search', + }, { path: 'slider', title: 'Slider',