Skip to content

Commit ee5b988

Browse files
authored
chore: upgrade trigger to 4.2.1 (#216)
1 parent 185c031 commit ee5b988

File tree

5 files changed

+118
-25
lines changed

5 files changed

+118
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ Online examples: <https://react-component.github.io/tooltip/examples/>
182182
</tr>
183183
<tr>
184184
<td>destroyTooltipOnHide</td>
185-
<td>boolean</td>
185+
<td>boolean | { keepParent: boolean }</td>
186186
<td>false</td>
187-
<td>whether destroy tooltip when tooltip is hidden</td>
187+
<td>whether destroy tooltip when tooltip is hidden.In general, destroyTooltipOnHide will only remove itself instead of parent container of it.Parent container will be removed include tooltip when keepParent is true</td>
188188
</tr>
189189
<tr>
190190
<td>id</td>

examples/simple.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ import { placements } from '../src/placements';
66
class Test extends Component {
77
state = {
88
destroyTooltipOnHide: false,
9+
destroyTooltipOptions: [
10+
{
11+
name: "don't destroy",
12+
value: 0,
13+
},
14+
{
15+
name: 'destroy parent',
16+
value: 1,
17+
},
18+
{
19+
name: 'keep parent',
20+
value: 2,
21+
},
22+
],
923
placement: 'right',
1024
trigger: {
1125
hover: 1,
@@ -60,10 +74,11 @@ class Test extends Component {
6074
console.log('tooltip', visible); // eslint-disable-line no-console
6175
};
6276

63-
onDestroyCheck = () => {
64-
this.setState(prevState => ({
65-
destroyTooltipOnHide: !prevState.destroyTooltipOnHide,
66-
}));
77+
onDestroyChange = e => {
78+
const { value } = e.target;
79+
this.setState({
80+
destroyTooltipOnHide: [false, { keepParent: false }, { keepParent: true }][value],
81+
});
6782
};
6883

6984
preventDefault = e => {
@@ -78,10 +93,7 @@ class Test extends Component {
7893
<div style={{ margin: '10px 20px' }}>
7994
<label>
8095
placement:
81-
<select
82-
value={this.state.placement}
83-
onChange={this.onPlacementChange}
84-
>
96+
<select value={this.state.placement} onChange={this.onPlacementChange}>
8597
{Object.keys(placements).map(p => (
8698
<option key={p} value={p}>
8799
{p}
@@ -101,12 +113,14 @@ class Test extends Component {
101113
</label>
102114
&nbsp;&nbsp;&nbsp;&nbsp;
103115
<label>
104-
<input
105-
type="checkbox"
106-
onChange={this.onDestroyCheck}
107-
checked={this.state.destroyTooltipOnHide}
108-
/>
109-
destroyTooltipOnHide
116+
destroyTooltipOnHide:
117+
<select onChange={this.onDestroyChange}>
118+
{this.state.destroyTooltipOptions.map(({ name, value }) => (
119+
<option key={value} value={value}>
120+
{name}
121+
</option>
122+
))}
123+
</select>
110124
</label>
111125
&nbsp;&nbsp;&nbsp;&nbsp; trigger:
112126
<label>
@@ -165,17 +179,13 @@ class Test extends Component {
165179
destroyTooltipOnHide={this.state.destroyTooltipOnHide}
166180
trigger={Object.keys(this.state.trigger)}
167181
onVisibleChange={this.onVisibleChange}
168-
overlay={
169-
<div style={{ height: 50, width: 50 }}>i am a tooltip</div>
170-
}
182+
overlay={<div style={{ height: 50, width: 50 }}>i am a tooltip</div>}
171183
align={{
172184
offset: [this.state.offsetX, this.state.offsetY],
173185
}}
174186
transitionName={this.state.transitionName}
175187
>
176-
<div style={{ height: 100, width: 100, border: '1px solid red' }}>
177-
trigger
178-
</div>
188+
<div style={{ height: 100, width: 100, border: '1px solid red' }}>trigger</div>
179189
</Tooltip>
180190
</div>
181191
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"test": "father test"
3838
},
3939
"dependencies": {
40-
"rc-trigger": "^4.0.0"
40+
"rc-trigger": "^4.2.1"
4141
},
4242
"devDependencies": {
4343
"@types/jest": "^25.1.0",

src/Tooltip.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export interface TooltipProps extends Pick<TriggerProps, 'onPopupAlign' | 'built
2020
mouseEnterDelay?: number;
2121
mouseLeaveDelay?: number;
2222
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
23-
destroyTooltipOnHide?: boolean;
23+
destroyTooltipOnHide?:
24+
| boolean
25+
| {
26+
keepParent?: boolean;
27+
};
2428
align?: AlignType;
2529
arrowContent?: React.ReactNode;
2630
id?: string;
@@ -67,6 +71,16 @@ const Tooltip = (props: TooltipProps, ref) => {
6771
];
6872
};
6973

74+
let destroyTooltip = false;
75+
let autoDestroy = false;
76+
if (typeof destroyTooltipOnHide === 'boolean') {
77+
destroyTooltip = destroyTooltipOnHide;
78+
} else if (destroyTooltipOnHide && typeof destroyTooltipOnHide === 'object') {
79+
const { keepParent } = destroyTooltipOnHide;
80+
destroyTooltip = keepParent === true;
81+
autoDestroy = keepParent === false;
82+
}
83+
7084
return (
7185
<Trigger
7286
popupClassName={overlayClassName}
@@ -83,7 +97,8 @@ const Tooltip = (props: TooltipProps, ref) => {
8397
popupTransitionName={transitionName}
8498
popupAnimation={animation}
8599
defaultPopupVisible={defaultVisible}
86-
destroyPopupOnHide={destroyTooltipOnHide}
100+
destroyPopupOnHide={destroyTooltip}
101+
autoDestroy={autoDestroy}
87102
mouseLeaveDelay={mouseLeaveDelay}
88103
popupStyle={overlayStyle}
89104
mouseEnterDelay={mouseEnterDelay}

tests/index.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,72 @@ describe('rc-tooltip', () => {
6464
expect(domRef.current).toBeTruthy();
6565
});
6666
});
67+
describe('destroyTooltipOnHide', () => {
68+
const destroyVerifyContent = (wrapper, content) => {
69+
wrapper.find('.target').simulate('click');
70+
expect(wrapper.find('.x-content').text()).toBe(content);
71+
expect(
72+
wrapper
73+
.find('Trigger')
74+
.instance()
75+
.getPopupDomNode(),
76+
).toBeTruthy();
77+
wrapper.find('.target').simulate('click');
78+
};
79+
it('default value', () => {
80+
const wrapper = mount(
81+
<Tooltip
82+
trigger={['click']}
83+
placement="left"
84+
overlay={<strong className="x-content">Tooltip content</strong>}
85+
>
86+
<div className="target">Click this</div>
87+
</Tooltip>,
88+
);
89+
wrapper.find('.target').simulate('click');
90+
verifyContent(wrapper, 'Tooltip content');
91+
});
92+
it('should only remove tooltip when value is true', () => {
93+
const wrapper = mount(
94+
<Tooltip
95+
destroyTooltipOnHide
96+
trigger={['click']}
97+
placement="left"
98+
overlay={<strong className="x-content">Tooltip content</strong>}
99+
>
100+
<div className="target">Click this</div>
101+
</Tooltip>,
102+
);
103+
destroyVerifyContent(wrapper, 'Tooltip content');
104+
expect(wrapper.html()).toBe('<div class="target">Click this</div><div></div>');
105+
});
106+
it('should only remove tooltip when keepParent is true', () => {
107+
const wrapper = mount(
108+
<Tooltip
109+
destroyTooltipOnHide={{ keepParent: true }}
110+
trigger={['click']}
111+
placement="left"
112+
overlay={<strong className="x-content">Tooltip content</strong>}
113+
>
114+
<div className="target">Click this</div>
115+
</Tooltip>,
116+
);
117+
destroyVerifyContent(wrapper, 'Tooltip content');
118+
expect(wrapper.html()).toBe('<div class="target">Click this</div><div></div>');
119+
});
120+
it('should remove tooltip and container when keepParent is false', () => {
121+
const wrapper = mount(
122+
<Tooltip
123+
destroyTooltipOnHide={{ keepParent: false }}
124+
trigger={['click']}
125+
placement="left"
126+
overlay={<strong className="x-content">Tooltip content</strong>}
127+
>
128+
<div className="target">Click this</div>
129+
</Tooltip>,
130+
);
131+
destroyVerifyContent(wrapper, 'Tooltip content');
132+
expect(wrapper.html()).toBe('<div class="target">Click this</div>');
133+
});
134+
});
67135
});

0 commit comments

Comments
 (0)