uni-collapse-item.vue 8.8 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 {Boolean} border = [true|false] 是否显示分隔线
  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. // #ifdef APP-NVUE
  207. this.height = data.height + 1
  208. // #endif
  209. // #ifndef APP-NVUE
  210. this.height = data.height
  211. // #endif
  212. this.isheight = true
  213. if (type) return
  214. this.onClick(this.isOpen, 'init')
  215. })
  216. .exec()
  217. },
  218. getNvueHwight(type) {
  219. const result = dom.getComponentRect(this.$refs['collapse--hook'], option => {
  220. if (option && option.result && option.size) {
  221. // #ifdef APP-NVUE
  222. this.height = option.size.height + 1
  223. // #endif
  224. // #ifndef APP-NVUE
  225. this.height = option.size.height
  226. // #endif
  227. this.isheight = true
  228. if (type) return
  229. this.onClick(this.open, 'init')
  230. }
  231. })
  232. },
  233. /**
  234. * 获取父元素实例
  235. */
  236. getCollapse(name = 'uniCollapse') {
  237. let parent = this.$parent;
  238. let parentName = parent.$options.name;
  239. while (parentName !== name) {
  240. parent = parent.$parent;
  241. if (!parent) return false;
  242. parentName = parent.$options.name;
  243. }
  244. return parent;
  245. }
  246. }
  247. }
  248. </script>
  249. <style lang="scss">
  250. .uni-collapse-item {
  251. /* #ifndef APP-NVUE */
  252. box-sizing: border-box;
  253. /* #endif */
  254. &__title {
  255. /* #ifndef APP-NVUE */
  256. display: flex;
  257. width: 100%;
  258. box-sizing: border-box;
  259. /* #endif */
  260. flex-direction: row;
  261. align-items: center;
  262. transition: border-bottom-color .3s;
  263. // transition-property: border-bottom-color;
  264. // transition-duration: 5s;
  265. &-wrap {
  266. width: 100%;
  267. flex: 1;
  268. }
  269. &-box {
  270. padding: 0 15px;
  271. /* #ifndef APP-NVUE */
  272. display: flex;
  273. width: 100%;
  274. box-sizing: border-box;
  275. /* #endif */
  276. flex-direction: row;
  277. justify-content: space-between;
  278. align-items: center;
  279. height: 48px;
  280. line-height: 48px;
  281. background-color: #fff;
  282. color: #303133;
  283. font-size: 13px;
  284. font-weight: 500;
  285. /* #ifdef H5 */
  286. cursor: pointer;
  287. outline: none;
  288. /* #endif */
  289. &.is-disabled {
  290. .uni-collapse-item__title-text {
  291. color: #999;
  292. }
  293. }
  294. }
  295. &.uni-collapse-item-border {
  296. border-bottom: 1px solid #ebeef5;
  297. }
  298. &.is-open {
  299. border-bottom-color: transparent;
  300. }
  301. &-img {
  302. height: 22px;
  303. width: 22px;
  304. margin-right: 10px;
  305. }
  306. &-text {
  307. flex: 1;
  308. font-size: 14px;
  309. /* #ifndef APP-NVUE */
  310. white-space: nowrap;
  311. color: inherit;
  312. /* #endif */
  313. /* #ifdef APP-NVUE */
  314. lines: 1;
  315. /* #endif */
  316. overflow: hidden;
  317. text-overflow: ellipsis;
  318. }
  319. &-arrow {
  320. /* #ifndef APP-NVUE */
  321. display: flex;
  322. box-sizing: border-box;
  323. /* #endif */
  324. align-items: center;
  325. justify-content: center;
  326. width: 20px;
  327. height: 20px;
  328. margin-right: 10px;
  329. transform: rotate(0deg);
  330. &-active {
  331. transform: rotate(-180deg);
  332. }
  333. }
  334. }
  335. &__wrap {
  336. /* #ifndef APP-NVUE */
  337. will-change: height;
  338. box-sizing: border-box;
  339. /* #endif */
  340. background-color: #fff;
  341. overflow: hidden;
  342. position: relative;
  343. height: 0;
  344. &.is--transition {
  345. // transition: all 0.3s;
  346. transition-property: height, border-bottom-width;
  347. transition-duration: 0.3s;
  348. /* #ifndef APP-NVUE */
  349. will-change: height;
  350. /* #endif */
  351. }
  352. &-content {
  353. position: absolute;
  354. font-size: 13px;
  355. color: #303133;
  356. // transition: height 0.3s;
  357. border-bottom-color: transparent;
  358. border-bottom-style: solid;
  359. border-bottom-width: 0;
  360. &.uni-collapse-item--border {
  361. border-bottom-width: 1px;
  362. border-bottom-color: red;
  363. border-bottom-color: #ebeef5;
  364. }
  365. &.open {
  366. position: relative;
  367. }
  368. }
  369. }
  370. &--animation {
  371. transition-property: transform;
  372. transition-duration: 0.3s;
  373. transition-timing-function: ease;
  374. }
  375. }
  376. </style>