uni-collapse-item.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. v-if="showArrow"
  16. :class="{ 'uni-collapse-item__title-arrow-active': isOpen, 'uni-collapse-item--animation': showAnimation === true }"
  17. class="uni-collapse-item__title-arrow">
  18. <uni-icons :color="disabled?'#ddd':'#bbb'" size="14" type="bottom" />
  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. },
  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.open,'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>