Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add modalRender #195

Merged
merged 4 commits into from Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/draggable.tsx
@@ -0,0 +1,64 @@
import 'bootstrap/dist/css/bootstrap.css';
import '../assets/bootstrap.less';
import * as React from 'react';
import Draggable from 'react-draggable';
import Dialog from '../src/DialogWrap';

const MyControl = () => {
const [visible, setVisible] = React.useState(false);
const [disabled, setDisabled] = React.useState(true);
const onClick = () => {
setVisible(true);
}

const onClose = () => {
setVisible(false);
}

return (
<div style={{ margin: 20 }}>
<p>
<button type="button" className="btn btn-primary" onClick={onClick}>show dialog</button>
</p>
<Dialog
visible={visible}
animation="slide-fade"
maskAnimation="fade"
onClose={onClose}
style={{ width: 600 }}
title={(
<div
style={{
width: '100%',
cursor: 'pointer',
}}
onMouseOver={() => {
if (disabled){
setDisabled(false)
}
}}
onMouseOut={() => {
setDisabled(true)
}}
// fix eslintjsx-a11y/mouse-events-have-key-events
// https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/mouse-events-have-key-events.md
onFocus={ () => {} }
onBlur={ () => {}}
// end
>modal</div>
)}
modalRender={modal => <Draggable disabled={disabled}>{modal}</Draggable>}
>
<div
style={{
height: 200,
}}
>
Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.
</div>
</Dialog>
</div>
);
};

export default MyControl;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -69,6 +69,7 @@
"rc-drawer": "4.1.0",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-draggable": "^4.4.3",
"typescript": "^4.0.2"
},
"typings": "./lib/DialogWrap.d.ts"
Expand Down
31 changes: 18 additions & 13 deletions src/Dialog.tsx
Expand Up @@ -233,6 +233,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
bodyProps,
children,
destroyOnClose,
modalRender,
} = this.props;
const dest: any = {};
if (width !== undefined) {
Expand Down Expand Up @@ -276,6 +277,22 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
);
}

const content = (
<div className={`${prefixCls}-content`}>
{closer}
{headerNode}
<div
className={`${prefixCls}-body`}
style={bodyStyle}
ref={this.saveRef('body')}
{...bodyProps}
>
{children}
</div>
{footerNode}
</div>
)

const styleBox = { ...style, ...dest };
const sentinelStyle = { width: 0, height: 0, overflow: 'hidden', outline: 'none' };
const transitionName = this.getTransitionName();
Expand All @@ -296,19 +313,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
style={sentinelStyle}
aria-hidden="true"
/>
<div className={`${prefixCls}-content`}>
{closer}
{headerNode}
<div
className={`${prefixCls}-body`}
style={bodyStyle}
ref={this.saveRef('body')}
{...bodyProps}
>
{children}
</div>
{footerNode}
</div>
{modalRender ? modalRender(content) : content }
<div
tabIndex={0}
ref={this.saveRef('sentinelEnd')}
Expand Down
1 change: 1 addition & 0 deletions src/IDialogPropTypes.tsx
Expand Up @@ -37,6 +37,7 @@ export interface IDialogPropTypes {
wrapProps?: any;
getContainer?: IStringOrHtmlElement | (() => IStringOrHtmlElement) | false;
closeIcon?: ReactNode;
modalRender?: (node: ReactNode) => ReactNode;
forceRender?: boolean;
// https://github.com/ant-design/ant-design/issues/19771
// https://github.com/react-component/dialog/issues/95
Expand Down
14 changes: 13 additions & 1 deletion tests/index.spec.js
@@ -1,6 +1,6 @@
/* eslint-disable react/no-render-return-value */
/* eslint-disable func-names */
import React from 'react';
import React, { cloneElement } from 'react';
import { mount } from 'enzyme';

import KeyCode from 'rc-util/lib/KeyCode';
Expand Down Expand Up @@ -425,4 +425,16 @@ describe('dialog', () => {
d.update();
expect(d.find('.rc-dialog-wrap').props().style.display).toEqual(null);
});

it('modalRender', () => {
const modalRender = mount(
<DialogWrap modalRender={node => cloneElement(node, { ...node.props, style: { background: '#1890ff' }})}>
</DialogWrap>
);
modalRender.setState({ visible: true });
jest.runAllTimers();
modalRender.update();
expect(modalRender.find('.rc-dialog-content').props().style.background).toEqual('#1890ff');
modalRender.unmount();
});
});