index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import baseComponent from '../helpers/baseComponent'
  2. baseComponent({
  3. relations: {
  4. '../accordion/index': {
  5. type: 'child',
  6. observer() {
  7. this.debounce(this.changeCurrent)
  8. },
  9. },
  10. },
  11. properties: {
  12. prefixCls: {
  13. type: String,
  14. value: 'wux-accordion-group',
  15. },
  16. cellGroupPrefixCls: {
  17. type: String,
  18. value: 'wux-cell-group',
  19. },
  20. defaultCurrent: {
  21. type: Array,
  22. value: [],
  23. },
  24. current: {
  25. type: Array,
  26. value: [],
  27. observer: 'changeCurrent',
  28. },
  29. controlled: {
  30. type: Boolean,
  31. value: false,
  32. },
  33. accordion: {
  34. type: Boolean,
  35. value: false,
  36. },
  37. title: {
  38. type: String,
  39. value: '',
  40. },
  41. label: {
  42. type: String,
  43. value: '',
  44. },
  45. },
  46. data: {
  47. activeKey: '',
  48. keys: [],
  49. },
  50. methods: {
  51. updated(activeKey, condition) {
  52. const elements = this.getRelationNodes('../accordion/index')
  53. if (elements.length > 0) {
  54. if (condition) {
  55. this.setData({
  56. activeKey,
  57. })
  58. elements.forEach((element, index) => {
  59. const key = element.data.key || String(index)
  60. const current = this.data.accordion ? activeKey[0] === key : activeKey.indexOf(key) !== -1
  61. element.changeCurrent(current, key)
  62. })
  63. }
  64. }
  65. if (this.data.keys.length !== elements.length) {
  66. this.setData({
  67. keys: elements.map((element) => element.data)
  68. })
  69. }
  70. },
  71. changeCurrent(activeKey = this.data.current) {
  72. this.updated(activeKey, this.data.controlled)
  73. },
  74. emitEvent(key) {
  75. this.triggerEvent('change', {
  76. key,
  77. keys: this.data.keys,
  78. })
  79. },
  80. setActiveKey(activeKey) {
  81. if (this.data.activeKey !== activeKey) {
  82. this.updated(activeKey, !this.data.controlled)
  83. }
  84. this.emitEvent(this.data.accordion ? activeKey[0] : activeKey)
  85. },
  86. onClickItem(key) {
  87. let activeKey = [...this.data.activeKey]
  88. if (this.data.accordion) {
  89. activeKey = activeKey[0] === key ? [] : [key]
  90. } else {
  91. activeKey = activeKey.indexOf(key) !== -1 ? activeKey.filter((n) => n !== key) : [...activeKey, key]
  92. }
  93. this.setActiveKey(activeKey)
  94. },
  95. },
  96. ready() {
  97. const { defaultCurrent, current, controlled } = this.data
  98. const activeKey = controlled ? current : defaultCurrent
  99. this.updated(activeKey, true)
  100. },
  101. })