Skip to content

Commit 3b39854

Browse files
authored
fate: add modalRender (#195)
* feat: 添加modalRender的属性 test: 添加的测试用例 fix * fix: 删除多余的空格 * feat: 添加拖拽modal的例子。
1 parent 7a95984 commit 3b39854

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

examples/draggable.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'bootstrap/dist/css/bootstrap.css';
2+
import '../assets/bootstrap.less';
3+
import * as React from 'react';
4+
import Draggable from 'react-draggable';
5+
import Dialog from '../src/DialogWrap';
6+
7+
const MyControl = () => {
8+
const [visible, setVisible] = React.useState(false);
9+
const [disabled, setDisabled] = React.useState(true);
10+
const onClick = () => {
11+
setVisible(true);
12+
}
13+
14+
const onClose = () => {
15+
setVisible(false);
16+
}
17+
18+
return (
19+
<div style={{ margin: 20 }}>
20+
<p>
21+
<button type="button" className="btn btn-primary" onClick={onClick}>show dialog</button>
22+
</p>
23+
<Dialog
24+
visible={visible}
25+
animation="slide-fade"
26+
maskAnimation="fade"
27+
onClose={onClose}
28+
style={{ width: 600 }}
29+
title={(
30+
<div
31+
style={{
32+
width: '100%',
33+
cursor: 'pointer',
34+
}}
35+
onMouseOver={() => {
36+
if (disabled){
37+
setDisabled(false)
38+
}
39+
}}
40+
onMouseOut={() => {
41+
setDisabled(true)
42+
}}
43+
// fix eslintjsx-a11y/mouse-events-have-key-events
44+
// https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/mouse-events-have-key-events.md
45+
onFocus={ () => {} }
46+
onBlur={ () => {}}
47+
// end
48+
>modal</div>
49+
)}
50+
modalRender={modal => <Draggable disabled={disabled}>{modal}</Draggable>}
51+
>
52+
<div
53+
style={{
54+
height: 200,
55+
}}
56+
>
57+
Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.
58+
</div>
59+
</Dialog>
60+
</div>
61+
);
62+
};
63+
64+
export default MyControl;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"rc-drawer": "4.1.0",
7070
"react": "^16.9.0",
7171
"react-dom": "^16.9.0",
72+
"react-draggable": "^4.4.3",
7273
"typescript": "^4.0.2"
7374
},
7475
"typings": "./lib/DialogWrap.d.ts"

src/Dialog.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
233233
bodyProps,
234234
children,
235235
destroyOnClose,
236+
modalRender,
236237
} = this.props;
237238
const dest: any = {};
238239
if (width !== undefined) {
@@ -276,6 +277,22 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
276277
);
277278
}
278279

280+
const content = (
281+
<div className={`${prefixCls}-content`}>
282+
{closer}
283+
{headerNode}
284+
<div
285+
className={`${prefixCls}-body`}
286+
style={bodyStyle}
287+
ref={this.saveRef('body')}
288+
{...bodyProps}
289+
>
290+
{children}
291+
</div>
292+
{footerNode}
293+
</div>
294+
)
295+
279296
const styleBox = { ...style, ...dest };
280297
const sentinelStyle = { width: 0, height: 0, overflow: 'hidden', outline: 'none' };
281298
const transitionName = this.getTransitionName();
@@ -296,19 +313,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
296313
style={sentinelStyle}
297314
aria-hidden="true"
298315
/>
299-
<div className={`${prefixCls}-content`}>
300-
{closer}
301-
{headerNode}
302-
<div
303-
className={`${prefixCls}-body`}
304-
style={bodyStyle}
305-
ref={this.saveRef('body')}
306-
{...bodyProps}
307-
>
308-
{children}
309-
</div>
310-
{footerNode}
311-
</div>
316+
{modalRender ? modalRender(content) : content }
312317
<div
313318
tabIndex={0}
314319
ref={this.saveRef('sentinelEnd')}

src/IDialogPropTypes.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface IDialogPropTypes {
3737
wrapProps?: any;
3838
getContainer?: IStringOrHtmlElement | (() => IStringOrHtmlElement) | false;
3939
closeIcon?: ReactNode;
40+
modalRender?: (node: ReactNode) => ReactNode;
4041
forceRender?: boolean;
4142
// https://github.com/ant-design/ant-design/issues/19771
4243
// https://github.com/react-component/dialog/issues/95

tests/index.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react/no-render-return-value */
22
/* eslint-disable func-names */
3-
import React from 'react';
3+
import React, { cloneElement } from 'react';
44
import { mount } from 'enzyme';
55

66
import KeyCode from 'rc-util/lib/KeyCode';
@@ -425,4 +425,16 @@ describe('dialog', () => {
425425
d.update();
426426
expect(d.find('.rc-dialog-wrap').props().style.display).toEqual(null);
427427
});
428+
429+
it('modalRender', () => {
430+
const modalRender = mount(
431+
<DialogWrap modalRender={node => cloneElement(node, { ...node.props, style: { background: '#1890ff' }})}>
432+
</DialogWrap>
433+
);
434+
modalRender.setState({ visible: true });
435+
jest.runAllTimers();
436+
modalRender.update();
437+
expect(modalRender.find('.rc-dialog-content').props().style.background).toEqual('#1890ff');
438+
modalRender.unmount();
439+
});
428440
});

0 commit comments

Comments
 (0)