Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] functional component multi listeners #2717

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/address-list/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default sfc({
return (
<Cell
class={bem({ disabled, unswitchable: !switchable })}
value-class={bem('value')}
valueClass={bem('value')}
isLink={!disabled && switchable}
scopedSlots={{
default: this.renderContent,
Expand Down
10 changes: 5 additions & 5 deletions packages/button/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { use } from '../utils';
import { inheritContext } from '../utils/functional';
import { emit, inherit } from '../utils/functional';
import Loading from '../loading';

const [sfc, bem] = use('button');
Expand Down Expand Up @@ -32,12 +32,12 @@ export default sfc({
},

render(h, context) {
const { props, listeners } = context;
const { props } = context;
const { type, disabled, loading } = props;

const onClick = event => {
if (!loading && !disabled && listeners.click) {
listeners.click(event);
if (!loading && !disabled) {
emit(context, 'click', event);
}
};

Expand All @@ -59,7 +59,7 @@ export default sfc({
}
])}
onClick={onClick}
{...inheritContext(context)}
{...inherit(context)}
>
{loading ? (
<Loading size="20px" color={type === 'default' ? undefined : ''} />
Expand Down
12 changes: 6 additions & 6 deletions packages/contact-card/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -22,7 +22,7 @@ export default sfc({
},

render(h, context) {
const { props, listeners } = context;
const { props } = context;
const { type, editable } = props;

return (
Expand All @@ -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')
Expand Down
12 changes: 6 additions & 6 deletions packages/contact-list/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: () => (
<Radio name={item.id}>
Expand All @@ -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 (
<div class={bem()} {...inheritContext(context)}>
<div class={bem()} {...inherit(context)}>
<RadioGroup value={props.value} class={bem('group')}>
{List}
</RadioGroup>
Expand Down
2 changes: 1 addition & 1 deletion packages/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/nav-bar/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -31,7 +31,7 @@ export default sfc({
<div
class={[bem({ fixed: props.fixed }), { 'van-hairline--bottom': props.border }]}
style={{ zIndex: props.zIndex }}
{...inheritContext(context)}
{...inherit(context)}
>
<div class={bem('left')} onClick={listeners['click-left'] || noop}>
{slots.left || [
Expand Down
2 changes: 1 addition & 1 deletion packages/panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
/>
)}
<div class={bem('content')}>{slots.default}</div>
Expand Down
5 changes: 3 additions & 2 deletions packages/password-input/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { use } from '../utils';
import { emit } from '../utils/functional';

const [sfc, bem] = use('password-input');

Expand All @@ -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 = [];
Expand All @@ -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}
>
Expand Down
4 changes: 2 additions & 2 deletions packages/submit-bar/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -34,7 +34,7 @@ export default sfc({
const hasPrice = typeof price === 'number';

return (
<div class={bem()} {...inheritContext(context)}>
<div class={bem()} {...inherit(context)}>
{slots.top}
{(slots.tip || tip) && (
<div class={bem('tip')}>
Expand Down
4 changes: 2 additions & 2 deletions packages/switch-cell/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,7 +24,7 @@ export default sfc({
const { props } = context;

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 }} />
</Cell>
);
Expand Down
17 changes: 16 additions & 1 deletion packages/utils/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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);
}
}
}