[new feature] add Collapse component (#674)

This commit is contained in:
neverland
2018-03-12 17:40:20 +08:00
committed by GitHub
parent 0b7468f596
commit 0768aab52c
14 changed files with 523 additions and 4 deletions
+4 -4
View File
@@ -11,8 +11,8 @@
:error="isError"
:on-icon-click="onIconClick"
@input="$emit('input', $event)"
@focus="handleFocus"
@blur="handleBlur"
@focus="onFocus"
@blur="onBlur"
>
<div slot="icon">
<span v-if="showIcon && isAndroid" class="van-address-edit-detail__finish-edit">{{ $t('complete') }}</span>
@@ -79,13 +79,13 @@ export default create({
},
methods: {
handleFocus(e) {
onFocus(e) {
this.isFocused = true;
this.$emit('focus', e);
this.$refs.root.scrollIntoView();
},
handleBlur(e) {
onBlur(e) {
// wait for click event finished
setTimeout(() => {
this.isFocused = false;
+75
View File
@@ -0,0 +1,75 @@
<template>
<div
class="van-collapse-item"
:class="{
'van-hairline--top': index,
'van-collapse-item--expanded': expanded
}"
>
<cell class="van-collapse-item__title" is-link @click="onClick">
<slot name="title">{{ title }}</slot>
</cell>
<div class="van-collapse-item__content" v-show="expanded">
<slot />
</div>
</div>
</template>
<script>
import Cell from '../cell';
import findParent from '../mixins/find-parent';
import { create, isDef } from '../utils';
export default create({
name: 'van-collapse-item',
mixins: [findParent],
components: {
Cell
},
props: {
name: [String, Number],
title: String
},
computed: {
items() {
return this.parentGroup.items;
},
index() {
return this.items.indexOf(this);
},
currentName() {
return isDef(this.name) ? this.name : this.index;
},
expanded() {
const { activeNames } = this.parentGroup;
return this.parentGroup.accordion
? activeNames === this.currentName
: activeNames.some(name => name === this.currentName);
}
},
created() {
this.findParentByName('van-collapse');
this.items.push(this);
},
destroyed() {
this.items.splice(this.index, 1);
},
methods: {
onClick() {
const { parentGroup } = this;
const name = parentGroup.accordion && this.currentName === parentGroup.activeNames ? '' : this.currentName;
this.parentGroup.switch(name, !this.expanded);
}
}
});
</script>
+41
View File
@@ -0,0 +1,41 @@
<template>
<div class="van-collapse van-hairline--top-bottom">
<slot />
</div>
</template>
<script>
import { create } from '../utils';
export default create({
name: 'van-collapse',
model: {
prop: 'activeNames'
},
props: {
accordion: Boolean,
activeNames: [String, Number, Array]
},
data() {
return {
items: []
};
},
methods: {
switch(name, expanded) {
const { activeNames } = this;
if (!this.accordion) {
name = expanded
? activeNames.concat(name)
: activeNames.filter(activeName => activeName !== name);
}
this.$emit('change', name);
this.$emit('input', name);
}
}
});
</script>
+6
View File
@@ -14,6 +14,8 @@ import Checkbox from './checkbox';
import CheckboxGroup from './checkbox-group';
import Circle from './circle';
import Col from './col';
import Collapse from './collapse';
import CollapseItem from './collapse-item';
import ContactCard from './contact-card';
import ContactEdit from './contact-edit';
import ContactList from './contact-list';
@@ -80,6 +82,8 @@ const components = [
CheckboxGroup,
Circle,
Col,
Collapse,
CollapseItem,
ContactCard,
ContactEdit,
ContactList,
@@ -153,6 +157,8 @@ export {
CheckboxGroup,
Circle,
Col,
Collapse,
CollapseItem,
ContactCard,
ContactEdit,
ContactList,
+31
View File
@@ -0,0 +1,31 @@
@import './common/var.css';
.van-collapse-item {
&__title {
.van-cell__right-icon::before {
transition: .3s;
transform: rotate(90deg);
}
&::after {
visibility: hidden;
}
}
&__content {
padding: 15px;
background-color: #fff;
}
&--expanded {
.van-collapse-item__title {
.van-cell__right-icon::before {
transform: rotate(-90deg);
}
&::after {
visibility: visible;
}
}
}
}
+1
View File
@@ -13,6 +13,7 @@
@import './button.css';
@import './cell.css';
@import './circle.css';
@import './collapse.css';
@import './loading.css';
@import './nav-bar.css';
@import './notice-bar.css';