mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 18:36:51 +08:00
feat: migrate Collapse component
This commit is contained in:
parent
0b90092d1c
commit
79511fe331
@ -60,4 +60,6 @@ module.exports = [
|
|||||||
'dropdown-menu',
|
'dropdown-menu',
|
||||||
'dropdown-item',
|
'dropdown-item',
|
||||||
'notify',
|
'notify',
|
||||||
|
'collapse',
|
||||||
|
'collapse-item',
|
||||||
];
|
];
|
||||||
|
@ -11,8 +11,6 @@ import { cellProps } from '../cell/shared';
|
|||||||
|
|
||||||
const [createComponent, bem] = createNamespace('collapse-item');
|
const [createComponent, bem] = createNamespace('collapse-item');
|
||||||
|
|
||||||
const CELL_SLOTS = ['title', 'icon', 'right-icon'];
|
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [ChildrenMixin('vanCollapse')],
|
mixins: [ChildrenMixin('vanCollapse')],
|
||||||
|
|
||||||
@ -43,20 +41,22 @@ export default createComponent({
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { value, accordion } = this.parent;
|
const { modelValue, accordion } = this.parent;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
process.env.NODE_ENV !== 'production' &&
|
process.env.NODE_ENV !== 'production' &&
|
||||||
!accordion &&
|
!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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return accordion
|
return accordion
|
||||||
? value === this.currentName
|
? modelValue === this.currentName
|
||||||
: value.some((name) => name === this.currentName);
|
: modelValue.some((name) => name === this.currentName);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { parent, currentName } = this;
|
const { parent, currentName } = this;
|
||||||
const close = parent.accordion && currentName === parent.value;
|
const close = parent.accordion && currentName === parent.modelValue;
|
||||||
const name = close ? '' : currentName;
|
const name = close ? '' : currentName;
|
||||||
|
|
||||||
parent.switch(name, !this.expanded);
|
parent.switch(name, !this.expanded);
|
||||||
@ -127,27 +127,22 @@ export default createComponent({
|
|||||||
genTitle() {
|
genTitle() {
|
||||||
const { border, disabled, expanded } = this;
|
const { border, disabled, expanded } = this;
|
||||||
|
|
||||||
const titleSlots = CELL_SLOTS.reduce((slots, name) => {
|
const slots = {
|
||||||
if (this.slots(name)) {
|
icon: this.$slots.icon,
|
||||||
slots[name] = () => this.slots(name);
|
title: this.$slots.title,
|
||||||
}
|
default: this.$slots.value,
|
||||||
|
'right-icon': this.$slots['right-icon'],
|
||||||
return slots;
|
};
|
||||||
}, {});
|
|
||||||
|
|
||||||
if (this.slots('value')) {
|
|
||||||
titleSlots.default = () => this.slots('value');
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Cell
|
<Cell
|
||||||
|
v-slots={slots}
|
||||||
role="button"
|
role="button"
|
||||||
class={bem('title', { disabled, expanded, borderless: !border })}
|
class={bem('title', { disabled, expanded, borderless: !border })}
|
||||||
onClick={this.onClick}
|
onClick={this.onClick}
|
||||||
scopedSlots={titleSlots}
|
|
||||||
tabindex={disabled ? -1 : 0}
|
tabindex={disabled ? -1 : 0}
|
||||||
aria-expanded={String(expanded)}
|
aria-expanded={String(expanded)}
|
||||||
{...{ props: this.$props }}
|
{...this.$props}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@ -162,7 +157,7 @@ export default createComponent({
|
|||||||
onTransitionend={this.onTransitionEnd}
|
onTransitionend={this.onTransitionEnd}
|
||||||
>
|
>
|
||||||
<div ref="content" class={bem('content')}>
|
<div ref="content" class={bem('content')}>
|
||||||
{this.slots()}
|
{this.$slots.default?.()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -9,29 +9,31 @@ export default createComponent({
|
|||||||
|
|
||||||
props: {
|
props: {
|
||||||
accordion: Boolean,
|
accordion: Boolean,
|
||||||
value: [String, Number, Array],
|
modelValue: [String, Number, Array],
|
||||||
border: {
|
border: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
emits: ['change', 'update:modelValue'],
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
switch(name, expanded) {
|
switch(name, expanded) {
|
||||||
if (!this.accordion) {
|
if (!this.accordion) {
|
||||||
name = expanded
|
name = expanded
|
||||||
? this.value.concat(name)
|
? this.modelValue.concat(name)
|
||||||
: this.value.filter((activeName) => activeName !== name);
|
: this.modelValue.filter((activeName) => activeName !== name);
|
||||||
}
|
}
|
||||||
this.$emit('change', name);
|
this.$emit('change', name);
|
||||||
this.$emit('input', name);
|
this.$emit('update:modelValue', name);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div class={[bem(), { [BORDER_TOP_BOTTOM]: this.border }]}>
|
<div class={[bem(), { [BORDER_TOP_BOTTOM]: this.border }]}>
|
||||||
{this.slots()}
|
{this.$slots.default?.()}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -225,10 +225,10 @@ module.exports = {
|
|||||||
path: 'circle',
|
path: 'circle',
|
||||||
title: 'Circle 环形进度条',
|
title: 'Circle 环形进度条',
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// path: 'collapse',
|
path: 'collapse',
|
||||||
// title: 'Collapse 折叠面板',
|
title: 'Collapse 折叠面板',
|
||||||
// },
|
},
|
||||||
{
|
{
|
||||||
path: 'count-down',
|
path: 'count-down',
|
||||||
title: 'CountDown 倒计时',
|
title: 'CountDown 倒计时',
|
||||||
@ -559,10 +559,10 @@ module.exports = {
|
|||||||
path: 'circle',
|
path: 'circle',
|
||||||
title: 'Circle',
|
title: 'Circle',
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// path: 'collapse',
|
path: 'collapse',
|
||||||
// title: 'Collapse',
|
title: 'Collapse',
|
||||||
// },
|
},
|
||||||
{
|
{
|
||||||
path: 'count-down',
|
path: 'count-down',
|
||||||
title: 'CountDown',
|
title: 'CountDown',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user