@@ -6,26 +6,42 @@ import {HtmlContent} from './html-content';
66import { CustomContent } from './custom-content' ;
77import { classes } from '../../helpers' ;
88
9+ /**
10+ * 懒加载内容组件的状态
11+ */
912export type LazyContentState = {
13+ /** 是否正在加载 */
1014 loading ?: boolean ;
15+ /** 加载出错时的错误对象 */
1116 error ?: Error ;
17+ /** 加载到的内容 */
1218 content ?: CustomContentType ;
1319} ;
1420
21+ /**
22+ * 懒加载内容组件
23+ * 用于异步加载并展示内容,支持 HTML、文本或自定义组件渲染
24+ */
1525export class LazyContent extends Component < LazyContentProps , LazyContentState > {
26+ /** 默认属性 */
1627 static defaultProps : Partial < LazyContentProps > = {
1728 type : 'html' ,
1829 loadingIndicator : true ,
1930 loadingClass : 'loading' ,
2031 clearBeforeLoad : true ,
2132 } ;
2233
34+ /** 组件内部状态 */
2335 state : LazyContentState = { } ;
2436
2537 protected _ref = createRef < HTMLDivElement > ( ) ;
2638
2739 protected _ajax ?: Ajax ;
2840
41+ /**
42+ * 加载内容
43+ * @param newFetcher 可选的新的获取器设置,如果提供将使用此设置进行加载,否则使用 props 中的 fetcher
44+ */
2945 async load ( newFetcher ?: FetcherSetting ) {
3046 const { props} = this ;
3147 const { fetcher, type, fetcherArgs, fetcherThis = this , clearBeforeLoad} = props ;
@@ -41,6 +57,9 @@ export class LazyContent extends Component<LazyContentProps, LazyContentState> {
4157 this . _ajax = undefined ;
4258 }
4359
60+ /**
61+ * 组件挂载后触发加载,并监听 `loadContent.zui` 事件
62+ */
4463 componentDidMount ( ) : void {
4564 this . load ( ) ;
4665 $ ( this . _ref . current ) . on ( 'loadContent.zui' , ( event : Event , fetcher ?: FetcherSetting ) => {
@@ -49,12 +68,19 @@ export class LazyContent extends Component<LazyContentProps, LazyContentState> {
4968 } ) ;
5069 }
5170
71+ /**
72+ * 组件更新时检查 fetcher 相关属性是否变化,若变化则重新加载
73+ * @param previousProps 上一次渲染的属性
74+ */
5275 componentDidUpdate ( previousProps : Readonly < LazyContentProps > ) : void {
5376 if ( this . props . fetcher !== previousProps . fetcher || this . props . fetcherArgs !== previousProps . fetcherArgs || this . props . fetcherThis !== previousProps . fetcherThis ) {
5477 this . load ( ) ;
5578 }
5679 }
5780
81+ /**
82+ * 组件卸载时中止未完成的请求并移除事件监听
83+ */
5884 componentWillUnmount ( ) : void {
5985 this . _ajax ?. abort ( ) ;
6086 $ ( this . _ref . current ) . off ( '.zui' ) ;
@@ -78,6 +104,10 @@ export class LazyContent extends Component<LazyContentProps, LazyContentState> {
78104 return < CustomContent content = { content } { ...otherProps } /> ;
79105 }
80106
107+ /**
108+ * 渲染组件结构
109+ * @param props 组件属性
110+ */
81111 render ( props : LazyContentProps ) {
82112 const { loading} = this . state ;
83113 const { id, loadingClass, loadingIndicator, className, style, attrs, loadingText, ...others } = props ;
0 commit comments