index.tsx 7.1 KB

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