You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

4 months ago
  1. <template>
  2. <view class="u-collapse">
  3. <u-line v-if="border"></u-line>
  4. <slot />
  5. </view>
  6. </template>
  7. <script>
  8. import { props } from './props';
  9. import { mpMixin } from '../../libs/mixin/mpMixin';
  10. import { mixin } from '../../libs/mixin/mixin';
  11. /**
  12. * collapse 折叠面板
  13. * @description 通过折叠面板收纳内容区域
  14. * @tutorial https://ijry.github.io/uview-plus/components/collapse.html
  15. * @property {String | Number | Array} value 当前展开面板的name非手风琴模式[<string | number>]手风琴模式string | number
  16. * @property {Boolean} accordion 是否手风琴模式 默认 false
  17. * @property {Boolean} border 是否显示外边框 ( 默认 true
  18. * @event {Function} change 当前激活面板展开时触发(如果是手风琴模式参数activeNames类型为String否则为Array)
  19. * @example <u-collapse></u-collapse>
  20. */
  21. export default {
  22. name: "u-collapse",
  23. mixins: [mpMixin, mixin,props],
  24. watch: {
  25. needInit() {
  26. this.init()
  27. },
  28. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  29. parentData() {
  30. if (this.children.length) {
  31. this.children.map(child => {
  32. // 判断子组件(u-checkbox)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  33. typeof(child.updateParentData) === 'function' && child.updateParentData()
  34. })
  35. }
  36. }
  37. },
  38. created() {
  39. this.children = []
  40. },
  41. computed: {
  42. needInit() {
  43. // 通过computed,同时监听accordion和value值的变化
  44. // 再通过watch去执行init()方法,进行再一次的初始化
  45. return [this.accordion, this.value]
  46. }
  47. },
  48. emits: ["open", "close", "change"],
  49. methods: {
  50. // 重新初始化一次内部的所有子元素
  51. init() {
  52. this.children.map(child => {
  53. child.init()
  54. })
  55. },
  56. /**
  57. * collapse-item被点击时触发由collapse统一处理各子组件的状态
  58. * @param {Object} target 被操作的面板的实例
  59. */
  60. onChange(target) {
  61. let changeArr = []
  62. this.children.map((child, index) => {
  63. // 如果是手风琴模式,将其他的折叠面板收起来
  64. if (this.accordion) {
  65. child.expanded = child === target ? !target.expanded : false
  66. child.setContentAnimate()
  67. } else {
  68. if(child === target) {
  69. child.expanded = !child.expanded
  70. child.setContentAnimate()
  71. }
  72. }
  73. // 拼接change事件中,数组元素的状态
  74. changeArr.push({
  75. // 如果没有定义name属性,则默认返回组件的index索引
  76. name: child.name || index,
  77. status: child.expanded ? 'open' : 'close'
  78. })
  79. })
  80. this.$emit('change', changeArr)
  81. this.$emit(target.expanded ? 'open' : 'close', target.name)
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. @import "../../libs/css/components.scss";
  88. </style>