Skip to content

Commit

Permalink
[bugfix] functional component should not inherit scropedSlots (youzan…
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Feb 15, 2019
1 parent 137fe83 commit 170dcbf
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 29 deletions.
3 changes: 2 additions & 1 deletion packages/card/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { use, isDef } from '../utils';
import { inherit } from '../utils/functional';
import Tag from '../tag';

const [sfc, bem] = use('card');
Expand Down Expand Up @@ -68,7 +69,7 @@ function Card(h, props, slots, ctx) {
);

return (
<div class={bem()} {...ctx.data}>
<div class={bem()} {...inherit(ctx, true)}>
<div class={bem('header')}>
{Thumb}
<div class={bem('content', { centered: props.centered })}>
Expand Down
3 changes: 2 additions & 1 deletion packages/cell-group/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { use } from '../utils';
import { inherit } from '../utils/functional';

const [sfc, bem] = use('cell-group');

function CellGroup(h, props, slots, ctx) {
return (
<div
class={[bem(), { 'van-hairline--top-bottom': props.border }]}
{...ctx.data}
{...inherit(ctx, true)}
>
{slots.default && slots.default()}
</div>
Expand Down
3 changes: 2 additions & 1 deletion packages/goods-action/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { use } from '../utils';
import { inherit } from '../utils/functional';

const [sfc, bem] = use('goods-action');

function GoodsAction(h, props, slots, ctx) {
return (
<div class={bem()} {...ctx.data}>
<div class={bem()} {...inherit(ctx, true)}>
{slots.default && slots.default()}
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/icon/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { use } from '../utils';
import { inherit } from '../utils/functional';
import Info from '../info';
import isSrc from '../utils/validate/src';

Expand All @@ -17,7 +18,7 @@ function Icon(h, props, slots, ctx) {
color: props.color,
fontSize: props.size
}}
{...ctx.data}
{...inherit(ctx, true)}
>
{ctx.default && ctx.default()}
{urlIcon && <img src={props.name} />}
Expand Down
3 changes: 2 additions & 1 deletion packages/info/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { use, isDef } from '../utils';
import { inherit } from '../utils/functional';

const [sfc, bem] = use('info');

function Info(h, props, slots, ctx) {
return (
isDef(props.info) && (
<div class={bem()} {...ctx.data}>
<div class={bem()} {...inherit(ctx, true)}>
{props.info}
</div>
)
Expand Down
3 changes: 2 additions & 1 deletion packages/loading/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { use } from '../utils';
import { inherit } from '../utils/functional';

const [sfc, bem] = use('loading');
const DEFAULT_COLOR = '#c9c9c9';
Expand Down Expand Up @@ -28,7 +29,7 @@ function Loading(h, props, slots, ctx) {
);

return (
<div class={bem([type, colorType])} style={style} {...ctx.data}>
<div class={bem([type, colorType])} style={style} {...inherit(ctx, true)}>
<span class={bem('spinner', type)}>
{Spin}
{Circular}
Expand Down
43 changes: 25 additions & 18 deletions packages/panel/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { use } from '../utils';
import Cell from '../cell';
import CellGroup from '../cell-group';
import { inherit } from '../utils/functional';

const [sfc, bem] = use('panel');

function Panel(h, props, slots, ctx) {
const Content = () => [
slots.header ? (
slots.header()
) : (
<Cell
icon={props.icon}
label={props.desc}
title={props.title}
value={props.status}
class={bem('header')}
valueClass={bem('header-value')}
/>
),
<div class={bem('content')}>{slots.default && slots.default()}</div>,
slots.footer && (
<div class={[bem('footer'), 'van-hairline--top']}>{slots.footer()}</div>
)
];

return (
<CellGroup class={bem()} {...ctx.data}>
{slots.header ? (
slots.header()
) : (
<Cell
icon={props.icon}
label={props.desc}
title={props.title}
value={props.status}
class={bem('header')}
valueClass={bem('header-value')}
/>
)}
<div class={bem('content')}>{slots.default && slots.default()}</div>
{slots.footer && (
<div class={[bem('footer'), 'van-hairline--top']}>{slots.footer()}</div>
)}
</CellGroup>
<CellGroup
class={bem()}
scopedSlots={{ default: Content }}
{...inherit(ctx, true)}
/>
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/password-input/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { use } from '../utils';
import { emit } from '../utils/functional';
import { emit, inherit } from '../utils/functional';

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

Expand All @@ -23,7 +23,7 @@ function PasswordInput(h, props, slots, ctx) {
event.stopPropagation();
emit(ctx, 'focus', event);
}}
{...ctx.data}
{...inherit(ctx, true)}
>
{Points}
</ul>
Expand Down
3 changes: 2 additions & 1 deletion packages/tag/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { use } from '../utils';
import { inherit } from '../utils/functional';
import { RED, BLUE, GREEN, GRAY_DARK } from '../utils/color';

const [sfc, bem] = use('tag');
Expand Down Expand Up @@ -29,7 +30,7 @@ function Tag(h, props, slots, ctx) {
'van-hairline--surround': plain
}
]}
{...ctx.data}
{...inherit(ctx, true)}
>
{slots.default && slots.default()}
</span>
Expand Down
11 changes: 9 additions & 2 deletions packages/utils/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const inheritKey = [
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };

// inherit partial context, map nativeOn to on
export function inherit(context: Context): InheritContext {
return inheritKey.reduce(
export function inherit(context: Context, inheritListeners: boolean): InheritContext {
const result = inheritKey.reduce(
(obj, key) => {
if (context.data[key]) {
obj[mapInheritKey[key] || key] = context.data[key];
Expand All @@ -31,6 +31,13 @@ export function inherit(context: Context): InheritContext {
},
{} as InheritContext
);

if (inheritListeners) {
result.on = result.on || {};
Object.assign(result.on, context.data.on);
}

return result;
}

// emit event
Expand Down

0 comments on commit 170dcbf

Please sign in to comment.