chore: improve Collapse、ContactCard jsx segment

This commit is contained in:
陈嘉涵 2019-12-16 12:01:57 +08:00
parent 9c6ae2c28b
commit 1dd0d42b5f
4 changed files with 71 additions and 51 deletions

View File

@ -25,7 +25,7 @@ function CellGroup(
) {
const Group = (
<div class={[bem(), { [BORDER_TOP_BOTTOM]: props.border }]} {...inherit(ctx, true)}>
{slots.default && slots.default()}
{slots.default?.()}
</div>
);

View File

@ -152,6 +152,7 @@ export default createComponent({
const now = Date.now();
const progress = Math.min((now - this.startTime) / this.duration, 1);
const rate = progress * (this.endRate - this.startRate) + this.startRate;
this.$emit('input', format(parseFloat(rate.toFixed(1))));
if (this.increase ? rate < this.endRate : rate > this.endRate) {

View File

@ -6,6 +6,7 @@ import { cellProps } from '../cell/shared';
import { ChildrenMixin } from '../mixins/relation';
const [createComponent, bem] = createNamespace('collapse-item');
const CELL_SLOTS = ['title', 'icon', 'right-icon'];
export default createComponent({
@ -40,7 +41,11 @@ export default createComponent({
const { value, accordion } = this.parent;
if (process.env.NODE_ENV !== 'production' && !accordion && !Array.isArray(value)) {
if (
process.env.NODE_ENV !== 'production' &&
!accordion &&
!Array.isArray(value)
) {
console.error('[Vant] Collapse: type of prop "value" should be Array');
return;
}
@ -73,6 +78,7 @@ export default createComponent({
nextTick(() => {
const { content, wrapper } = this.$refs;
if (!content || !wrapper) {
return;
}
@ -99,9 +105,9 @@ export default createComponent({
return;
}
const { parent } = this;
const name =
parent.accordion && this.currentName === parent.value ? '' : this.currentName;
const { parent, currentName } = this;
const name = parent.accordion && currentName === parent.value ? '' : currentName;
this.parent.switch(name, !this.expanded);
},
@ -111,52 +117,59 @@ export default createComponent({
} else {
this.$refs.wrapper.style.height = '';
}
},
genTitle() {
const { disabled, expanded } = this;
const titleSlots = CELL_SLOTS.reduce((slots, name) => {
if (this.slots(name)) {
slots[name] = () => this.slots(name);
}
return slots;
}, {});
if (this.slots('value')) {
titleSlots.default = () => this.slots('value');
}
return (
<Cell
role="button"
class={bem('title', { disabled, expanded })}
onClick={this.onClick}
scopedSlots={titleSlots}
tabindex={disabled ? -1 : 0}
aria-expanded={String(expanded)}
{...{ props: this.$props }}
/>
);
},
genContent() {
if (this.inited) {
return (
<div
vShow={this.show}
ref="wrapper"
class={bem('wrapper')}
onTransitionend={this.onTransitionEnd}
>
<div ref="content" class={bem('content')}>
{this.slots()}
</div>
</div>
);
}
}
},
render() {
const { disabled, expanded } = this;
const titleSlots = CELL_SLOTS.reduce((slots, name) => {
if (this.slots(name)) {
slots[name] = () => this.slots(name);
}
return slots;
}, {});
if (this.slots('value')) {
titleSlots.default = () => this.slots('value');
}
const Title = (
<Cell
role="button"
class={bem('title', { disabled, expanded })}
onClick={this.onClick}
scopedSlots={titleSlots}
tabindex={disabled ? -1 : 0}
aria-expanded={String(expanded)}
{...{ props: this.$props }}
/>
);
const Content = this.inited && (
<div
vShow={this.show}
ref="wrapper"
class={bem('wrapper')}
onTransitionend={this.onTransitionEnd}
>
<div ref="content" class={bem('content')}>
{this.slots()}
</div>
</div>
);
return (
<div class={[bem(), { [BORDER_TOP]: this.index }]}>
{Title}
{Content}
{this.genTitle()}
{this.genContent()}
</div>
);
}

View File

@ -30,6 +30,17 @@ function ContactCard(
}
}
function Content() {
if (type === 'add') {
return props.addText || t('addText');
}
return [
<div>{`${t('name')}${props.name}`}</div>,
<div>{`${t('tel')}${props.tel}`}</div>
];
}
return (
<Cell
center
@ -41,12 +52,7 @@ function ContactCard(
onClick={onClick}
{...inherit(ctx)}
>
{type === 'add'
? props.addText || t('addText')
: [
<div>{`${t('name')}${props.name}`}</div>,
<div>{`${t('tel')}${props.tel}`}</div>
]}
{Content()}
</Cell>
);
}