uni-collapse-item.vue 8.9 KB

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