TabPane.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React, {createRef, PureComponent, 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 {TabContextValue} from './interface';
  8. import {PlainTab, TabPaneProps} from './interface';
  9. import CSSAnimation from "../_cssAnimation";
  10. class TabPane extends PureComponent<TabPaneProps> {
  11. static isTabPane = true;
  12. static contextType = TabsContext;
  13. static propTypes = {
  14. className: PropTypes.string,
  15. style: PropTypes.object,
  16. children: PropTypes.node,
  17. disabled: PropTypes.bool,
  18. itemKey: PropTypes.string,
  19. tab: PropTypes.node,
  20. icon: PropTypes.node,
  21. closable: PropTypes.bool
  22. };
  23. lastActiveKey: string = null;
  24. ref = createRef<HTMLDivElement>();
  25. _active: boolean;
  26. context: TabContextValue;
  27. firstRender: boolean = true;
  28. componentDidMount(): void {
  29. this.lastActiveKey = this.context.activeKey;
  30. }
  31. // get direction from current item key to activeKey
  32. getDirection = (activeKey: string, itemKey: string, panes: Array<PlainTab>): boolean => {
  33. if (itemKey !== null && activeKey !== null && Array.isArray(panes) && panes.length) {
  34. const activeIndex = panes.findIndex(pane => pane.itemKey === activeKey);
  35. const itemIndex = panes.findIndex(pane => pane.itemKey === itemKey);
  36. const lastActiveIndex = panes.findIndex(pane => pane.itemKey === this.lastActiveKey);
  37. this.lastActiveKey = activeKey;
  38. if (activeIndex === itemIndex) {
  39. return lastActiveIndex > activeIndex;
  40. } else {
  41. return itemIndex < activeIndex;
  42. }
  43. }
  44. return false;
  45. };
  46. shouldRender = (): boolean => {
  47. const {itemKey} = this.props;
  48. const {activeKey, lazyRender} = this.context;
  49. const active = activeKey === itemKey;
  50. this._active = this._active || active;
  51. return lazyRender ? this._active : true;
  52. };
  53. render(): ReactNode {
  54. const {tabPaneMotion: motion, tabPosition,isFirstRender} = this.context;
  55. const {className, style, children, itemKey, ...restProps} = this.props;
  56. const active = this.context.activeKey === itemKey;
  57. const classNames = cls(className, {
  58. [cssClasses.TABS_PANE_INACTIVE]: !active,
  59. [cssClasses.TABS_PANE_ACTIVE]: active,
  60. [cssClasses.TABS_PANE]: true,
  61. });
  62. const shouldRender = this.shouldRender();
  63. return (
  64. <div
  65. ref={this.ref}
  66. role="tabpanel"
  67. id={`semiTabPanel${itemKey}`}
  68. aria-labelledby={`semiTab${itemKey}`}
  69. className={classNames}
  70. style={style}
  71. aria-hidden={active ? 'false' : 'true'}
  72. tabIndex={0}
  73. {...getDataAttr(restProps)}
  74. x-semi-prop="children"
  75. >
  76. <CSSAnimation motion={motion && active && !isFirstRender} animationState={active ? "enter" : "leave"}
  77. startClassName={(() => {
  78. const direction = this.getDirection(this.context.activeKey, itemKey, this.context.panes);
  79. if (tabPosition === 'top') {
  80. if (direction) {
  81. return cssClasses.TABS_PANE_ANIMATE_RIGHT_SHOW
  82. } else {
  83. return cssClasses.TABS_PANE_ANIMATE_LEFT_SHOW
  84. }
  85. } else {
  86. if (direction) {
  87. return cssClasses.TABS_PANE_ANIMATE_BOTTOM_SHOW
  88. } else {
  89. return cssClasses.TABS_PANE_ANIMATE_TOP_SHOW
  90. }
  91. }
  92. })()}>
  93. {
  94. ({animationClassName, animationEventsNeedBind}) => {
  95. return <div
  96. className={`${cssClasses.TABS_PANE_MOTION_OVERLAY} ${animationClassName}`}
  97. x-semi-prop="children"
  98. {...animationEventsNeedBind}
  99. >
  100. {shouldRender ? children : null}
  101. </div>
  102. }
  103. }
  104. </CSSAnimation>
  105. </div>
  106. );
  107. }
  108. }
  109. export default TabPane;