Skip to content

Commit

Permalink
chore: code optimization (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed May 8, 2024
1 parent 852caad commit dcc74ee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Dialog/Content/MemoChildren.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
export type MemoChildrenProps = {
shouldUpdate: boolean;
children: React.ReactNode;
}
};

export default React.memo(
({ children }: MemoChildrenProps) => children as React.ReactElement,
Expand Down
72 changes: 41 additions & 31 deletions src/Dialog/Content/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
contentStyle.height = height;
}
// ================================ Render ================================
let footerNode: React.ReactNode;
if (footer) {
footerNode = <div className={classNames(`${prefixCls}-footer`, modalClassNames?.footer)} style={{...modalStyles?.footer}}>{footer}</div>;
}
const footerNode = footer ? (
<div
className={classNames(`${prefixCls}-footer`, modalClassNames?.footer)}
style={{ ...modalStyles?.footer }}
>
{footer}
</div>
) : null;

let headerNode: React.ReactNode;
if (title) {
headerNode = (
<div className={classNames(`${prefixCls}-header`, modalClassNames?.header)} style={{...modalStyles?.header}}>
<div className={`${prefixCls}-title`} id={ariaId}>
{title}
</div>
const headerNode = title ? (
<div
className={classNames(`${prefixCls}-header`, modalClassNames?.header)}
style={{ ...modalStyles?.header }}
>
<div className={`${prefixCls}-title`} id={ariaId}>
{title}
</div>
);
}
</div>
) : null;


const closableObj = useMemo(() => {
if (typeof closable === 'object' && closable !== null) {
return closable;
Expand All @@ -106,24 +109,34 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
return { closeIcon: closeIcon ?? <span className={`${prefixCls}-close-x`} /> };
}
return {};
}, [closable, closeIcon]);
}, [closable, closeIcon, prefixCls]);

const ariaProps = pickAttrs(closableObj, true);

let closer: React.ReactNode;
if (closable) {
closer = (
<button type="button" onClick={onClose} aria-label="Close" {...ariaProps} className={`${prefixCls}-close`}>
{closableObj.closeIcon}
</button>
);
}

const closerNode = closable ? (
<button
type="button"
onClick={onClose}
aria-label="Close"
{...ariaProps}
className={`${prefixCls}-close`}
>
{closableObj.closeIcon}
</button>
) : null;

const content = (
<div className={classNames(`${prefixCls}-content`, modalClassNames?.content)} style={modalStyles?.content}>
{closer}
<div
className={classNames(`${prefixCls}-content`, modalClassNames?.content)}
style={modalStyles?.content}
>
{closerNode}
{headerNode}
<div className={classNames(`${prefixCls}-body`, modalClassNames?.body)} style={{...bodyStyle, ...modalStyles?.body}} {...bodyProps}>
<div
className={classNames(`${prefixCls}-body`, modalClassNames?.body)}
style={{ ...bodyStyle, ...modalStyles?.body }}
{...bodyProps}
>
{children}
</div>
{footerNode}
Expand All @@ -137,10 +150,7 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
aria-labelledby={title ? ariaId : null}
aria-modal="true"
ref={mergedRef}
style={{
...style,
...contentStyle,
}}
style={{ ...style, ...contentStyle }}
className={classNames(prefixCls, className)}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
Expand Down
7 changes: 4 additions & 3 deletions src/Dialog/Mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ export type MaskProps = {
className?: string;
};

export default function Mask(props: MaskProps) {
const Mask: React.FC<MaskProps> = (props) => {
const { prefixCls, style, visible, maskProps, motionName, className } = props;

return (
<CSSMotion
key="mask"
Expand All @@ -31,4 +30,6 @@ export default function Mask(props: MaskProps) {
)}
</CSSMotion>
);
}
};

export default Mask;
30 changes: 17 additions & 13 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import type { ContentRef } from './Content/Panel';
import Mask from './Mask';
import { warning } from 'rc-util/lib/warning';

export default function Dialog(props: IDialogPropTypes) {
const Dialog: React.FC<IDialogPropTypes> = (props) => {
const {
prefixCls = 'rc-dialog',
zIndex,
visible = false,
keyboard = true,
focusTriggerAfterClose = true,
// scrollLocker,

// Wrapper
wrapStyle,
wrapClassName,
Expand All @@ -47,12 +46,12 @@ export default function Dialog(props: IDialogPropTypes) {
} = props;

if (process.env.NODE_ENV !== 'production') {
["wrapStyle", "bodyStyle", "maskStyle"].forEach((prop) => {
['wrapStyle', 'bodyStyle', 'maskStyle'].forEach((prop) => {
// (prop in props) && console.error(`Warning: ${prop} is deprecated, please use styles instead.`)
warning(!(prop in props), `${prop} is deprecated, please use styles instead.`)
warning(!(prop in props), `${prop} is deprecated, please use styles instead.`);
});
if ("wrapClassName" in props) {
warning(false, `wrapClassName is deprecated, please use classNames instead.`)
if ('wrapClassName' in props) {
warning(false, `wrapClassName is deprecated, please use classNames instead.`);
}
}

Expand Down Expand Up @@ -167,6 +166,13 @@ export default function Dialog(props: IDialogPropTypes) {
[],
);

const mergedStyle: React.CSSProperties = {
zIndex,
...wrapStyle,
...modalStyles?.wrapper,
display: !animatedVisible ? 'none' : null,
};

// ========================= Render =========================
return (
<div
Expand All @@ -177,11 +183,7 @@ export default function Dialog(props: IDialogPropTypes) {
prefixCls={prefixCls}
visible={mask && visible}
motionName={getMotionName(prefixCls, maskTransitionName, maskAnimation)}
style={{
zIndex,
...maskStyle,
...modalStyles?.mask,
}}
style={{ zIndex, ...maskStyle, ...modalStyles?.mask }}
maskProps={maskProps}
className={modalClassNames?.mask}
/>
Expand All @@ -191,7 +193,7 @@ export default function Dialog(props: IDialogPropTypes) {
className={classNames(`${prefixCls}-wrap`, wrapClassName, modalClassNames?.wrapper)}
ref={wrapperRef}
onClick={onWrapperClick}
style={{ zIndex, ...wrapStyle, ...modalStyles?.wrapper, display: !animatedVisible ? 'none' : null }}
style={mergedStyle}
{...wrapProps}
>
<Content
Expand All @@ -210,4 +212,6 @@ export default function Dialog(props: IDialogPropTypes) {
</div>
</div>
);
}
};

export default Dialog;
2 changes: 1 addition & 1 deletion src/DialogWrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { IDialogPropTypes } from './IDialogPropTypes';
* So here should add a child (div element) to custom container.
* */

const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
const {
visible,
getContainer,
Expand Down
2 changes: 1 addition & 1 deletion src/IDialogPropTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type IDialogPropTypes = {
keyboard?: boolean;
style?: CSSProperties;
mask?: boolean;
children?: any;
children?: React.ReactNode;
afterClose?: () => any;
afterOpenChange?: (open: boolean) => void;
onClose?: (e: SyntheticEvent) => any;
Expand Down

0 comments on commit dcc74ee

Please sign in to comment.