index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { getDefaultContext } from '../helpers/shared/getDefaultContext'
  4. import { useRect } from '../helpers/hooks/useDOM'
  5. import { props as tabsProps } from '../tabs/props'
  6. const defaultContext = getDefaultContext(tabsProps, [
  7. 'scroll',
  8. 'theme',
  9. 'direction',
  10. 'activeLineMode',
  11. ])
  12. baseComponent({
  13. relations: {
  14. '../tabs/index': {
  15. type: 'parent',
  16. },
  17. },
  18. properties: {
  19. prefixCls: {
  20. type: String,
  21. value: 'wux-tabs__tab',
  22. },
  23. key: {
  24. type: String,
  25. value: '',
  26. },
  27. title: {
  28. type: String,
  29. value: '',
  30. },
  31. disabled: {
  32. type: Boolean,
  33. value: false,
  34. },
  35. },
  36. data: {
  37. current: false,
  38. context: defaultContext,
  39. },
  40. computed: {
  41. classes: ['prefixCls, disabled, current, context', function(prefixCls, disabled, current, context) {
  42. const { direction, scroll, theme, activeLineMode } = context
  43. const wrap = classNames(prefixCls, {
  44. [`${prefixCls}--${direction}`]: direction,
  45. [`${prefixCls}--${theme}`]: theme,
  46. [`${prefixCls}--scroll`]: scroll,
  47. [`${prefixCls}--current`]: current,
  48. [`${prefixCls}--disabled`]: disabled,
  49. })
  50. const title = `${prefixCls}-title`
  51. const bar = classNames(`${prefixCls}-bar`, {
  52. [`${prefixCls}-bar--${activeLineMode}`]: activeLineMode,
  53. })
  54. return {
  55. wrap,
  56. title,
  57. bar,
  58. }
  59. }],
  60. },
  61. methods: {
  62. activeTabRef() {
  63. return new Promise((resolve) => {
  64. const { prefixCls } = this.data
  65. useRect(`.${prefixCls}`, this)
  66. .then((activeTab) => {
  67. const activeTabLeft = activeTab.left
  68. const activeTabWidth = activeTab.width
  69. const activeTabTop = activeTab.top
  70. const activeTabHeight = activeTab.height
  71. resolve({
  72. activeTabLeft,
  73. activeTabWidth,
  74. activeTabTop,
  75. activeTabHeight,
  76. })
  77. })
  78. })
  79. },
  80. changeCurrent({ current, context = defaultContext }) {
  81. this.setData({
  82. current,
  83. context,
  84. })
  85. },
  86. onTap() {
  87. const { key, disabled } = this.data
  88. const parent = this.getRelationsByName('../tabs/index')[0]
  89. if (disabled || !parent) return
  90. this.triggerEvent('click', { key })
  91. parent.setActiveKey(key)
  92. },
  93. },
  94. })