Skip to content

Commit

Permalink
feat(StyleLoadFinish): 子元素支持传递方法
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 18, 2024
1 parent 7afbd1a commit c7c567e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/components/StyleLoadFinish/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import React, { useRef, useEffect, useState } from 'react';
import { isFunction } from '@pansy/shared';
import { useMap } from '../../hooks/useMap';

interface StyleLoadFinishProps {
children?: React.ReactNode;
children?: React.ReactNode | ((finish: boolean, firstTime: boolean) => React.ReactNode);
}

export const StyleLoadFinish: React.FC<StyleLoadFinishProps> = (props) => {
const { children } = props;
const { map } = useMap();
const [, setStyleLoaded] = useState(0);
const themeStatus = useRef([0, 0, 0]);
const checkStylePassRef = useRef(0);

const forceUpdate = () => {
setStyleLoaded((version) => version + 1);
};

const checkStyle = () => {
return ['131', '031'].includes(themeStatus.current.join(''));
const result = ['031', '131'].includes(themeStatus.current.join(''));

if (result) {
checkStylePassRef.current = checkStylePassRef.current + 1;
}

return result;
};

const handleStyleLoading = () => {
Expand Down Expand Up @@ -51,5 +60,9 @@ export const StyleLoadFinish: React.FC<StyleLoadFinishProps> = (props) => {
return undefined;
}, [map]);

return checkStyle() && props.children;
if (isFunction(children)) {
return children(checkStyle(), checkStylePassRef.current === 2);
}

return checkStyle() && (props.children as React.ReactNode);
};

0 comments on commit c7c567e

Please sign in to comment.