index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import React from 'react';
  2. import type {
  3. CollapsibleAdapter,
  4. CollapsibleFoundationProps,
  5. CollapsibleFoundationState
  6. } from "@douyinfe/semi-foundation/collapsible/foundation";
  7. import CollapsibleFoundation from "@douyinfe/semi-foundation/collapsible/foundation";
  8. import BaseComponent from "../_base/baseComponent";
  9. import PropTypes from "prop-types";
  10. import cls from "classnames";
  11. import { cssClasses } from "@douyinfe/semi-foundation/collapsible/constants";
  12. import { isEqual } from "lodash";
  13. import "@douyinfe/semi-foundation/collapsible/collapsible.scss";
  14. interface CollapsibleProps extends CollapsibleFoundationProps {
  15. motion?: boolean;
  16. children?: React.ReactNode;
  17. isOpen?: boolean;
  18. duration?: number;
  19. keepDOM?: boolean;
  20. className?: string;
  21. style?: React.CSSProperties;
  22. collapseHeight?: number;
  23. reCalcKey?: number | string;
  24. id?: string;
  25. onMotionEnd?: () => void
  26. }
  27. interface CollapsibleState extends CollapsibleFoundationState {
  28. domInRenderTree: boolean;
  29. domHeight: number;
  30. visible: boolean;
  31. isTransitioning: boolean
  32. }
  33. class Collapsible extends BaseComponent<CollapsibleProps, CollapsibleState> {
  34. static defaultProps = {
  35. isOpen: false,
  36. duration: 250,
  37. motion: true,
  38. keepDOM: false,
  39. collapseHeight: 0,
  40. fade: false
  41. };
  42. public foundation: CollapsibleFoundation;
  43. private domRef = React.createRef<HTMLDivElement>();
  44. private resizeObserver: ResizeObserver | null;
  45. constructor(props: CollapsibleProps) {
  46. super(props);
  47. this.state = {
  48. domInRenderTree: false,
  49. domHeight: 0,
  50. visible: this.props.isOpen,
  51. isTransitioning: false
  52. };
  53. this.foundation = new CollapsibleFoundation(this.adapter);
  54. }
  55. get adapter(): CollapsibleAdapter<CollapsibleProps, CollapsibleState> {
  56. return {
  57. ...super.adapter,
  58. setDOMInRenderTree: (domInRenderTree) => {
  59. if (this.state.domInRenderTree !== domInRenderTree) {
  60. this.setState({ domInRenderTree });
  61. }
  62. },
  63. setDOMHeight: (domHeight) => {
  64. if (this.state.domHeight !== domHeight) {
  65. this.setState({ domHeight });
  66. }
  67. },
  68. setVisible: (visible) => {
  69. if (this.state.visible !== visible) {
  70. this.setState({ visible });
  71. }
  72. },
  73. setIsTransitioning: (isTransitioning) => {
  74. if (this.state.isTransitioning !== isTransitioning) {
  75. this.setState({ isTransitioning });
  76. }
  77. }
  78. };
  79. }
  80. static getEntryInfo = (entry: ResizeObserverEntry) => {
  81. //judge whether parent or self display none
  82. let inRenderTree: boolean;
  83. if (entry.borderBoxSize) {
  84. inRenderTree = !(entry.borderBoxSize[0].blockSize === 0 && entry.borderBoxSize[0].inlineSize === 0);
  85. } else {
  86. inRenderTree = !(entry.contentRect.height === 0 && entry.contentRect.width === 0);
  87. }
  88. let height = 0;
  89. if (entry.borderBoxSize) {
  90. height = Math.ceil(entry.borderBoxSize[0].blockSize);
  91. } else {
  92. const target = entry.target as HTMLElement;
  93. height = target.clientHeight;
  94. }
  95. return {
  96. isShown: inRenderTree, height
  97. };
  98. }
  99. componentDidMount() {
  100. super.componentDidMount();
  101. this.resizeObserver = new ResizeObserver(this.handleResize);
  102. this.resizeObserver.observe(this.domRef.current);
  103. const domInRenderTree = this.isChildrenInRenderTree();
  104. this.foundation.updateDOMInRenderTree(domInRenderTree);
  105. if (domInRenderTree) {
  106. this.foundation.updateDOMHeight(this.domRef.current.scrollHeight);
  107. }
  108. }
  109. componentDidUpdate(prevProps: Readonly<CollapsibleProps>, prevState: Readonly<CollapsibleState>, snapshot?: any) {
  110. const changedPropKeys = Object.keys(this.props).filter(key => !isEqual(this.props[key], prevProps[key]));
  111. const changedStateKeys = Object.keys(this.state).filter(key => !isEqual(this.state[key], prevState[key]));
  112. if (changedPropKeys.includes("reCalcKey")) {
  113. this.foundation.updateDOMHeight(this.domRef.current.scrollHeight);
  114. }
  115. if (changedStateKeys.includes("domInRenderTree") && this.state.domInRenderTree) {
  116. this.foundation.updateDOMHeight(this.domRef.current.scrollHeight);
  117. }
  118. if (changedPropKeys.includes("isOpen")) {
  119. if (this.props.isOpen || !this.props.motion) {
  120. this.foundation.updateVisible(this.props.isOpen);
  121. }
  122. }
  123. if (this.props.motion && (prevProps.isOpen !== this.props.isOpen)) {
  124. this.foundation.updateIsTransitioning(true);
  125. }
  126. }
  127. componentWillUnmount() {
  128. super.componentWillUnmount();
  129. this.resizeObserver.disconnect();
  130. }
  131. handleResize = (entryList: ResizeObserverEntry[]) => {
  132. const entry = entryList[0];
  133. if (entry) {
  134. const entryInfo = Collapsible.getEntryInfo(entry);
  135. this.foundation.updateDOMHeight(entryInfo.height);
  136. this.foundation.updateDOMInRenderTree(entryInfo.isShown);
  137. }
  138. }
  139. isChildrenInRenderTree = () => {
  140. if (this.domRef.current) {
  141. return this.domRef.current.offsetHeight > 0;
  142. } else {
  143. return false;
  144. }
  145. }
  146. render() {
  147. const wrapperStyle: React.CSSProperties = {
  148. overflow: 'hidden',
  149. height: this.props.isOpen ? this.state.domHeight : this.props.collapseHeight,
  150. opacity: (this.props.isOpen || !this.props.fade || this.props.collapseHeight !== 0) ? 1 : 0,
  151. transitionDuration: `${this.props.motion && this.state.isTransitioning ? this.props.duration : 0}ms`,
  152. ...this.props.style
  153. };
  154. const wrapperCls = cls(`${cssClasses.PREFIX}-wrapper`, {
  155. [`${cssClasses.PREFIX}-transition`]: this.props.motion && this.state.isTransitioning
  156. }, this.props.className);
  157. return <div className={wrapperCls} style={wrapperStyle} onTransitionEnd={() => {
  158. if (!this.props.isOpen) {
  159. this.foundation.updateVisible(false);
  160. }
  161. this.foundation.updateIsTransitioning(false);
  162. this.props.onMotionEnd?.();
  163. }}>
  164. <div
  165. x-semi-prop="children"
  166. ref={this.domRef}
  167. style={{ overflow: 'hidden' }}
  168. id={this.props.id}
  169. >
  170. {
  171. (this.props.keepDOM || this.props.collapseHeight !== 0 || this.state.visible || this.props.isOpen) && this.props.children
  172. }
  173. </div>
  174. </div>;
  175. }
  176. }
  177. Collapsible.propTypes = {
  178. motion: PropTypes.bool,
  179. children: PropTypes.node,
  180. isOpen: PropTypes.bool,
  181. duration: PropTypes.number,
  182. keepDOM: PropTypes.bool,
  183. collapseHeight: PropTypes.number,
  184. style: PropTypes.object,
  185. className: PropTypes.string,
  186. reCalcKey: PropTypes.oneOfType([
  187. PropTypes.string,
  188. PropTypes.number
  189. ]),
  190. };
  191. export default Collapsible;