uni-collapse-item.vue 4.6 KB

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