index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. const getDefaultActiveKey = (elements) => {
  4. const target = elements.filter((element) => !element.data.disabled)[0]
  5. if (target) {
  6. return target.data.key
  7. }
  8. return null
  9. }
  10. const activeKeyIsValid = (elements, key) => {
  11. return elements.map((element) => element.data.key).includes(key)
  12. }
  13. const getActiveKey = (elements, activeKey) => {
  14. const defaultActiveKey = getDefaultActiveKey(elements)
  15. return !activeKey ? defaultActiveKey : !activeKeyIsValid(elements, activeKey) ? defaultActiveKey : activeKey
  16. }
  17. baseComponent({
  18. relations: {
  19. '../tab/index': {
  20. type: 'child',
  21. observer() {
  22. this.debounce(this.changeCurrent)
  23. },
  24. },
  25. },
  26. properties: {
  27. prefixCls: {
  28. type: String,
  29. value: 'wux-tabs',
  30. },
  31. defaultCurrent: {
  32. type: String,
  33. value: '',
  34. },
  35. current: {
  36. type: String,
  37. value: '',
  38. observer: 'changeCurrent',
  39. },
  40. scroll: {
  41. type: Boolean,
  42. value: false,
  43. },
  44. controlled: {
  45. type: Boolean,
  46. value: false,
  47. },
  48. theme: {
  49. type: String,
  50. value: 'balanced',
  51. },
  52. direction: {
  53. type: String,
  54. value: 'horizontal',
  55. },
  56. },
  57. data: {
  58. activeKey: '',
  59. keys: [],
  60. },
  61. computed: {
  62. classes() {
  63. const { prefixCls, direction, scroll } = this.data
  64. const wrap = classNames(prefixCls, {
  65. [`${prefixCls}--${direction}`]: direction,
  66. [`${prefixCls}--scroll`]: scroll,
  67. })
  68. return {
  69. wrap,
  70. }
  71. },
  72. },
  73. methods: {
  74. updated(value, condition) {
  75. const elements = this.getRelationNodes('../tab/index')
  76. const activeKey = getActiveKey(elements, value)
  77. const { scroll, theme, direction } = this.data
  78. if (elements.length > 0) {
  79. if (condition) {
  80. this.setData({
  81. activeKey,
  82. })
  83. elements.forEach((element) => {
  84. element.changeCurrent({
  85. current: element.data.key === activeKey,
  86. scroll,
  87. theme,
  88. direction,
  89. })
  90. })
  91. }
  92. }
  93. if (this.data.keys.length !== elements.length) {
  94. this.setData({
  95. keys: elements.map((element) => element.data)
  96. })
  97. }
  98. },
  99. changeCurrent(value = this.data.current) {
  100. this.updated(value, this.data.controlled)
  101. },
  102. emitEvent(key) {
  103. this.triggerEvent('change', {
  104. key,
  105. keys: this.data.keys,
  106. })
  107. },
  108. setActiveKey(activeKey) {
  109. if (this.data.activeKey !== activeKey) {
  110. this.updated(activeKey, !this.data.controlled)
  111. }
  112. this.emitEvent(activeKey)
  113. },
  114. },
  115. ready() {
  116. const { defaultCurrent, current, controlled } = this.data
  117. const activeKey = controlled ? current : defaultCurrent
  118. this.updated(activeKey, true)
  119. },
  120. })