uni-collapse-item.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <view class="uni-collapse-item">
  3. <!-- onClick(!isOpen) -->
  4. <view @click="onClick(!isOpen)" class="uni-collapse-item__title"
  5. :class="{'is-open':isOpen &&titleBorder === 'auto' ,'uni-collapse-item-border':titleBorder !== 'none'}">
  6. <view class="uni-collapse-item__title-wrap">
  7. <slot name="title">
  8. <view class="uni-collapse-item__title-box" :class="{'is-disabled':disabled}">
  9. <image v-if="thumb" :src="thumb" class="uni-collapse-item__title-img" />
  10. <text class="uni-collapse-item__title-text">{{ title }}</text>
  11. </view>
  12. </slot>
  13. </view>
  14. <view v-if="showArrow" class="flex1 cldelistz" :class="isOpen?'act':''">
  15. <view class="cofe f15">{{isOpen?'信息折叠':'信息展开'}}</view>
  16. <image :src="upimg" ></image>
  17. </view>
  18. <!-- <view v-if="showArrow"
  19. :class="{ 'uni-collapse-item__title-arrow-active': isOpen, 'uni-collapse-item--animation': showAnimation === true }"
  20. class="uni-collapse-item__title-arrow">
  21. <uni-icons :color="disabled?'#ddd':'#bbb'" size="14" type="bottom" />
  22. </view> -->
  23. </view>
  24. <view class="uni-collapse-item__wrap" :class="{'is--transition':showAnimation}"
  25. :style="{height: (isOpen?height:0) +'px'}">
  26. <view :id="elId" ref="collapse--hook" class="uni-collapse-item__wrap-content"
  27. :class="{open:isheight,'uni-collapse-item--border':border&&isOpen}">
  28. <slot></slot>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // #ifdef APP-NVUE
  35. const dom = weex.requireModule('dom')
  36. // #endif
  37. /**
  38. * CollapseItem 折叠面板子组件
  39. * @description 折叠面板子组件
  40. * @property {String} title 标题文字
  41. * @property {String} thumb 标题左侧缩略图
  42. * @property {String} name 唯一标志符
  43. * @property {Boolean} open = [true|false] 是否展开组件
  44. * @property {Boolean} titleBorder = [true|false] 是否显示标题分隔线
  45. * @property {Boolean} border = [true|false] 是否显示分隔线
  46. * @property {Boolean} disabled = [true|false] 是否展开面板
  47. * @property {Boolean} showAnimation = [true|false] 开启动画
  48. * @property {Boolean} showArrow = [true|false] 是否显示右侧箭头
  49. */
  50. export default {
  51. name: 'uniCollapseItem',
  52. props: {
  53. // 列表标题
  54. title: {
  55. type: String,
  56. default: ''
  57. },
  58. name: {
  59. type: [Number, String],
  60. default: ''
  61. },
  62. // 是否禁用
  63. disabled: {
  64. type: Boolean,
  65. default: false
  66. },
  67. // #ifdef APP-PLUS
  68. // 是否显示动画,app 端默认不开启动画,卡顿严重
  69. showAnimation: {
  70. type: Boolean,
  71. default: false
  72. },
  73. // #endif
  74. // #ifndef APP-PLUS
  75. // 是否显示动画
  76. showAnimation: {
  77. type: Boolean,
  78. default: true
  79. },
  80. // #endif
  81. // 是否展开
  82. open: {
  83. type: Boolean,
  84. default: false
  85. },
  86. // 缩略图
  87. thumb: {
  88. type: String,
  89. default: ''
  90. },
  91. // 标题分隔线显示类型
  92. titleBorder: {
  93. type: String,
  94. default: 'auto'
  95. },
  96. border: {
  97. type: Boolean,
  98. default: true
  99. },
  100. showArrow: {
  101. type: Boolean,
  102. default: true
  103. }
  104. },
  105. data() {
  106. // TODO 随机生生元素ID,解决百度小程序获取同一个元素位置信息的bug
  107. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  108. return {
  109. isOpen: false,
  110. isheight: null,
  111. height: 0,
  112. elId,
  113. nameSync: 0,
  114. upimg:require('@/static/images/index/up.png'),
  115. }
  116. },
  117. watch: {
  118. open(val) {
  119. this.isOpen = val
  120. this.onClick(val, 'init')
  121. }
  122. },
  123. updated(e) {
  124. this.$nextTick(() => {
  125. this.init(true)
  126. })
  127. },
  128. created() {
  129. this.collapse = this.getCollapse()
  130. this.oldHeight = 0
  131. this.onClick(this.open, 'init')
  132. },
  133. // #ifndef VUE3
  134. // TODO vue2
  135. destroyed() {
  136. if (this.__isUnmounted) return
  137. this.uninstall()
  138. },
  139. // #endif
  140. // #ifdef VUE3
  141. // TODO vue3
  142. unmounted() {
  143. this.__isUnmounted = true
  144. this.uninstall()
  145. },
  146. // #endif
  147. mounted() {
  148. if (!this.collapse) return
  149. if (this.name !== '') {
  150. this.nameSync = this.name
  151. } else {
  152. this.nameSync = this.collapse.childrens.length + ''
  153. }
  154. if (this.collapse.names.indexOf(this.nameSync) === -1) {
  155. this.collapse.names.push(this.nameSync)
  156. } else {
  157. console.warn(`name 值 ${this.nameSync} 重复`);
  158. }
  159. if (this.collapse.childrens.indexOf(this) === -1) {
  160. this.collapse.childrens.push(this)
  161. }
  162. this.init()
  163. },
  164. methods: {
  165. init(type) {
  166. // #ifndef APP-NVUE
  167. this.getCollapseHeight(type)
  168. // #endif
  169. // #ifdef APP-NVUE
  170. this.getNvueHwight(type)
  171. // #endif
  172. },
  173. uninstall() {
  174. if (this.collapse) {
  175. this.collapse.childrens.forEach((item, index) => {
  176. if (item === this) {
  177. this.collapse.childrens.splice(index, 1)
  178. }
  179. })
  180. this.collapse.names.forEach((item, index) => {
  181. if (item === this.nameSync) {
  182. this.collapse.names.splice(index, 1)
  183. }
  184. })
  185. }
  186. },
  187. onClick(isOpen, type) {
  188. if (this.disabled) return
  189. this.isOpen = isOpen
  190. if (this.isOpen && this.collapse) {
  191. this.collapse.setAccordion(this)
  192. }
  193. if (type !== 'init') {
  194. this.collapse.onChange(isOpen, this)
  195. }
  196. },
  197. getCollapseHeight(type, index = 0) {
  198. const views = uni.createSelectorQuery().in(this)
  199. views
  200. .select(`#${this.elId}`)
  201. .fields({
  202. size: true
  203. }, data => {
  204. // TODO 百度中可能获取不到节点信息 ,需要循环获取
  205. if (index >= 10) return
  206. if (!data) {
  207. index++
  208. this.getCollapseHeight(false, index)
  209. return
  210. }
  211. // #ifdef APP-NVUE
  212. this.height = data.height + 1
  213. // #endif
  214. // #ifndef APP-NVUE
  215. this.height = data.height
  216. // #endif
  217. this.isheight = true
  218. if (type) return
  219. this.onClick(this.isOpen, 'init')
  220. })
  221. .exec()
  222. },
  223. getNvueHwight(type) {
  224. const result = dom.getComponentRect(this.$refs['collapse--hook'], option => {
  225. if (option && option.result && option.size) {
  226. // #ifdef APP-NVUE
  227. this.height = option.size.height + 1
  228. // #endif
  229. // #ifndef APP-NVUE
  230. this.height = option.size.height
  231. // #endif
  232. this.isheight = true
  233. if (type) return
  234. this.onClick(this.open, 'init')
  235. }
  236. })
  237. },
  238. /**
  239. * 获取父元素实例
  240. */
  241. getCollapse(name = 'uniCollapse') {
  242. let parent = this.$parent;
  243. let parentName = parent.$options.name;
  244. while (parentName !== name) {
  245. parent = parent.$parent;
  246. if (!parent) return false;
  247. parentName = parent.$options.name;
  248. }
  249. return parent;
  250. }
  251. }
  252. }
  253. </script>
  254. <style lang="scss">
  255. .cldelistz{display: flex;align-items: center;justify-content: flex-end;padding-right: 24rpx;
  256. image{width: 26rpx;height: 20rpx;margin-left: 20rpx;transition: all .5s; }
  257. &.act{
  258. image{transform: rotate(-180deg);}
  259. }
  260. }
  261. .uni-collapse-item {
  262. /* #ifndef APP-NVUE */
  263. box-sizing: border-box;
  264. /* #endif */
  265. &__title {
  266. /* #ifndef APP-NVUE */
  267. display: flex;
  268. width: 100%;
  269. box-sizing: border-box;
  270. /* #endif */
  271. flex-direction: row;
  272. align-items: center;
  273. transition: border-bottom-color .3s;
  274. // transition-property: border-bottom-color;
  275. // transition-duration: 5s;
  276. &-wrap {
  277. width: 100%;
  278. flex: 1;
  279. }
  280. &-box {
  281. padding: 0 15px;
  282. /* #ifndef APP-NVUE */
  283. display: flex;
  284. width: 100%;
  285. box-sizing: border-box;
  286. /* #endif */
  287. flex-direction: row;
  288. justify-content: space-between;
  289. align-items: center;
  290. height: 48px;
  291. line-height: 48px;
  292. background-color: #fff;
  293. color: #303133;
  294. font-size: 13px;
  295. font-weight: 500;
  296. /* #ifdef H5 */
  297. cursor: pointer;
  298. outline: none;
  299. /* #endif */
  300. &.is-disabled {
  301. .uni-collapse-item__title-text {
  302. color: #999;
  303. }
  304. }
  305. }
  306. &.uni-collapse-item-border {
  307. border-bottom: 1px solid #ebeef5;
  308. }
  309. &.is-open {
  310. border-bottom-color: transparent;
  311. }
  312. &-img {
  313. height: 22px;
  314. width: 22px;
  315. margin-right: 10px;
  316. }
  317. &-text {
  318. flex: 1;
  319. font-size: 14px;
  320. /* #ifndef APP-NVUE */
  321. white-space: nowrap;
  322. color: inherit;
  323. /* #endif */
  324. /* #ifdef APP-NVUE */
  325. lines: 1;
  326. /* #endif */
  327. overflow: hidden;
  328. text-overflow: ellipsis;
  329. }
  330. &-arrow {
  331. /* #ifndef APP-NVUE */
  332. display: flex;
  333. box-sizing: border-box;
  334. /* #endif */
  335. align-items: center;
  336. justify-content: center;
  337. width: 20px;
  338. height: 20px;
  339. margin-right: 10px;
  340. transform: rotate(0deg);
  341. &-active {
  342. transform: rotate(-180deg);
  343. }
  344. }
  345. }
  346. &__wrap {
  347. /* #ifndef APP-NVUE */
  348. will-change: height;
  349. box-sizing: border-box;
  350. /* #endif */
  351. background-color: #fff;
  352. overflow: hidden;
  353. position: relative;
  354. height: 0;
  355. &.is--transition {
  356. // transition: all 0.3s;
  357. transition-property: height, border-bottom-width;
  358. transition-duration: 0.3s;
  359. /* #ifndef APP-NVUE */
  360. will-change: height;
  361. /* #endif */
  362. }
  363. &-content {
  364. position: absolute;
  365. font-size: 13px;
  366. color: #303133;
  367. // transition: height 0.3s;
  368. border-bottom-color: transparent;
  369. border-bottom-style: solid;
  370. border-bottom-width: 0;
  371. &.uni-collapse-item--border {
  372. border-bottom-width: 1px;
  373. border-bottom-color: red;
  374. border-bottom-color: #ebeef5;
  375. }
  376. &.open {
  377. position: relative;
  378. }
  379. }
  380. }
  381. &--animation {
  382. transition-property: transform;
  383. transition-duration: 0.3s;
  384. transition-timing-function: ease;
  385. }
  386. }
  387. </style>