Skip to content

Commit 2d86274

Browse files
authored
feat: add autoDestroy prop(#19536); (#178)
1 parent 242e9e7 commit 2d86274

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

examples/simple.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class Test extends React.Component {
158158
});
159159
};
160160

161+
autoDestroy = e => {
162+
this.setState({
163+
autoDestroy: e.target.checked,
164+
});
165+
};
166+
161167
render() {
162168
const { state } = this;
163169
const { trigger } = state;
@@ -248,6 +254,15 @@ class Test extends React.Component {
248254
destroyPopupOnHide
249255
</label>
250256
&nbsp;&nbsp;&nbsp;&nbsp;
257+
<label>
258+
<input
259+
checked={!!this.state.autoDestroy}
260+
type="checkbox"
261+
onChange={this.autoDestroy}
262+
/>
263+
autoDestroy
264+
</label>
265+
&nbsp;&nbsp;&nbsp;&nbsp;
251266
<label>
252267
<input
253268
checked={!!this.state.mask}
@@ -294,6 +309,7 @@ class Test extends React.Component {
294309
popupAlign={this.getPopupAlign()}
295310
popupPlacement={state.placement}
296311
destroyPopupOnHide={this.state.destroyPopupOnHide}
312+
autoDestroy={this.state.autoDestroy}
297313
// zIndex={40}
298314
mask={this.state.mask}
299315
maskClosable={this.state.maskClosable}

src/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export interface TriggerProps {
7575
popupAlign?: AlignType;
7676
popupVisible?: boolean;
7777
defaultPopupVisible?: boolean;
78+
autoDestroy: boolean;
7879

7980
stretch?: string;
8081
alignPoint?: boolean; // Maybe we can support user pass position in the future
@@ -136,6 +137,7 @@ export function generateTrigger(
136137
action: [],
137138
showAction: [],
138139
hideAction: [],
140+
autoDestroy: false,
139141
};
140142

141143
popupRef = React.createRef<Popup>();
@@ -730,7 +732,13 @@ export function generateTrigger(
730732

731733
render() {
732734
const { popupVisible } = this.state;
733-
const { children, forceRender, alignPoint, className } = this.props;
735+
const {
736+
children,
737+
forceRender,
738+
alignPoint,
739+
className,
740+
autoDestroy,
741+
} = this.props;
734742
const child = React.Children.only(children) as React.ReactElement;
735743
const newChildProps: HTMLAttributes<HTMLElement> & { key: string } = {
736744
key: 'trigger',
@@ -802,6 +810,10 @@ export function generateTrigger(
802810
);
803811
}
804812

813+
if (!popupVisible && autoDestroy) {
814+
portal = null;
815+
}
816+
805817
return (
806818
<TriggerContext.Provider
807819
value={{ onPopupMouseDown: this.onPopupMouseDown }}

tests/basic.test.jsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import Portal from 'rc-util/lib/Portal';
34
import Trigger from '../src';
45

56
const autoAdjustOverflow = {
@@ -382,6 +383,43 @@ describe('Trigger.Basic', () => {
382383
});
383384
});
384385

386+
describe('support autoDestroy', () => {
387+
it('defaults to false', () => {
388+
const wrapper = mount(
389+
<Trigger
390+
action={['click']}
391+
popupAlign={placementAlignMap.topRight}
392+
popup={<strong>trigger</strong>}
393+
>
394+
<div className="target">click</div>
395+
</Trigger>,
396+
);
397+
expect(wrapper.prop('autoDestroy')).toBeFalsy();
398+
wrapper.trigger();
399+
expect(wrapper.find(Portal).exists()).toBe(true);
400+
wrapper.trigger();
401+
expect(wrapper.find(Portal).exists()).toBe(true);
402+
});
403+
404+
it('set true will destroy portal on hide', () => {
405+
const wrapper = mount(
406+
<Trigger
407+
action={['click']}
408+
autoDestroy
409+
popupAlign={placementAlignMap.topRight}
410+
popup={<strong>trigger</strong>}
411+
>
412+
<div className="target">click</div>
413+
</Trigger>,
414+
);
415+
416+
wrapper.trigger();
417+
expect(wrapper.find(Portal).exists()).toBe(true);
418+
wrapper.trigger();
419+
expect(wrapper.find(Portal).exists()).toBe(false);
420+
});
421+
});
422+
385423
describe('github issues', () => {
386424
// https://github.com/ant-design/ant-design/issues/9114
387425
it('click in popup of popup', () => {

0 commit comments

Comments
 (0)