uni-collapse-item.vue 9.0 KB

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