uni-collapse-item.vue 8.8 KB

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