uni-collapse-item.vue 8.9 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. <image class="wh-30" src="/static/img/information/right.png" mode=""></image>
  19. </view>
  20. </view>
  21. <view class="uni-collapse-item__wrap" :class="{'is--transition':showAnimation}"
  22. :style="{height: (isOpen?height:0) +'px'}">
  23. <view :id="elId" ref="collapse--hook" class="uni-collapse-item__wrap-content"
  24. :class="{open:isheight,'uni-collapse-item--border':border&&isOpen}">
  25. <slot></slot>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. // #ifdef APP-NVUE
  32. const dom = weex.requireModule('dom')
  33. // #endif
  34. /**
  35. * CollapseItem 折叠面板子组件
  36. * @description 折叠面板子组件
  37. * @property {String} title 标题文字
  38. * @property {String} thumb 标题左侧缩略图
  39. * @property {String} name 唯一标志符
  40. * @property {Boolean} open = [true|false] 是否展开组件
  41. * @property {Boolean} titleBorder = [true|false] 是否显示标题分隔线
  42. * @property {Boolean} border = [true|false] 是否显示分隔线
  43. * @property {Boolean} disabled = [true|false] 是否展开面板
  44. * @property {Boolean} showAnimation = [true|false] 开启动画
  45. * @property {Boolean} showArrow = [true|false] 是否显示右侧箭头
  46. */
  47. export default {
  48. name: 'uniCollapseItem',
  49. props: {
  50. // 列表标题
  51. title: {
  52. type: String,
  53. default: ''
  54. },
  55. name: {
  56. type: [Number, String],
  57. default: ''
  58. },
  59. // 是否禁用
  60. disabled: {
  61. type: Boolean,
  62. default: false
  63. },
  64. // #ifdef APP-PLUS
  65. // 是否显示动画,app 端默认不开启动画,卡顿严重
  66. showAnimation: {
  67. type: Boolean,
  68. default: false
  69. },
  70. // #endif
  71. // #ifndef APP-PLUS
  72. // 是否显示动画
  73. showAnimation: {
  74. type: Boolean,
  75. default: true
  76. },
  77. // #endif
  78. // 是否展开
  79. open: {
  80. type: Boolean,
  81. default: false
  82. },
  83. // 缩略图
  84. thumb: {
  85. type: String,
  86. default: ''
  87. },
  88. // 标题分隔线显示类型
  89. titleBorder: {
  90. type: String,
  91. default: 'auto'
  92. },
  93. border: {
  94. type: Boolean,
  95. default: true
  96. },
  97. showArrow: {
  98. type: Boolean,
  99. default: true
  100. }
  101. },
  102. data() {
  103. // TODO 随机生生元素ID,解决百度小程序获取同一个元素位置信息的bug
  104. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  105. return {
  106. isOpen: false,
  107. isheight: null,
  108. height: 0,
  109. elId,
  110. nameSync: 0
  111. }
  112. },
  113. watch: {
  114. open(val) {
  115. this.isOpen = val
  116. this.onClick(val, 'init')
  117. }
  118. },
  119. updated(e) {
  120. this.$nextTick(() => {
  121. this.init(true)
  122. })
  123. },
  124. created() {
  125. this.collapse = this.getCollapse()
  126. this.oldHeight = 0
  127. this.onClick(this.open, 'init')
  128. },
  129. // #ifndef VUE3
  130. // TODO vue2
  131. destroyed() {
  132. if (this.__isUnmounted) return
  133. this.uninstall()
  134. },
  135. // #endif
  136. // #ifdef VUE3
  137. // TODO vue3
  138. unmounted() {
  139. this.__isUnmounted = true
  140. this.uninstall()
  141. },
  142. // #endif
  143. mounted() {
  144. if (!this.collapse) return
  145. if (this.name !== '') {
  146. this.nameSync = this.name
  147. } else {
  148. this.nameSync = this.collapse.childrens.length + ''
  149. }
  150. if (this.collapse.names.indexOf(this.nameSync) === -1) {
  151. this.collapse.names.push(this.nameSync)
  152. } else {
  153. console.warn(`name 值 ${this.nameSync} 重复`);
  154. }
  155. if (this.collapse.childrens.indexOf(this) === -1) {
  156. this.collapse.childrens.push(this)
  157. }
  158. this.init()
  159. },
  160. methods: {
  161. init(type) {
  162. // #ifndef APP-NVUE
  163. this.getCollapseHeight(type)
  164. // #endif
  165. // #ifdef APP-NVUE
  166. this.getNvueHwight(type)
  167. // #endif
  168. },
  169. uninstall() {
  170. if (this.collapse) {
  171. this.collapse.childrens.forEach((item, index) => {
  172. if (item === this) {
  173. this.collapse.childrens.splice(index, 1)
  174. }
  175. })
  176. this.collapse.names.forEach((item, index) => {
  177. if (item === this.nameSync) {
  178. this.collapse.names.splice(index, 1)
  179. }
  180. })
  181. }
  182. },
  183. onClick(isOpen, type) {
  184. if (this.disabled) return
  185. this.isOpen = isOpen
  186. if (this.isOpen && this.collapse) {
  187. this.collapse.setAccordion(this)
  188. }
  189. if (type !== 'init') {
  190. this.collapse.onChange(isOpen, this)
  191. }
  192. },
  193. getCollapseHeight(type, index = 0) {
  194. const views = uni.createSelectorQuery().in(this)
  195. views
  196. .select(`#${this.elId}`)
  197. .fields({
  198. size: true
  199. }, data => {
  200. // TODO 百度中可能获取不到节点信息 ,需要循环获取
  201. if (index >= 10) return
  202. if (!data) {
  203. index++
  204. this.getCollapseHeight(false, index)
  205. return
  206. }
  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: #fff;
  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: #999;
  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(90deg);
  333. }
  334. }
  335. }
  336. &__wrap {
  337. /* #ifndef APP-NVUE */
  338. will-change: height;
  339. box-sizing: border-box;
  340. /* #endif */
  341. background-color: #fff;
  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>