[improvement] Functional: ContactList, NavBar, Panel, SubmitBar, SwitchCell, Tag (#2675)

This commit is contained in:
neverland 2019-02-02 16:04:54 +08:00 committed by GitHub
parent 3c6c32e305
commit 5926d02d38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 302 additions and 265 deletions

View File

@ -1,4 +1,4 @@
import { use } from '../utils'; import { use, noop } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
import Cell from '../cell'; import Cell from '../cell';
import Button from '../button'; import Button from '../button';
@ -7,55 +7,57 @@ import RadioGroup from '../radio-group';
const [sfc, bem, t] = use('contact-list'); const [sfc, bem, t] = use('contact-list');
export default sfc({ export default sfc(
{
props: { props: {
value: null, value: null,
list: Array, list: Array,
addText: String addText: String
}, },
render(h) { render(h, context) {
return ( const { props, listeners } = context;
<div class={bem()}>
<RadioGroup const List = props.list.map((item, index) => (
value={this.value} <Cell
class={bem('group')} key={item.id}
onInput={event => { isLink
this.$emit('input', event);
}}
>
{this.list.map((item, index) => (
<Cell key={item.id} isLink>
<Radio
name={item.id}
onClick={() => { onClick={() => {
this.$emit('select', item, index); listeners.input && listeners.input(item.id);
listeners.select && listeners.select(item, index);
}} }}
> >
<Radio name={item.id}>
<div class={bem('name')}>{`${item.name}${item.tel}`}</div> <div class={bem('name')}>{`${item.name}${item.tel}`}</div>
</Radio> </Radio>
<Icon <Icon
slot="right-icon" slot="right-icon"
name="edit" name="edit"
class={bem('edit')} class={bem('edit')}
onClick={() => { onClick={event => {
this.$emit('edit', item, index); event.stopPropagation();
listeners.edit && listeners.edit(item, index);
}} }}
/> />
</Cell> </Cell>
))} ));
return (
<div class={bem()} {...context.data}>
<RadioGroup value={props.value} class={bem('group')}>
{List}
</RadioGroup> </RadioGroup>
<Button <Button
square square
size="large" size="large"
type="danger" type="danger"
class={bem('add')} class={bem('add')}
text={this.addText || t('addText')} text={props.addText || t('addText')}
onClick={() => { onClick={listeners.add || noop}
this.$emit('add');
}}
/> />
</div> </div>
); );
} }
}); },
true
);

View File

@ -19,16 +19,24 @@
margin-left: 27px; margin-left: 27px;
} }
.van-radio__input { .van-radio__icon {
top: 50%; top: 50%;
left: 0; left: 0;
font-size: 16px; height: 16px;
position: absolute; position: absolute;
line-height: 16px;
transform: translate(0, -50%); transform: translate(0, -50%);
.van-icon {
width: 16px;
height: 16px;
font-size: 12px;
}
} }
.van-icon-checked { .van-radio__icon--checked .van-icon {
color: @red; border-color: @red;
background-color: @red;
} }
&__group { &__group {

View File

@ -1,9 +1,10 @@
import { use } from '../utils'; import { use, noop } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
const [sfc, bem] = use('nav-bar'); const [sfc, bem] = use('nav-bar');
export default sfc({ export default sfc(
{
props: { props: {
title: String, title: String,
fixed: Boolean, fixed: Boolean,
@ -20,34 +21,35 @@ export default sfc({
} }
}, },
render(h) { render(h, context) {
const { props, listeners } = context;
const slots = context.slots();
return ( return (
<div <div
class={[bem({ fixed: this.fixed }), { 'van-hairline--bottom': this.border }]} class={[bem({ fixed: props.fixed }), { 'van-hairline--bottom': props.border }]}
style={{ zIndex: this.zIndex }} style={{ zIndex: props.zIndex }}
{...context.data}
> >
<div <div
class={bem('left')} class={bem('left')}
onClick={() => { onClick={listeners['click-left'] || noop}
this.$emit('click-left');
}}
> >
{this.$slots.left || [ {slots.left || [
this.leftArrow && <Icon class={bem('arrow')} name="arrow-left" />, props.leftArrow && <Icon class={bem('arrow')} name="arrow-left" />,
this.leftText && <span class={bem('text')}>{this.leftText}</span> props.leftText && <span class={bem('text')}>{props.leftText}</span>
]} ]}
</div> </div>
<div class={[bem('title'), 'van-ellipsis']}>{this.$slots.title || this.title}</div> <div class={[bem('title'), 'van-ellipsis']}>{slots.title || props.title}</div>
<div <div
class={bem('right')} class={bem('right')}
onClick={() => { onClick={listeners['click-right'] || noop}
this.$emit('click-right');
}}
> >
{this.$slots.right || {slots.right || (props.rightText && <span class={bem('text')}>{props.rightText}</span>)}
(this.rightText && <span class={bem('text')}>{this.rightText}</span>)}
</div> </div>
</div> </div>
); );
} }
}); },
true
);

View File

@ -4,7 +4,8 @@ import CellGroup from '../cell-group';
const [sfc, bem] = use('panel'); const [sfc, bem] = use('panel');
export default sfc({ export default sfc(
{
props: { props: {
icon: String, icon: String,
desc: String, desc: String,
@ -12,18 +13,19 @@ export default sfc({
status: String status: String
}, },
render(h) { render(h, context) {
const slots = this.$slots; const { props } = context;
const slots = context.slots();
return ( return (
<CellGroup class={bem()}> <CellGroup class={bem()} {...context.data}>
{slots.header || ( {slots.header || (
<Cell <Cell
class={bem('header')} class={bem('header')}
icon={this.icon} icon={props.icon}
label={this.desc} label={props.desc}
title={this.title} title={props.title}
value={this.status} value={props.status}
/> />
)} )}
<div class={bem('content')}>{slots.default}</div> <div class={bem('content')}>{slots.default}</div>
@ -31,4 +33,6 @@ export default sfc({
</CellGroup> </CellGroup>
); );
} }
}); },
true
);

View File

@ -1,9 +1,10 @@
import { use } from '../utils'; import { use, noop } from '../utils';
import Button from '../button'; import Button from '../button';
const [sfc, bem, t] = use('submit-bar'); const [sfc, bem, t] = use('submit-bar');
export default sfc({ export default sfc(
{
props: { props: {
tip: String, tip: String,
label: String, label: String,
@ -24,40 +25,42 @@ export default sfc({
} }
}, },
render(h) { render(h, context) {
const { tip, price, $slots } = this; const { props, listeners } = context;
const { tip, price } = props;
const slots = context.slots();
const hasPrice = typeof price === 'number'; const hasPrice = typeof price === 'number';
return ( return (
<div class={bem()}> <div class={bem()} {...context.data}>
{$slots.top} {slots.top}
{($slots.tip || tip) && ( {(slots.tip || tip) && (
<div class={bem('tip')}> <div class={bem('tip')}>
{tip} {tip}
{$slots.tip} {slots.tip}
</div> </div>
)} )}
<div class={bem('bar')}> <div class={bem('bar')}>
{$slots.default} {slots.default}
<div class={bem('text')}> <div class={bem('text')}>
{hasPrice && [ {hasPrice && [
<span>{this.label || t('label')}</span>, <span>{props.label || t('label')}</span>,
<span class={bem('price')}>{`${this.currency} ${(price / 100).toFixed(2)}`}</span> <span class={bem('price')}>{`${props.currency} ${(price / 100).toFixed(2)}`}</span>
]} ]}
</div> </div>
<Button <Button
square square
size="large" size="large"
type={this.buttonType} type={props.buttonType}
loading={this.loading} loading={props.loading}
disabled={this.disabled} disabled={props.disabled}
text={this.loading ? '' : this.buttonText} text={props.loading ? '' : props.buttonText}
onClick={() => { onClick={listeners.submit || noop}
this.$emit('submit');
}}
/> />
</div> </div>
</div> </div>
); );
} }
}); },
true
);

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`submit 1`] = ` exports[`disable submit 1`] = `
<div class="van-submit-bar"> <div class="van-submit-bar">
<div class="van-submit-bar__bar"> <div class="van-submit-bar__bar">
<div class="van-submit-bar__text"><span>合计:</span><span class="van-submit-bar__price">¥ 0.00</span></div><button disabled="disabled" class="van-button van-button--danger van-button--large van-button--square van-button--disabled"><span class="van-button__text"></span></button> <div class="van-submit-bar__text"><span>合计:</span><span class="van-submit-bar__price">¥ 0.00</span></div><button disabled="disabled" class="van-button van-button--danger van-button--large van-button--square van-button--disabled"><span class="van-button__text"></span></button>

View File

@ -2,10 +2,30 @@ import SubmitBar from '..';
import { mount } from '../../../test/utils'; import { mount } from '../../../test/utils';
test('submit', () => { test('submit', () => {
const submit = jest.fn();
const wrapper = mount(SubmitBar, { const wrapper = mount(SubmitBar, {
propsData: { context: {
props: {
price: 0.01
},
on: { submit }
}
});
const button = wrapper.find('.van-button');
button.trigger('click');
expect(submit.mock.calls[0]).toBeTruthy();
});
test('disable submit', () => {
const submit = jest.fn();
const wrapper = mount(SubmitBar, {
context: {
props: {
price: 0.01, price: 0.01,
disabled: true disabled: true
},
on: { submit }
} }
}); });
@ -14,10 +34,5 @@ test('submit', () => {
// disabled // disabled
const button = wrapper.find('.van-button'); const button = wrapper.find('.van-button');
button.trigger('click'); button.trigger('click');
expect(wrapper.emitted('submit')).toBeFalsy(); expect(submit.mock.calls[0]).toBeFalsy();
// submit
wrapper.vm.disabled = false;
button.trigger('click');
expect(wrapper.emitted('submit')).toBeTruthy();
}); });

View File

@ -5,7 +5,8 @@ import SwitchMixin from '../mixins/switch';
const [sfc, bem] = use('switch-cell'); const [sfc, bem] = use('switch-cell');
export default sfc({ export default sfc(
{
mixins: [SwitchMixin], mixins: [SwitchMixin],
props: { props: {
@ -17,22 +18,21 @@ export default sfc({
} }
}, },
watch: { render(h, context) {
value() { const { props } = context;
this.$emit('change', this.value);
}
},
render(h) {
return ( return (
<Cell center title={this.title} border={this.border} class={bem()}> <Cell
<Switch center
{...{ props: this.$props }} title={props.title}
onInput={value => { border={props.border}
this.$emit('input', value); style={context.style}
}} class={[bem(), context.class, context.staticClass]}
/> >
<Switch {...{ props, on: context.listeners }} />
</Cell> </Cell>
); );
} }
}); },
true
);

View File

@ -1,13 +1,20 @@
import SwitchCell from '..'; import SwitchCell from '..';
import { mount } from '../../../test/utils'; import { mount } from '../../../test/utils';
test('emit event', () => { test('change event', () => {
const wrapper = mount(SwitchCell); const onChange = jest.fn();
const wrapper = mount(SwitchCell, {
context: {
on: {
change: onChange
}
}
});
wrapper.vm.$on('input', value => { wrapper.vm.$on('input', value => {
wrapper.setProps({ value }); wrapper.setProps({ value });
}); });
wrapper.find('.van-switch').trigger('click'); wrapper.find('.van-switch').trigger('click');
expect(wrapper.emitted('change')).toBeTruthy(); expect(onChange.mock.calls[0]).toBeTruthy();
}); });

View File

@ -9,7 +9,8 @@ const COLOR_MAP = {
success: GREEN success: GREEN
}; };
export default sfc({ export default sfc(
{
props: { props: {
size: String, size: String,
type: String, type: String,
@ -20,40 +21,33 @@ export default sfc({
textColor: String textColor: String
}, },
computed: { render(h, context) {
style() { const { props } = context;
const color = this.color || COLOR_MAP[this.type] || GRAY_DARK; const { mark, plain, round, size } = context.props;
const key = this.plain ? 'color' : 'backgroundColor';
const color = props.color || COLOR_MAP[props.type] || GRAY_DARK;
const key = plain ? 'color' : 'backgroundColor';
const style = { [key]: color }; const style = { [key]: color };
if (this.textColor) { if (props.textColor) {
style.color = this.textColor; style.color = props.textColor;
} }
return style;
}
},
render(h) {
const {
mark,
plain,
round,
size
} = this;
return ( return (
<span <span
style={style}
class={[ class={[
bem({ mark, plain, round, [size]: size }), bem({ mark, plain, round, [size]: size }),
{ {
'van-hairline--surround': plain 'van-hairline--surround': plain
} }
]} ]}
style={this.style} {...context.data}
> >
{this.$slots.default} {context.children}
</span> </span>
); );
} }
}); },
true
);

View File

@ -4,6 +4,8 @@ export { use, useSlots } from './use';
export const isServer = Vue.prototype.$isServer; export const isServer = Vue.prototype.$isServer;
export function noop() {}
export function isDef(value) { export function isDef(value) {
return value !== undefined && value !== null; return value !== undefined && value !== null;
} }