From 7155dfe07f3c4ce4547cb12abfcf57867aa0d40b Mon Sep 17 00:00:00 2001 From: sunhao Date: Thu, 31 Oct 2024 20:47:25 +0800 Subject: [PATCH] * core: add more options to lazy content. --- .../src/react/components/lazy-content.tsx | 29 +++++++++++++++---- .../src/react/types/lazy-content-props.ts | 8 +++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/core/src/react/components/lazy-content.tsx b/lib/core/src/react/components/lazy-content.tsx index 0aa0a8b9dd..9f6e6a2a17 100644 --- a/lib/core/src/react/components/lazy-content.tsx +++ b/lib/core/src/react/components/lazy-content.tsx @@ -3,6 +3,7 @@ import type {LazyContentProps, CustomContentType} from '../types'; import {fetchData, type Ajax} from '../../ajax'; import {HtmlContent} from './html-content'; import {CustomContent} from './custom-content'; +import {classes} from '../../helpers'; export type LazyContentState = { loading?: boolean; @@ -13,6 +14,8 @@ export type LazyContentState = { export class LazyContent extends Component { static defaultProps: Partial = { type: 'html', + loadingIndicator: true, + loadingClass: 'loading', }; state: LazyContentState = {}; @@ -27,7 +30,7 @@ export class LazyContent extends Component { const content = await fetchData(fetcher, fetcherArgs, {throws: true, dataType: type === 'custom' ? 'json' : 'text'}, fetcherThis, (ajax) => { this._ajax = ajax; }); - this.setState({content, loading: false}); + this.setState({content: content as CustomContentType, loading: false}); } catch (error) { this.setState({error: error as Error, loading: false}); } @@ -38,25 +41,41 @@ export class LazyContent extends Component { this.load(); } + componentDidUpdate(previousProps: Readonly): void { + if (this.props.fetcher !== previousProps.fetcher || this.props.fetcherArgs !== previousProps.fetcherArgs || this.props.fetcherThis !== previousProps.fetcherThis) { + this.load(); + } + } + componentWillUnmount(): void { this._ajax?.abort(); } - render(props: LazyContentProps) { + protected _renderContent(props: LazyContentProps) { const {loading, error, content = ''} = this.state; - const {loadingText, errorText, type, ...others} = props; + const {loadingContent, errorText, type} = props; if (loading) { - return loadingText; + return loadingContent; } if (error) { return errorText ?? error.message; } if (type === 'html') { - return ; + return ; } if (type === 'text') { return content; } return ; } + + render(props: LazyContentProps) { + const {loading} = this.state; + const {loadingClass, loadingIndicator, className, style, attrs, loadingText} = props; + return ( +
+ {this._renderContent(props)} +
+ ); + } } diff --git a/lib/core/src/react/types/lazy-content-props.ts b/lib/core/src/react/types/lazy-content-props.ts index ec6b14541a..16375a9ba0 100644 --- a/lib/core/src/react/types/lazy-content-props.ts +++ b/lib/core/src/react/types/lazy-content-props.ts @@ -1,7 +1,15 @@ +import type {JSX} from 'preact/jsx-runtime'; import type {FetcherSetting} from '../../ajax'; +import type {ClassNameLike} from '../../helpers'; import type {CustomContentType} from './custom-content-type'; export type LazyContentProps = { + className?: ClassNameLike; + style?: JSX.CSSProperties; + attrs?: Record; + loadingClass?: ClassNameLike; + loadingIndicator?: boolean; + loadingContent?: CustomContentType; fetcher: FetcherSetting; fetcherArgs: A; fetcherThis?: THIS;