uni-collapse-item.vue 5.0 KB

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