uni-collapse-item.vue 5.1 KB

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