uni-collapse-item.vue 4.7 KB

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