fix(striker-ui): add loading switch to ExpandablePanel

main
Tsu-ba-me 2 years ago
parent 7182961a45
commit fadd6e3bac
  1. 34
      striker-ui/components/Panels/ExpandablePanel.tsx

@ -7,12 +7,15 @@ import { FC, ReactNode, useMemo, useState } from 'react';
import { GREY } from '../../lib/consts/DEFAULT_THEME'; import { GREY } from '../../lib/consts/DEFAULT_THEME';
import FlexBox from '../FlexBox';
import InnerPanel from './InnerPanel'; import InnerPanel from './InnerPanel';
import InnerPanelBody from './InnerPanelBody'; import InnerPanelBody from './InnerPanelBody';
import InnerPanelHeader from './InnerPanelHeader'; import InnerPanelHeader from './InnerPanelHeader';
import Spinner from '../Spinner';
type ExpandablePanelOptionalProps = { type ExpandablePanelOptionalProps = {
isExpandInitially?: boolean; isExpandInitially?: boolean;
isLoading?: boolean;
}; };
type ExpandablePanelProps = ExpandablePanelOptionalProps & { type ExpandablePanelProps = ExpandablePanelOptionalProps & {
@ -21,12 +24,14 @@ type ExpandablePanelProps = ExpandablePanelOptionalProps & {
const EXPANDABLE_PANEL_DEFAULT_PROPS: Required<ExpandablePanelOptionalProps> = { const EXPANDABLE_PANEL_DEFAULT_PROPS: Required<ExpandablePanelOptionalProps> = {
isExpandInitially: false, isExpandInitially: false,
isLoading: false,
}; };
const ExpandablePanel: FC<ExpandablePanelProps> = ({ const ExpandablePanel: FC<ExpandablePanelProps> = ({
children, children,
header, header,
isExpandInitially = EXPANDABLE_PANEL_DEFAULT_PROPS.isExpandInitially, isExpandInitially = EXPANDABLE_PANEL_DEFAULT_PROPS.isExpandInitially,
isLoading = EXPANDABLE_PANEL_DEFAULT_PROPS.isLoading,
}) => { }) => {
const [isExpand, setIsExpand] = useState<boolean>(isExpandInitially); const [isExpand, setIsExpand] = useState<boolean>(isExpandInitially);
@ -35,11 +40,34 @@ const ExpandablePanel: FC<ExpandablePanelProps> = ({
[isExpand], [isExpand],
); );
const contentHeight = useMemo(() => (isExpand ? 'auto' : '.2em'), [isExpand]); const contentHeight = useMemo(() => (isExpand ? 'auto' : '.2em'), [isExpand]);
const headerSpinner = useMemo(() => {
const spinnerLength = '1.2em';
return !isExpand && isLoading ? (
<Spinner
progressProps={{
style: { height: spinnerLength, width: spinnerLength },
}}
/>
) : undefined;
}, [isExpand, isLoading]);
const content = useMemo(
() =>
isExpand && isLoading ? (
<Spinner sx={{ margin: '1em 0' }} />
) : (
<InnerPanelBody>{children}</InnerPanelBody>
),
[children, isExpand, isLoading],
);
return ( return (
<InnerPanel> <InnerPanel>
<InnerPanelHeader> <InnerPanelHeader>
{header} <FlexBox row>
{header}
{headerSpinner}
</FlexBox>
<IconButton <IconButton
onClick={() => { onClick={() => {
setIsExpand((previous) => !previous); setIsExpand((previous) => !previous);
@ -49,9 +77,7 @@ const ExpandablePanel: FC<ExpandablePanelProps> = ({
{expandButtonIcon} {expandButtonIcon}
</IconButton> </IconButton>
</InnerPanelHeader> </InnerPanelHeader>
<Box sx={{ height: contentHeight, overflowY: 'hidden' }}> <Box sx={{ height: contentHeight, overflowY: 'hidden' }}>{content}</Box>
<InnerPanelBody>{children}</InnerPanelBody>
</Box>
</InnerPanel> </InnerPanel>
); );
}; };

Loading…
Cancel
Save