Skip to content

Commit

Permalink
feat: dialog support classNames (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiner-tang committed Sep 18, 2023
1 parent 8fb5644 commit bdb8573
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ReactDOM.render(
| ---------------------- | ------------------------------ | --------- | ------------------------------------------------------------------------------- | ------- |
| prefixCls | String | rc-dialog | The dialog dom node's prefixCls | |
| className | String | | additional className for dialog | |
| classNames | { mask?: string; wrapper?: string; header?: string; body?: string; footer?: string} | | pass className to target area | |
| style | Object | {} | Root style for dialog element.Such as width, height | |
| zIndex | Number | | | |
| bodyStyle | Object | {} | body style for dialog body element.Such as height | |
Expand Down
7 changes: 4 additions & 3 deletions src/Dialog/Content/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
forceRender,
width,
height,
classNames: modalClassNames,
} = props;

// ================================= Refs =================================
Expand Down Expand Up @@ -78,13 +79,13 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
// ================================ Render ================================
let footerNode: React.ReactNode;
if (footer) {
footerNode = <div className={`${prefixCls}-footer`}>{footer}</div>;
footerNode = <div className={classNames(`${prefixCls}-footer`, modalClassNames?.footer)}>{footer}</div>;
}

let headerNode: React.ReactNode;
if (title) {
headerNode = (
<div className={`${prefixCls}-header`}>
<div className={classNames(`${prefixCls}-header`, modalClassNames?.header)}>
<div className={`${prefixCls}-title`} id={ariaId}>
{title}
</div>
Expand All @@ -105,7 +106,7 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
<div className={`${prefixCls}-content`}>
{closer}
{headerNode}
<div className={`${prefixCls}-body`} style={bodyStyle} {...bodyProps}>
<div className={classNames(`${prefixCls}-body`, modalClassNames?.body)} style={bodyStyle} {...bodyProps}>
{children}
</div>
{footerNode}
Expand Down
5 changes: 3 additions & 2 deletions src/Dialog/Mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export type MaskProps = {
motionName?: string;
style?: React.CSSProperties;
maskProps?: React.HTMLAttributes<HTMLDivElement>;
className?: string;
};

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

return (
<CSSMotion
Expand All @@ -24,7 +25,7 @@ export default function Mask(props: MaskProps) {
<div
ref={ref}
style={{ ...motionStyle, ...style }}
className={classNames(`${prefixCls}-mask`, motionClassName)}
className={classNames(`${prefixCls}-mask`, motionClassName, className)}
{...maskProps}
/>
)}
Expand Down
4 changes: 3 additions & 1 deletion src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function Dialog(props: IDialogPropTypes) {
maskStyle,
maskProps,
rootClassName,
classNames: modalClassNames,
} = props;

const lastOutSideActiveElementRef = useRef<HTMLElement>();
Expand Down Expand Up @@ -169,11 +170,12 @@ export default function Dialog(props: IDialogPropTypes) {
...maskStyle,
}}
maskProps={maskProps}
className={modalClassNames?.mask}
/>
<div
tabIndex={-1}
onKeyDown={onWrapperKeyDown}
className={classNames(`${prefixCls}-wrap`, wrapClassName)}
className={classNames(`${prefixCls}-wrap`, wrapClassName, modalClassNames?.wrapper)}
ref={wrapperRef}
onClick={onWrapperClick}
style={{ zIndex, ...wrapStyle, display: !animatedVisible ? 'none' : null }}
Expand Down
10 changes: 10 additions & 0 deletions src/IDialogPropTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import type { GetContainer } from 'rc-util/lib/PortalWrapper';
import type { CSSProperties, ReactNode, SyntheticEvent } from 'react';


export interface ModalClassNames {
header: string;
body: string;
footer: string;
mask: string;
wrapper: string;
}

export type IDialogPropTypes = {
className?: string;
keyboard?: boolean;
Expand Down Expand Up @@ -35,6 +44,7 @@ export type IDialogPropTypes = {
bodyProps?: any;
maskProps?: any;
rootClassName?: string;
classNames?: ModalClassNames;
wrapProps?: any;
getContainer?: GetContainer | false;
closeIcon?: ReactNode;
Expand Down
65 changes: 65 additions & 0 deletions tests/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,68 @@ exports[`dialog should render correct 1`] = `
</div>
</div>
`;

exports[`dialog should support classNames 1`] = `
<div
class="rc-dialog-root"
>
<div
class="rc-dialog-mask custom-mask"
/>
<div
class="rc-dialog-wrap custom-wrapper"
style="font-size: 10px;"
tabindex="-1"
>
<div
aria-labelledby="test-id"
aria-modal="true"
class="rc-dialog"
role="dialog"
style="width: 600px; height: 903px;"
>
<div
aria-hidden="true"
style="width: 0px; height: 0px; overflow: hidden; outline: none;"
tabindex="0"
/>
<div
class="rc-dialog-content"
>
<button
aria-label="Close"
class="rc-dialog-close"
type="button"
>
<span
class="rc-dialog-close-x"
/>
</button>
<div
class="rc-dialog-header custom-header"
>
<div
class="rc-dialog-title"
id="test-id"
>
Default
</div>
</div>
<div
class="rc-dialog-body custom-body"
/>
<div
class="rc-dialog-footer custom-footer"
>
Footer
</div>
</div>
<div
aria-hidden="true"
style="width: 0px; height: 0px; overflow: hidden; outline: none;"
tabindex="0"
/>
</div>
</div>
</div>
`;
30 changes: 30 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,34 @@ describe('dialog', () => {
expect(afterOpenChange).toHaveBeenCalledTimes(2);
});
});

it('should support classNames', () => {
const wrapper = mount(
<Dialog
visible
title='Default'
footer='Footer'
classNames={{
header: 'custom-header',
body: 'custom-body',
footer: 'custom-footer',
mask: 'custom-mask',
wrapper: 'custom-wrapper',
}}
style={{ width: 600 }}
height={903}
wrapStyle={{ fontSize: 10 }}
/>,
);
jest.runAllTimers();
wrapper.update();

expect(wrapper.render()).toMatchSnapshot();
console.log(wrapper.find('.rc-dialog-wrap').html())
expect(wrapper.find('.rc-dialog-wrap').props().className).toContain('custom-wrapper');
expect(wrapper.find('.rc-dialog-body').props().className).toContain('custom-body');
expect(wrapper.find('.rc-dialog-header').props().className).toContain('custom-header');
expect(wrapper.find('.rc-dialog-footer').props().className).toContain('custom-footer');
expect(wrapper.find('.rc-dialog-mask').props().className).toContain('custom-mask');
});
});

0 comments on commit bdb8573

Please sign in to comment.