uni-collapse-item.vue 11 KB

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