TabPane.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { PureComponent, createRef, ReactNode } from 'react';
  2. import PropTypes from 'prop-types';
  3. import cls from 'classnames';
  4. import { cssClasses } from '@douyinfe/semi-foundation/tabs/constants';
  5. import getDataAttr from '@douyinfe/semi-foundation/utils/getDataAttr';
  6. import TabsContext from './tabs-context';
  7. import TabPaneTransition from './TabPaneTransition';
  8. import { TabPaneProps, PlainTab } from './interface';
  9. class TabPane extends PureComponent<TabPaneProps> {
  10. static isTabPane = true;
  11. static contextType = TabsContext;
  12. static propTypes = {
  13. className: PropTypes.string,
  14. style: PropTypes.object,
  15. children: PropTypes.node,
  16. disabled: PropTypes.bool,
  17. itemKey: PropTypes.string,
  18. tab: PropTypes.node,
  19. icon: PropTypes.node,
  20. };
  21. lastActiveKey: string = null;
  22. ref = createRef<HTMLDivElement>();
  23. isAnimating: boolean;
  24. _active: boolean;
  25. componentDidMount(): void {
  26. this.lastActiveKey = this.context.activeKey;
  27. }
  28. // get direction from current item key to activeKey
  29. getDirection = (activeKey: string, itemKey: string, panes: Array<PlainTab>): boolean => {
  30. if (itemKey !== null && activeKey !== null && Array.isArray(panes) && panes.length) {
  31. const activeIndex = panes.findIndex(pane => pane.itemKey === activeKey);
  32. const itemIndex = panes.findIndex(pane => pane.itemKey === itemKey);
  33. const lastActiveIndex = panes.findIndex(pane => pane.itemKey === this.lastActiveKey);
  34. this.lastActiveKey = activeKey;
  35. if (activeIndex === itemIndex) {
  36. return lastActiveIndex > activeIndex;
  37. } else {
  38. return itemIndex < activeIndex;
  39. }
  40. }
  41. return false;
  42. };
  43. hideScroll = (): void => {
  44. if (this.ref && this.ref.current) {
  45. this.ref.current.style.overflow = 'hidden';
  46. this.isAnimating = true;
  47. }
  48. };
  49. autoScroll = (): void => {
  50. if (this.ref && this.ref.current) {
  51. this.ref.current.style.overflow = '';
  52. this.isAnimating = false;
  53. }
  54. };
  55. shouldRender = (): boolean => {
  56. const { itemKey } = this.props;
  57. const { activeKey, lazyRender } = this.context;
  58. const active = activeKey === itemKey;
  59. this._active = this._active || active;
  60. return lazyRender ? this._active : true;
  61. };
  62. render(): ReactNode {
  63. const { tabPaneMotion: motion, tabPosition } = this.context;
  64. const { className, style, children, itemKey, ...restProps } = this.props;
  65. const active = this.context.activeKey === itemKey;
  66. const classNames = cls(className, {
  67. [cssClasses.TABS_PANE_INACTIVE]: !active,
  68. [cssClasses.TABS_PANE_ACTIVE]: active,
  69. [cssClasses.TABS_PANE]: true,
  70. });
  71. const shouldRender = this.shouldRender();
  72. return (
  73. <div
  74. ref={this.ref}
  75. role="tab-panel"
  76. className={classNames}
  77. style={style}
  78. aria-hidden={active ? 'false' : 'true'}
  79. {...getDataAttr(restProps)}
  80. >
  81. {motion ? (
  82. <TabPaneTransition
  83. direction={this.getDirection(this.context.activeKey, itemKey, this.context.panes)}
  84. motion={motion}
  85. mode={tabPosition === 'top' ? 'horizontal' : 'vertical'}
  86. state={active ? 'enter' : 'leave'}
  87. >
  88. {(transitionStyle): ReactNode => (
  89. <div className={`${cssClasses.TABS_PANE_MOTION_OVERLAY}`} style={{ ...transitionStyle }}>
  90. {shouldRender ? children : null}
  91. </div>
  92. )}
  93. </TabPaneTransition>
  94. ) : (
  95. shouldRender ? children : null
  96. )}
  97. </div>
  98. );
  99. }
  100. }
  101. export default TabPane;