uni-collapse-item.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view
  3. :class="['uni-collapse-cell', { 'uni-collapse-cell--disabled': disabled, 'uni-collapse-cell--open': isOpen }]"
  4. :hover-class="disabled ? '' : 'uni-collapse-cell--hover'">
  5. <view
  6. class="uni-collapse-cell__title header"
  7. @click="onClick">
  8. <view
  9. v-if="thumb"
  10. class="uni-collapse-cell__title-extra"><image
  11. :src="thumb"
  12. class="uni-collapse-cell__title-img" /></view>
  13. <view class="uni-collapse-cell__title-inner">
  14. <view class="uni-collapse-cell__title-text">{{ title }}</view>
  15. </view>
  16. <view
  17. :class="{ 'uni-active': isOpen, 'uni-collapse-cell--animation': showAnimation === true }"
  18. class="uni-collapse-cell__title-arrow">
  19. <uni-icons
  20. color="#bbb"
  21. size="20"
  22. type="arrowdown" />
  23. </view>
  24. </view>
  25. <view
  26. :style="{ height: isOpen ? 'auto' : '0px' }"
  27. class="uni-collapse-cell__content">
  28. <view :class="{ 'uni-collapse-cell--animation': showAnimation === true }" :style="{ transform: isOpen ? 'translateY(0px)' : 'translateY(-50%)','-webkit-transform' : isOpen ? 'translateY(0px)' : 'translateY(-50%)' }">
  29. <slot />
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import uniIcons from '../uni-icons/uni-icons.vue'
  36. export default {
  37. name: 'UniCollapseItem',
  38. components: {
  39. uniIcons
  40. },
  41. props: {
  42. title: {
  43. // 列表标题
  44. type: String,
  45. default: ''
  46. },
  47. name: {
  48. // 唯一标识符
  49. type: [Number, String],
  50. default: 0
  51. },
  52. disabled: {
  53. // 是否禁用
  54. type: [Boolean, String],
  55. default: false
  56. },
  57. showAnimation: {
  58. // 是否显示动画
  59. type: Boolean,
  60. default: false
  61. },
  62. open: {
  63. // 是否展开
  64. type: [Boolean, String],
  65. default: false
  66. },
  67. thumb: {
  68. // 缩略图
  69. type: String,
  70. default: ''
  71. }
  72. },
  73. data () {
  74. return {
  75. isOpen: false
  76. }
  77. },
  78. watch: {
  79. open (val) {
  80. this.isOpen = val
  81. }
  82. },
  83. inject: ['collapse'],
  84. created () {
  85. this.isOpen = this.open
  86. this.nameSync = this.name ? this.name : this.collapse.childrens.length
  87. this.collapse.childrens.push(this)
  88. if (String(this.collapse.accordion) === 'true') {
  89. if (this.isOpen) {
  90. let lastEl = this.collapse.childrens[this.collapse.childrens.length - 2]
  91. if (lastEl) {
  92. this.collapse.childrens[this.collapse.childrens.length - 2].isOpen = false
  93. }
  94. }
  95. }
  96. },
  97. methods: {
  98. onClick () {
  99. if (this.disabled) {
  100. return
  101. }
  102. if (String(this.collapse.accordion) === 'true') {
  103. this.collapse.childrens.forEach(vm => {
  104. if (vm === this) {
  105. return
  106. }
  107. vm.isOpen = false
  108. })
  109. }
  110. this.isOpen = !this.isOpen
  111. this.collapse.onChange && this.collapse.onChange()
  112. this.$forceUpdate()
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss">
  118. @mixin collapse-hover {
  119. background-color: #f5f5f5;
  120. }
  121. @mixin collapse-disabled {
  122. opacity: 0.3;
  123. }
  124. $collapse-title-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  125. .uni-collapse-cell {
  126. position: relative;
  127. &--hover {
  128. @include collapse-hover;
  129. }
  130. &--open {
  131. @include collapse-hover;
  132. }
  133. &--disabled {
  134. @include collapse-disabled;
  135. }
  136. &--animation {
  137. transition: all 0.3s;
  138. }
  139. &:after {
  140. position: absolute;
  141. z-index: 3;
  142. right: 0;
  143. bottom: 0;
  144. left: 0px;
  145. height: 1px;
  146. content: '';
  147. -webkit-transform: scaleY(0.5);
  148. transform: scaleY(0.5);
  149. background-color: $uni-border-color;
  150. }
  151. &__title {
  152. padding: $collapse-title-pd;
  153. width: 100%;
  154. box-sizing: border-box;
  155. flex: 1;
  156. position: relative;
  157. display: flex;
  158. flex-direction: row;
  159. justify-content: space-between;
  160. align-items: center;
  161. &-extra {
  162. margin-right: 18upx;
  163. display: flex;
  164. flex-direction: row;
  165. justify-content: center;
  166. align-items: center;
  167. }
  168. &-img {
  169. height: $uni-img-size-base;
  170. width: $uni-img-size-base;
  171. }
  172. &-arrow {
  173. width: 20px;
  174. height: 20px;
  175. transform: rotate(0deg);
  176. transform-origin: center center;
  177. &.uni-active {
  178. transform: rotate(180deg);
  179. }
  180. }
  181. &-inner {
  182. flex: 1;
  183. overflow: hidden;
  184. display: flex;
  185. flex-direction: column;
  186. }
  187. &-text {
  188. font-size: $uni-font-size-lg;
  189. text-overflow: ellipsis;
  190. white-space: nowrap;
  191. color: inherit;
  192. line-height: 1.5;
  193. overflow: hidden;
  194. }
  195. }
  196. &__content {
  197. position: relative;
  198. width: 100%;
  199. overflow: hidden;
  200. .view {
  201. font-size: $uni-font-size-base;
  202. }
  203. }
  204. }
  205. </style>