[bugfix] functional component multi listeners (#2717)

This commit is contained in:
neverland 2019-02-12 10:44:40 +08:00 committed by GitHub
parent 0bf4a9e799
commit e6584a28d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 29 deletions

View File

@ -45,7 +45,7 @@ export default sfc({
return ( return (
<Cell <Cell
class={bem({ disabled, unswitchable: !switchable })} class={bem({ disabled, unswitchable: !switchable })}
value-class={bem('value')} valueClass={bem('value')}
isLink={!disabled && switchable} isLink={!disabled && switchable}
scopedSlots={{ scopedSlots={{
default: this.renderContent, default: this.renderContent,

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { inheritContext } from '../utils/functional'; import { emit, inherit } from '../utils/functional';
import Loading from '../loading'; import Loading from '../loading';
const [sfc, bem] = use('button'); const [sfc, bem] = use('button');
@ -32,12 +32,12 @@ export default sfc({
}, },
render(h, context) { render(h, context) {
const { props, listeners } = context; const { props } = context;
const { type, disabled, loading } = props; const { type, disabled, loading } = props;
const onClick = event => { const onClick = event => {
if (!loading && !disabled && listeners.click) { if (!loading && !disabled) {
listeners.click(event); emit(context, 'click', event);
} }
}; };
@ -59,7 +59,7 @@ export default sfc({
} }
])} ])}
onClick={onClick} onClick={onClick}
{...inheritContext(context)} {...inherit(context)}
> >
{loading ? ( {loading ? (
<Loading size="20px" color={type === 'default' ? undefined : ''} /> <Loading size="20px" color={type === 'default' ? undefined : ''} />

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { inheritContext } from '../utils/functional'; import { emit, inherit } from '../utils/functional';
import Cell from '../cell'; import Cell from '../cell';
const [sfc, bem, t] = use('contact-card'); const [sfc, bem, t] = use('contact-card');
@ -22,7 +22,7 @@ export default sfc({
}, },
render(h, context) { render(h, context) {
const { props, listeners } = context; const { props } = context;
const { type, editable } = props; const { type, editable } = props;
return ( return (
@ -31,14 +31,14 @@ export default sfc({
border={false} border={false}
isLink={editable} isLink={editable}
class={bem([type])} class={bem([type])}
value-class={bem('value')} valueClass={bem('value')}
icon={type === 'edit' ? 'contact' : 'add-square'} icon={type === 'edit' ? 'contact' : 'add-square'}
onClick={event => { onClick={event => {
if (editable && listeners.click) { if (editable) {
listeners.click(event); emit(context, 'click', event);
} }
}} }}
{...inheritContext(context)} {...inherit(context)}
> >
{type === 'add' {type === 'add'
? props.addText || t('addText') ? props.addText || t('addText')

View File

@ -1,5 +1,5 @@
import { use, noop } from '../utils'; import { use, noop } from '../utils';
import { inheritContext } from '../utils/functional'; import { emit, inherit } from '../utils/functional';
import Icon from '../icon'; import Icon from '../icon';
import Cell from '../cell'; import Cell from '../cell';
import Button from '../button'; import Button from '../button';
@ -25,7 +25,7 @@ export default sfc({
key={item.id} key={item.id}
isLink isLink
class={bem('item')} class={bem('item')}
value-class={bem('item-value')} valueClass={bem('item-value')}
scopedSlots={{ scopedSlots={{
default: () => ( default: () => (
<Radio name={item.id}> <Radio name={item.id}>
@ -38,20 +38,20 @@ export default sfc({
class={bem('edit')} class={bem('edit')}
onClick={event => { onClick={event => {
event.stopPropagation(); event.stopPropagation();
listeners.edit && listeners.edit(item, index); emit(context, 'edit', item, index);
}} }}
/> />
) )
}} }}
onClick={() => { onClick={() => {
listeners.input && listeners.input(item.id); emit(context, 'input', item.id);
listeners.select && listeners.select(item, index); emit(context, 'select', item, index);
}} }}
/> />
)); ));
return ( return (
<div class={bem()} {...inheritContext(context)}> <div class={bem()} {...inherit(context)}>
<RadioGroup value={props.value} class={bem('group')}> <RadioGroup value={props.value} class={bem('group')}>
{List} {List}
</RadioGroup> </RadioGroup>

View File

@ -220,7 +220,7 @@ export default sfc({
border={this.border} border={this.border}
isLink={this.isLink} isLink={this.isLink}
required={this.required} required={this.required}
title-class={bem('label', labelAlign)} titleClass={bem('label', labelAlign)}
class={bem({ class={bem({
error: this.error, error: this.error,
disabled: this.$attrs.disabled, disabled: this.$attrs.disabled,

View File

@ -1,5 +1,5 @@
import { use, noop } from '../utils'; import { use, noop } from '../utils';
import { inheritContext } from '../utils/functional'; import { inherit } from '../utils/functional';
import Icon from '../icon'; import Icon from '../icon';
const [sfc, bem] = use('nav-bar'); const [sfc, bem] = use('nav-bar');
@ -31,7 +31,7 @@ export default sfc({
<div <div
class={[bem({ fixed: props.fixed }), { 'van-hairline--bottom': props.border }]} class={[bem({ fixed: props.fixed }), { 'van-hairline--bottom': props.border }]}
style={{ zIndex: props.zIndex }} style={{ zIndex: props.zIndex }}
{...inheritContext(context)} {...inherit(context)}
> >
<div class={bem('left')} onClick={listeners['click-left'] || noop}> <div class={bem('left')} onClick={listeners['click-left'] || noop}>
{slots.left || [ {slots.left || [

View File

@ -27,7 +27,7 @@ export default sfc({
title={props.title} title={props.title}
value={props.status} value={props.status}
class={bem('header')} class={bem('header')}
value-class={bem('header-value')} valueClass={bem('header-value')}
/> />
)} )}
<div class={bem('content')}>{slots.default}</div> <div class={bem('content')}>{slots.default}</div>

View File

@ -1,4 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { emit } from '../utils/functional';
const [sfc, bem] = use('password-input'); const [sfc, bem] = use('password-input');
@ -19,7 +20,7 @@ export default sfc({
}, },
render(h, context) { render(h, context) {
const { props, listeners } = context; const { props } = context;
const info = props.errorInfo || props.info; const info = props.errorInfo || props.info;
const Points = []; const Points = [];
@ -37,7 +38,7 @@ export default sfc({
class={[bem('security'), 'van-hairline--surround']} class={[bem('security'), 'van-hairline--surround']}
onTouchstart={event => { onTouchstart={event => {
event.stopPropagation(); event.stopPropagation();
listeners.focus && listeners.focus(); emit(context, 'focus', event);
}} }}
{...context.data} {...context.data}
> >

View File

@ -1,5 +1,5 @@
import { use, noop } from '../utils'; import { use, noop } from '../utils';
import { inheritContext } from '../utils/functional'; import { inherit } from '../utils/functional';
import Button from '../button'; import Button from '../button';
const [sfc, bem, t] = use('submit-bar'); const [sfc, bem, t] = use('submit-bar');
@ -34,7 +34,7 @@ export default sfc({
const hasPrice = typeof price === 'number'; const hasPrice = typeof price === 'number';
return ( return (
<div class={bem()} {...inheritContext(context)}> <div class={bem()} {...inherit(context)}>
{slots.top} {slots.top}
{(slots.tip || tip) && ( {(slots.tip || tip) && (
<div class={bem('tip')}> <div class={bem('tip')}>

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { inheritContext } from '../utils/functional'; import { inherit } from '../utils/functional';
import Cell from '../cell'; import Cell from '../cell';
import Switch from '../switch'; import Switch from '../switch';
import SwitchMixin from '../mixins/switch'; import SwitchMixin from '../mixins/switch';
@ -24,7 +24,7 @@ export default sfc({
const { props } = context; const { props } = context;
return ( return (
<Cell center title={props.title} border={props.border} class={bem()} {...inheritContext(context)}> <Cell center title={props.title} border={props.border} class={bem()} {...inherit(context)}>
<Switch {...{ props, on: context.listeners }} /> <Switch {...{ props, on: context.listeners }} />
</Cell> </Cell>
); );

View File

@ -20,7 +20,8 @@ const inheritKey = [
const mapInheritKey: ObjectIndex = { nativeOn: 'on' }; 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( return inheritKey.reduce(
(obj, key) => { (obj, key) => {
if (context.data[key]) { if (context.data[key]) {
@ -31,3 +32,17 @@ export function inheritContext(context: Context): InheritContext {
{} as 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);
}
}
}