diff --git a/packages/address-list/Item.js b/packages/address-list/Item.js index cf5000701..5163a1081 100644 --- a/packages/address-list/Item.js +++ b/packages/address-list/Item.js @@ -45,7 +45,7 @@ export default sfc({ return ( { - if (!loading && !disabled && listeners.click) { - listeners.click(event); + if (!loading && !disabled) { + emit(context, 'click', event); } }; @@ -59,7 +59,7 @@ export default sfc({ } ])} onClick={onClick} - {...inheritContext(context)} + {...inherit(context)} > {loading ? ( diff --git a/packages/contact-card/index.js b/packages/contact-card/index.js index bd9317609..2849c0e94 100644 --- a/packages/contact-card/index.js +++ b/packages/contact-card/index.js @@ -1,5 +1,5 @@ import { use } from '../utils'; -import { inheritContext } from '../utils/functional'; +import { emit, inherit } from '../utils/functional'; import Cell from '../cell'; const [sfc, bem, t] = use('contact-card'); @@ -22,7 +22,7 @@ export default sfc({ }, render(h, context) { - const { props, listeners } = context; + const { props } = context; const { type, editable } = props; return ( @@ -31,14 +31,14 @@ export default sfc({ border={false} isLink={editable} class={bem([type])} - value-class={bem('value')} + valueClass={bem('value')} icon={type === 'edit' ? 'contact' : 'add-square'} onClick={event => { - if (editable && listeners.click) { - listeners.click(event); + if (editable) { + emit(context, 'click', event); } }} - {...inheritContext(context)} + {...inherit(context)} > {type === 'add' ? props.addText || t('addText') diff --git a/packages/contact-list/index.js b/packages/contact-list/index.js index 3ed828453..b45d28c76 100644 --- a/packages/contact-list/index.js +++ b/packages/contact-list/index.js @@ -1,5 +1,5 @@ import { use, noop } from '../utils'; -import { inheritContext } from '../utils/functional'; +import { emit, inherit } from '../utils/functional'; import Icon from '../icon'; import Cell from '../cell'; import Button from '../button'; @@ -25,7 +25,7 @@ export default sfc({ key={item.id} isLink class={bem('item')} - value-class={bem('item-value')} + valueClass={bem('item-value')} scopedSlots={{ default: () => ( @@ -38,20 +38,20 @@ export default sfc({ class={bem('edit')} onClick={event => { event.stopPropagation(); - listeners.edit && listeners.edit(item, index); + emit(context, 'edit', item, index); }} /> ) }} onClick={() => { - listeners.input && listeners.input(item.id); - listeners.select && listeners.select(item, index); + emit(context, 'input', item.id); + emit(context, 'select', item, index); }} /> )); return ( -
+
{List} diff --git a/packages/field/index.js b/packages/field/index.js index 2df26552c..131741cf6 100644 --- a/packages/field/index.js +++ b/packages/field/index.js @@ -220,7 +220,7 @@ export default sfc({ border={this.border} isLink={this.isLink} required={this.required} - title-class={bem('label', labelAlign)} + titleClass={bem('label', labelAlign)} class={bem({ error: this.error, disabled: this.$attrs.disabled, diff --git a/packages/nav-bar/index.js b/packages/nav-bar/index.js index c923c8ac8..676c3b09c 100644 --- a/packages/nav-bar/index.js +++ b/packages/nav-bar/index.js @@ -1,5 +1,5 @@ import { use, noop } from '../utils'; -import { inheritContext } from '../utils/functional'; +import { inherit } from '../utils/functional'; import Icon from '../icon'; const [sfc, bem] = use('nav-bar'); @@ -31,7 +31,7 @@ export default sfc({
{slots.left || [ diff --git a/packages/panel/index.js b/packages/panel/index.js index 7704f6fc8..27c18a2fc 100644 --- a/packages/panel/index.js +++ b/packages/panel/index.js @@ -27,7 +27,7 @@ export default sfc({ title={props.title} value={props.status} class={bem('header')} - value-class={bem('header-value')} + valueClass={bem('header-value')} /> )}
{slots.default}
diff --git a/packages/password-input/index.js b/packages/password-input/index.js index 5a109c356..1338e834d 100644 --- a/packages/password-input/index.js +++ b/packages/password-input/index.js @@ -1,4 +1,5 @@ import { use } from '../utils'; +import { emit } from '../utils/functional'; const [sfc, bem] = use('password-input'); @@ -19,7 +20,7 @@ export default sfc({ }, render(h, context) { - const { props, listeners } = context; + const { props } = context; const info = props.errorInfo || props.info; const Points = []; @@ -37,7 +38,7 @@ export default sfc({ class={[bem('security'), 'van-hairline--surround']} onTouchstart={event => { event.stopPropagation(); - listeners.focus && listeners.focus(); + emit(context, 'focus', event); }} {...context.data} > diff --git a/packages/submit-bar/index.js b/packages/submit-bar/index.js index d49eb2613..86a30c1cc 100644 --- a/packages/submit-bar/index.js +++ b/packages/submit-bar/index.js @@ -1,5 +1,5 @@ import { use, noop } from '../utils'; -import { inheritContext } from '../utils/functional'; +import { inherit } from '../utils/functional'; import Button from '../button'; const [sfc, bem, t] = use('submit-bar'); @@ -34,7 +34,7 @@ export default sfc({ const hasPrice = typeof price === 'number'; return ( -
+
{slots.top} {(slots.tip || tip) && (
diff --git a/packages/switch-cell/index.js b/packages/switch-cell/index.js index a88a50974..8da552669 100644 --- a/packages/switch-cell/index.js +++ b/packages/switch-cell/index.js @@ -1,5 +1,5 @@ import { use } from '../utils'; -import { inheritContext } from '../utils/functional'; +import { inherit } from '../utils/functional'; import Cell from '../cell'; import Switch from '../switch'; import SwitchMixin from '../mixins/switch'; @@ -24,7 +24,7 @@ export default sfc({ const { props } = context; return ( - + ); diff --git a/packages/utils/functional.ts b/packages/utils/functional.ts index aba54550e..a37567ee5 100644 --- a/packages/utils/functional.ts +++ b/packages/utils/functional.ts @@ -20,7 +20,8 @@ const inheritKey = [ const mapInheritKey: ObjectIndex = { nativeOn: 'on' }; -export function inheritContext(context: Context): InheritContext { +// inherit partial context, map nativeOn to on +export function inherit(context: Context): InheritContext { return inheritKey.reduce( (obj, key) => { if (context.data[key]) { @@ -31,3 +32,17 @@ export function inheritContext(context: Context): InheritContext { {} as InheritContext ); } + +// emit event +export function emit(context: Context, eventName: string, ...args: any[]) { + const listeners = context.listeners[eventName]; + if (listeners) { + if (Array.isArray(listeners)) { + listeners.forEach(listener => { + listener(...args); + }) + } else { + listeners(...args); + } + } +}