uni-collapse-item.vue 4.9 KB

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