Skip to content

Commit 3edfaf0

Browse files
shaodahongafc163
authored andcommitted
fix: multiple Dialog change visible abnormal
1 parent bbdb967 commit 3edfaf0

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

examples/ant-design.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const getSvg = (path: string, props = {}, align = false) => {
3030
class MyControl extends React.Component<any, any> {
3131
state = {
3232
visible: false,
33+
visible2: false,
3334
width: 600,
3435
destroyOnClose: false,
3536
center: false,
@@ -54,6 +55,13 @@ class MyControl extends React.Component<any, any> {
5455
});
5556
}
5657

58+
onClose2 = (e: React.SyntheticEvent) => {
59+
this.setState({
60+
visible: false,
61+
visible2: false,
62+
});
63+
}
64+
5765
onDestroyOnCloseChange = (e: React.ChangeEvent<HTMLInputElement>) => {
5866
this.setState({
5967
destroyOnClose: e.target.checked,
@@ -107,6 +115,39 @@ class MyControl extends React.Component<any, any> {
107115
>
108116
<input autoFocus />
109117
<p>basic modal</p>
118+
<button onClick={() => {
119+
this.setState({
120+
visible: false,
121+
visible2: true,
122+
});
123+
}}>打开第二个并关闭当前的</button>
124+
<button onClick={() => {
125+
this.setState({
126+
visible: true,
127+
visible2: true,
128+
});
129+
}}>打开第二个</button>
130+
<button onClick={this.changeWidth}>change width</button>
131+
<button onClick={this.toggleCloseIcon}>
132+
use custom icon, is using icon: {this.state.useIcon && 'true' || 'false'}.
133+
</button>
134+
<div style={{ height: 200 }} />
135+
</Dialog>
136+
);
137+
138+
const dialog2 = (
139+
<Dialog
140+
visible={this.state.visible2}
141+
onClose={this.onClose2}
142+
>
143+
<input autoFocus />
144+
<p>basic modal</p>
145+
<button onClick={() => {
146+
this.setState({
147+
visible2: false,
148+
});
149+
}}>关闭当前</button>
150+
<button onClick={this.onClose2}>关闭所有</button>
110151
<button onClick={this.changeWidth}>change width</button>
111152
<button onClick={this.toggleCloseIcon}>
112153
use custom icon, is using icon: {this.state.useIcon && 'true' || 'false'}.
@@ -159,6 +200,7 @@ class MyControl extends React.Component<any, any> {
159200
</label>
160201
</p>
161202
{dialog}
203+
{dialog2}
162204
</div>
163205
);
164206
}

src/Dialog.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ function offset(el: any) {
4545
return pos;
4646
}
4747

48+
// https://github.com/ant-design/ant-design/issues/19340
49+
// https://github.com/ant-design/ant-design/issues/19332
50+
let cacheOverflow = {};
51+
4852
export interface IDialogChildProps extends IDialogPropTypes {
4953
getOpenCount: () => number;
5054
}
@@ -96,7 +100,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
96100
// first show
97101
if (!prevProps.visible) {
98102
this.openTime = Date.now();
99-
this.addScrollingEffect();
103+
this.switchScrollingEffect();
100104
this.tryFocus();
101105
const dialogNode = ReactDOM.findDOMNode(this.dialog);
102106
if (mousePosition) {
@@ -119,13 +123,16 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
119123
}
120124
this.lastOutSideFocusNode = null;
121125
}
126+
// if (!props.visible) {
127+
// this.switchScrollingEffect();
128+
// }
122129
}
123130
}
124131

125132
componentWillUnmount() {
126133
const { visible, getOpenCount } = this.props;
127134
if ((visible || this.inTransition) && !getOpenCount()) {
128-
this.removeScrollingEffect();
135+
this.switchScrollingEffect();
129136
}
130137
clearTimeout(this.timeoutId);
131138
}
@@ -145,7 +152,7 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
145152
this.wrap.style.display = 'none';
146153
}
147154
this.inTransition = false;
148-
this.removeScrollingEffect();
155+
this.switchScrollingEffect();
149156
if (afterClose) {
150157
afterClose();
151158
}
@@ -368,26 +375,26 @@ export default class Dialog extends React.Component<IDialogChildProps, any> {
368375
return transitionName;
369376
}
370377

371-
addScrollingEffect = () => {
378+
switchScrollingEffect = () => {
372379
const { getOpenCount } = this.props;
373380
const openCount = getOpenCount();
374-
if (openCount !== 1) {
375-
return;
376-
}
377-
switchScrollingEffect();
378-
this.cacheOverflow = {
379-
overflowX: document.body.style.overflowX,
380-
overflowY: document.body.style.overflowY,
381-
};
382-
document.body.style.overflow = 'hidden';
383-
}
384381

385-
removeScrollingEffect = () => {
386-
const { getOpenCount } = this.props;
387-
const openCount = getOpenCount();
388-
if (!openCount && this.cacheOverflow) {
389-
document.body.style.overflowX = this.cacheOverflow.overflowX;
390-
document.body.style.overflowY = this.cacheOverflow.overflowY;
382+
if (openCount === 1) {
383+
if (cacheOverflow.hasOwnProperty('overflowX')) {
384+
return;
385+
}
386+
cacheOverflow = {
387+
overflowX: document.body.style.overflowX,
388+
overflowY: document.body.style.overflowY,
389+
};
390+
document.body.style.overflow = 'hidden';
391+
switchScrollingEffect();
392+
} else if (!openCount) {
393+
if (cacheOverflow.overflowX !== undefined || cacheOverflow.overflowY !== undefined) {
394+
document.body.style.overflowX = cacheOverflow.overflowX;
395+
document.body.style.overflowY = cacheOverflow.overflowY;
396+
}
397+
cacheOverflow = {};
391398
switchScrollingEffect(true);
392399
}
393400
}

0 commit comments

Comments
 (0)