uni-collapse-item.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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--disabled': disabled}" 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. /* #ifdef H5 */
  129. cursor: not-allowed !important;
  130. /* #endif */
  131. }
  132. .uni-collapse-cell--hide {
  133. height: 48px;
  134. }
  135. .uni-collapse-cell--animation {
  136. transition-property: transform;
  137. transition-duration: 0.3s;
  138. transition-timing-function: ease;
  139. }
  140. .uni-collapse-cell__title {
  141. padding: 12px 12px;
  142. position: relative;
  143. /* #ifndef APP-NVUE */
  144. display: flex;
  145. width: 100%;
  146. box-sizing: border-box;
  147. /* #endif */
  148. height: 48px;
  149. line-height: 24px;
  150. flex-direction: row;
  151. justify-content: space-between;
  152. align-items: center;
  153. /* #ifdef H5 */
  154. cursor: pointer;
  155. /* #endif */
  156. }
  157. .uni-collapse-cell__title:active {
  158. background-color: #f1f1f1;
  159. }
  160. .uni-collapse-cell__title-img {
  161. height: 26px;
  162. width: 26px;
  163. margin-right: 10px;
  164. }
  165. .uni-collapse-cell__title-arrow {
  166. width: 20px;
  167. height: 20px;
  168. transform: rotate(0deg);
  169. transform-origin: center center;
  170. }
  171. .uni-collapse-cell__title-arrow-active {
  172. transform: rotate(180deg);
  173. }
  174. .uni-collapse-cell__title-text {
  175. flex: 1;
  176. font-size: 14px;
  177. /* #ifndef APP-NVUE */
  178. white-space: nowrap;
  179. color: inherit;
  180. /* #endif */
  181. /* #ifdef APP-NVUE */
  182. lines: 1;
  183. /* #endif */
  184. overflow: hidden;
  185. text-overflow: ellipsis;
  186. }
  187. .uni-collapse-cell__content {
  188. overflow: hidden;
  189. }
  190. .uni-collapse-cell__wrapper {
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. flex-direction: column;
  195. }
  196. .uni-collapse-cell__content--hide {
  197. height: 0px;
  198. line-height: 0px;
  199. }
  200. </style>