Skip to content

Commit

Permalink
feat(ui): support destroy node after element hided
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Nov 10, 2022
1 parent 355c2d9 commit 060c0cd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/ui/src/components/_transition/Transition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ export interface DTransitionProps {
dDuring: number | { enter: number; leave: number };
dMountBeforeFirstEnter?: boolean;
dSkipFirstTransition?: boolean | [boolean, boolean];
dDestroyWhenLeaved?: boolean;
onEnter?: () => void;
afterEnter?: () => void;
afterLeave?: () => void;
}

export function DTransition(props: DTransitionProps): JSX.Element | null {
const { children, dIn, dDuring, dMountBeforeFirstEnter = true, dSkipFirstTransition = true, onEnter, afterEnter, afterLeave } = props;
const {
children,
dIn,
dDuring,
dMountBeforeFirstEnter = true,
dSkipFirstTransition = true,
dDestroyWhenLeaved = false,
onEnter,
afterEnter,
afterLeave,
} = props;

const initState = useMemo<DTransitionState>(() => {
const [skipEnter, skipLeave] = isArray(dSkipFirstTransition) ? dSkipFirstTransition : [dSkipFirstTransition, dSkipFirstTransition];
Expand Down Expand Up @@ -101,6 +112,10 @@ export function DTransition(props: DTransitionProps): JSX.Element | null {
}, [stateChange]);

const shouldRender = (() => {
if (state === 'leaved' && dDestroyWhenLeaved) {
return false;
}

if (dataRef.current.isFirstEnter && !dMountBeforeFirstEnter) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function DAlert(props: DAlertProps): JSX.Element | null {
},
leaved: { display: 'none' },
}}
dDestroyWhenLeaved
afterEnter={() => {
afterVisibleChange?.(true);
}}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function Button(props: DButtonProps, ref: React.ForwardedRef<HTMLButtonElement>)
},
leaved: { display: 'none' },
}}
dDestroyWhenLeaved
>
{(collapseRef, collapseStyle) => buttonIcon(true, collapseRef, collapseStyle)}
</DCollapseTransition>
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface DDrawerProps extends React.HTMLAttributes<HTMLDivElement> {
dMaskClosable?: boolean;
dEscClosable?: boolean;
dSkipFirstTransition?: boolean;
dDestroyAfterClose?: boolean;
dHeader?: React.ReactElement | string;
dFooter?: React.ReactElement;
dChildDrawer?: React.ReactElement;
Expand Down Expand Up @@ -59,6 +60,7 @@ export const DDrawer: {
dMaskClosable = true,
dEscClosable = true,
dSkipFirstTransition = true,
dDestroyAfterClose = true,
dHeader,
dFooter,
dChildDrawer,
Expand Down Expand Up @@ -232,6 +234,7 @@ export const DDrawer: {
dIn={visible}
dDuring={TTANSITION_DURING_BASE}
dSkipFirstTransition={dSkipFirstTransition}
dDestroyWhenLeaved={dDestroyAfterClose}
afterEnter={() => {
afterVisibleChange?.(true);
}}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/image/ImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export function DImagePreview(props: DImagePreviewProps): JSX.Element | null {
<DTransition
dIn={visible}
dDuring={TTANSITION_DURING_BASE}
dDestroyWhenLeaved
afterEnter={() => {
afterVisibleChange?.(true);
}}
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface DModalProps extends React.HTMLAttributes<HTMLDivElement> {
dMaskClosable?: boolean;
dEscClosable?: boolean;
dSkipFirstTransition?: boolean;
dDestroyAfterClose?: boolean;
dType?: {
type: 'success' | 'warning' | 'error' | 'info';
title?: React.ReactNode;
Expand Down Expand Up @@ -54,6 +55,7 @@ export const DModal: {
dMaskClosable = true,
dEscClosable = true,
dSkipFirstTransition = true,
dDestroyAfterClose = true,
dType,
dHeader,
dFooter,
Expand Down Expand Up @@ -137,6 +139,7 @@ export const DModal: {
dIn={visible}
dDuring={TTANSITION_DURING_BASE}
dSkipFirstTransition={dSkipFirstTransition}
dDestroyWhenLeaved={dDestroyAfterClose}
onEnter={() => {
if (isUndefined(ROOT_DATA.clickEvent) || performance.now() - ROOT_DATA.clickEvent.time > 100) {
dataRef.current.transformOrigin = undefined;
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface DPopoverProps extends Omit<React.HTMLAttributes<HTMLDivElement>
dContainer?: DRefExtra | false;
dPlacement?: DPopupPlacement;
dEscClosable?: boolean;
dDestroyAfterClose?: boolean;
dArrow?: boolean;
dModal?: boolean;
dDistance?: number;
Expand All @@ -53,6 +54,7 @@ function Popover(props: DPopoverProps, ref: React.ForwardedRef<DPopoverRef>): JS
dContainer,
dPlacement = 'top',
dEscClosable = true,
dDestroyAfterClose = false,
dArrow = true,
dModal = false,
dDistance = 10,
Expand Down Expand Up @@ -292,6 +294,7 @@ function Popover(props: DPopoverProps, ref: React.ForwardedRef<DPopoverRef>): JS
<DTransition
dIn={visible}
dDuring={TTANSITION_DURING}
dDestroyWhenLeaved={dDestroyAfterClose}
onEnter={updatePosition}
afterEnter={() => {
afterVisibleChange?.(true);
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function DSpinner(props: DSpinnerProps): JSX.Element | null {
containerRef.current.style.height = `${spinnerRef.current.clientHeight}px`;
}
}}
dDestroyWhenLeaved
afterEnter={() => {
afterVisibleChange?.(true);
}}
Expand Down

0 comments on commit 060c0cd

Please sign in to comment.