index.tsx 7.2 KB

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