2020-09-03 12:36:06 +08:00
|
|
|
---
|
|
|
|
order: 13
|
|
|
|
title:
|
|
|
|
zh-CN: 自定义渲染对话框
|
|
|
|
en-US: Custom modal content render
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
自定义渲染对话框, 可通过 `react-draggable` 来实现拖拽。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Custom modal content render. use `react-draggable` implements draggable.
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
import { Modal, Button } from 'antd';
|
|
|
|
import Draggable from 'react-draggable';
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
visible: false,
|
|
|
|
disabled: true,
|
2020-12-28 10:45:06 +08:00
|
|
|
bounds: { left: 0, top: 0, bottom: 0, right: 0 },
|
2020-09-03 12:36:06 +08:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:45:06 +08:00
|
|
|
draggleRef = React.createRef();
|
|
|
|
|
2020-09-03 12:36:06 +08:00
|
|
|
showModal = () => {
|
|
|
|
this.setState({
|
|
|
|
visible: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleOk = e => {
|
|
|
|
console.log(e);
|
|
|
|
this.setState({
|
|
|
|
visible: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCancel = e => {
|
|
|
|
console.log(e);
|
|
|
|
this.setState({
|
|
|
|
visible: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:45:06 +08:00
|
|
|
onStart = (event, uiData) => {
|
2021-11-26 12:18:21 +08:00
|
|
|
const { clientWidth, clientHeight } = window.document.documentElement;
|
|
|
|
const targetRect = this.draggleRef.current?.getBoundingClientRect();
|
|
|
|
if (!targetRect) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-28 10:45:06 +08:00
|
|
|
this.setState({
|
|
|
|
bounds: {
|
2021-11-26 12:18:21 +08:00
|
|
|
left: -targetRect.left + uiData.x,
|
|
|
|
right: clientWidth - (targetRect.right - uiData.x),
|
|
|
|
top: -targetRect.top + uiData.y,
|
|
|
|
bottom: clientHeight - (targetRect.bottom - uiData.y),
|
2020-12-28 10:45:06 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-03 12:36:06 +08:00
|
|
|
render() {
|
2020-12-28 10:45:06 +08:00
|
|
|
const { bounds, disabled, visible } = this.state;
|
2020-09-03 12:36:06 +08:00
|
|
|
return (
|
|
|
|
<>
|
2020-10-21 11:06:07 +08:00
|
|
|
<Button onClick={this.showModal}>Open Draggable Modal</Button>
|
2020-09-03 12:36:06 +08:00
|
|
|
<Modal
|
|
|
|
title={
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
width: '100%',
|
|
|
|
cursor: 'move',
|
|
|
|
}}
|
|
|
|
onMouseOver={() => {
|
2020-12-28 10:45:06 +08:00
|
|
|
if (disabled) {
|
2020-09-03 12:36:06 +08:00
|
|
|
this.setState({
|
2020-10-10 23:02:38 +08:00
|
|
|
disabled: false,
|
2020-09-03 12:36:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onMouseOut={() => {
|
|
|
|
this.setState({
|
2020-10-10 23:02:38 +08:00
|
|
|
disabled: true,
|
2020-09-03 12:36:06 +08:00
|
|
|
});
|
|
|
|
}}
|
|
|
|
// 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
|
|
|
|
>
|
|
|
|
Draggable Modal
|
|
|
|
</div>
|
|
|
|
}
|
2020-12-28 10:45:06 +08:00
|
|
|
visible={visible}
|
2020-09-03 12:36:06 +08:00
|
|
|
onOk={this.handleOk}
|
|
|
|
onCancel={this.handleCancel}
|
2020-12-28 10:45:06 +08:00
|
|
|
modalRender={modal => (
|
|
|
|
<Draggable
|
|
|
|
disabled={disabled}
|
|
|
|
bounds={bounds}
|
|
|
|
onStart={(event, uiData) => this.onStart(event, uiData)}
|
|
|
|
>
|
|
|
|
<div ref={this.draggleRef}>{modal}</div>
|
|
|
|
</Draggable>
|
|
|
|
)}
|
2020-09-03 12:36:06 +08:00
|
|
|
>
|
|
|
|
<p>
|
|
|
|
Just don't learn physics at school and your life will be full of magic and
|
|
|
|
miracles.
|
|
|
|
</p>
|
|
|
|
<br />
|
|
|
|
<p>Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.</p>
|
|
|
|
</Modal>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <App />;
|
2020-09-03 12:36:06 +08:00
|
|
|
```
|