[improvement] GoodsAction: subComponents functional (#2730)

This commit is contained in:
neverland 2019-02-13 17:56:19 +08:00 committed by GitHub
parent 4aad9add9c
commit d7c337710c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 36 deletions

View File

@ -1,7 +1,7 @@
import { use, isDef } from '../utils'; import { use, isDef } from '../utils';
import { cellProps } from './shared'; import { cellProps } from './shared';
import { emit, inherit, unifySlots } from '../utils/functional'; import { emit, inherit, unifySlots } from '../utils/functional';
import { routeProps, functionalRoute } from '../mixins/router-link'; import { routeProps, functionalRoute } from '../mixins/router';
import Icon from '../icon'; import Icon from '../icon';
const [sfc, bem] = use('cell'); const [sfc, bem] = use('cell');

View File

@ -1,10 +1,13 @@
import { use } from '../utils'; import { use } from '../utils';
import Button from '../button'; import Button from '../button';
import { route, routeProps } from '../mixins/router-link'; import { emit, inherit, unifySlots } from '../utils/functional';
import { functionalRoute, routeProps } from '../mixins/router';
const [sfc, bem] = use('goods-action-big-btn'); const [sfc, bem] = use('goods-action-big-btn');
export default sfc({ export default sfc({
functional: true,
props: { props: {
...routeProps, ...routeProps,
text: String, text: String,
@ -13,25 +16,27 @@ export default sfc({
disabled: Boolean disabled: Boolean
}, },
methods: { render(h, context) {
onClick(event) { const { props } = context;
this.$emit('click', event); const slots = unifySlots(context);
route(this.$router, this);
} const onClick = event => {
}, emit(context, 'click', event);
functionalRoute(context);
};
render(h) {
return ( return (
<Button <Button
square square
class={bem()} class={bem()}
size="large" size="large"
loading={this.loading} loading={props.loading}
disabled={this.disabled} disabled={props.disabled}
type={this.primary ? 'danger' : 'warning'} type={props.primary ? 'danger' : 'warning'}
onClick={this.onClick} onClick={onClick}
{...inherit(context)}
> >
{this.slots() || this.text} {slots.default ? slots.default() : props.text}
</Button> </Button>
); );
} }

View File

@ -1,30 +1,42 @@
import { use } from '../utils'; import { use } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
import { route, routeProps } from '../mixins/router-link'; import { emit, inherit, unifySlots } from '../utils/functional';
import { functionalRoute, routeProps } from '../mixins/router';
const [sfc, bem] = use('goods-action-mini-btn'); const [sfc, bem] = use('goods-action-mini-btn');
export default sfc({ export default sfc({
functional: true,
props: { props: {
...routeProps, ...routeProps,
text: String, text: String,
info: [String, Number],
icon: String, icon: String,
info: [String, Number],
iconClass: String iconClass: String
}, },
methods: { render(h, context) {
onClick(event) { const { props } = context;
this.$emit('click', event); const slots = unifySlots(context);
route(this.$router, this);
} const onClick = event => {
}, emit(context, 'click', event);
functionalRoute(context);
};
render(h) {
return ( return (
<div class={[bem(), 'van-hairline']} onClick={this.onClick}> <div
<Icon class={[bem('icon'), this.iconClass]} info={this.info} name={this.icon} /> class={[bem(), 'van-hairline']}
{this.slots() || this.text} onClick={onClick}
{...inherit(context)}
>
<Icon
class={[bem('icon'), props.iconClass]}
info={props.info}
name={props.icon}
/>
{slots.default ? slots.default() : props.text}
</div> </div>
); );
} }

View File

@ -1,17 +1,31 @@
import BigBtn from '../../goods-action-big-btn'; import BigBtn from '../../goods-action-big-btn';
import SmallBtn from '../../goods-action-mini-btn'; import MiniBtn from '../../goods-action-mini-btn';
import { mount } from '../../../test/utils'; import { mount } from '../../../test/utils';
test('big btn click event', () => { test('BigBtn click event', () => {
const wrapper = mount(BigBtn); const click = jest.fn();
const wrapper = mount(BigBtn, {
context: {
on: {
click
}
}
});
wrapper.trigger('click'); wrapper.trigger('click');
expect(wrapper.emitted('click')).toBeTruthy(); expect(click.mock.calls.length).toEqual(1);
}); });
test('small btn click event', () => { test('MiniBtn click event', () => {
const wrapper = mount(SmallBtn); const click = jest.fn();
const wrapper = mount(MiniBtn, {
context: {
on: {
click
}
}
});
wrapper.trigger('click'); wrapper.trigger('click');
expect(wrapper.emitted('click')).toBeTruthy(); expect(click.mock.calls.length).toEqual(1);
}); });

View File

@ -1,8 +1,8 @@
/** /**
* add Vue-Router support * Vue Router support
*/ */
import { RenderContext } from 'vue'; import { RenderContext } from 'vue/types';
import VueRouter, { RawLocation } from 'vue-router/types'; import VueRouter, { RawLocation } from 'vue-router/types';
export type RouteConfig = { export type RouteConfig = {

View File

@ -1,7 +1,7 @@
import { use } from '../utils'; import { use } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
import Info from '../info'; import Info from '../info';
import { route, routeProps } from '../mixins/router-link'; import { route, routeProps } from '../mixins/router';
const [sfc, bem] = use('tabbar-item'); const [sfc, bem] = use('tabbar-item');