feat: migrate Collapse component

This commit is contained in:
chenjiahan 2020-08-16 17:06:55 +08:00
parent 0b90092d1c
commit 79511fe331
4 changed files with 34 additions and 35 deletions

View File

@ -60,4 +60,6 @@ module.exports = [
'dropdown-menu',
'dropdown-item',
'notify',
'collapse',
'collapse-item',
];

View File

@ -11,8 +11,6 @@ import { cellProps } from '../cell/shared';
const [createComponent, bem] = createNamespace('collapse-item');
const CELL_SLOTS = ['title', 'icon', 'right-icon'];
export default createComponent({
mixins: [ChildrenMixin('vanCollapse')],
@ -43,20 +41,22 @@ export default createComponent({
return null;
}
const { value, accordion } = this.parent;
const { modelValue, accordion } = this.parent;
if (
process.env.NODE_ENV !== 'production' &&
!accordion &&
!Array.isArray(value)
!Array.isArray(modelValue)
) {
console.error('[Vant] Collapse: type of prop "value" should be Array');
console.error(
'[Vant] Collapse: type of prop "modelValue" should be Array'
);
return;
}
return accordion
? value === this.currentName
: value.some((name) => name === this.currentName);
? modelValue === this.currentName
: modelValue.some((name) => name === this.currentName);
},
},
@ -110,7 +110,7 @@ export default createComponent({
}
const { parent, currentName } = this;
const close = parent.accordion && currentName === parent.value;
const close = parent.accordion && currentName === parent.modelValue;
const name = close ? '' : currentName;
parent.switch(name, !this.expanded);
@ -127,27 +127,22 @@ export default createComponent({
genTitle() {
const { border, 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 slots = {
icon: this.$slots.icon,
title: this.$slots.title,
default: this.$slots.value,
'right-icon': this.$slots['right-icon'],
};
return (
<Cell
v-slots={slots}
role="button"
class={bem('title', { disabled, expanded, borderless: !border })}
onClick={this.onClick}
scopedSlots={titleSlots}
tabindex={disabled ? -1 : 0}
aria-expanded={String(expanded)}
{...{ props: this.$props }}
{...this.$props}
/>
);
},
@ -162,7 +157,7 @@ export default createComponent({
onTransitionend={this.onTransitionEnd}
>
<div ref="content" class={bem('content')}>
{this.slots()}
{this.$slots.default?.()}
</div>
</div>
);

View File

@ -9,29 +9,31 @@ export default createComponent({
props: {
accordion: Boolean,
value: [String, Number, Array],
modelValue: [String, Number, Array],
border: {
type: Boolean,
default: true,
},
},
emits: ['change', 'update:modelValue'],
methods: {
switch(name, expanded) {
if (!this.accordion) {
name = expanded
? this.value.concat(name)
: this.value.filter((activeName) => activeName !== name);
? this.modelValue.concat(name)
: this.modelValue.filter((activeName) => activeName !== name);
}
this.$emit('change', name);
this.$emit('input', name);
this.$emit('update:modelValue', name);
},
},
render() {
return (
<div class={[bem(), { [BORDER_TOP_BOTTOM]: this.border }]}>
{this.slots()}
{this.$slots.default?.()}
</div>
);
},

View File

@ -225,10 +225,10 @@ module.exports = {
path: 'circle',
title: 'Circle 环形进度条',
},
// {
// path: 'collapse',
// title: 'Collapse 折叠面板',
// },
{
path: 'collapse',
title: 'Collapse 折叠面板',
},
{
path: 'count-down',
title: 'CountDown 倒计时',
@ -559,10 +559,10 @@ module.exports = {
path: 'circle',
title: 'Circle',
},
// {
// path: 'collapse',
// title: 'Collapse',
// },
{
path: 'collapse',
title: 'Collapse',
},
{
path: 'count-down',
title: 'CountDown',