index.tsx 7.0 KB

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