Skip to content

Commit 584b731

Browse files
authored
feat: support disabled prop (#534)
1 parent 97804a3 commit 584b731

6 files changed

Lines changed: 60 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Then open `http://localhost:8000`.
6969
| `classNames` | `Partial<Record<SemanticName, string>>` | - | Semantic class names for root, arrow, and container nodes. |
7070
| `defaultVisible` | boolean | - | Initial uncontrolled visible state. |
7171
| `destroyOnHidden` | boolean | false | Destroy popup DOM when hidden. |
72+
| `disabled` | boolean | false | Temporarily hide tooltip while preserving visible state. |
7273
| `fresh` | boolean | - | Keep popup content fresh when closed. |
7374
| `getTooltipContainer` | `(node: HTMLElement) => HTMLElement` | - | Resolve popup container. |
7475
| `id` | string | generated id | Tooltip id used for accessibility. |

docs/demo/disabled.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Disabled
3+
order: 9
4+
---
5+
6+
Hover the button to show the Tooltip. Click it to toggle `disabled`.
7+
8+
```jsx
9+
import DisabledDemo from '../examples/disabled';
10+
11+
export default () => <DisabledDemo />;
12+
```

docs/examples/disabled.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { useState } from 'react';
2+
import '../../assets/bootstrap.less';
3+
import Tooltip from '../../src';
4+
5+
const DisabledDemo = () => {
6+
const [disabled, setDisabled] = useState(false);
7+
8+
return (
9+
<div style={{ margin: 100 }}>
10+
<Tooltip disabled={disabled} overlay="Tooltip content" placement="top">
11+
<button type="button" onClick={() => setDisabled((value) => !value)}>
12+
{disabled ? 'Enable Tooltip' : 'Disable Tooltip'}
13+
</button>
14+
</Tooltip>
15+
</div>
16+
);
17+
};
18+
19+
export default DisabledDemo;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"prepare": "husky"
4646
},
4747
"dependencies": {
48-
"@rc-component/trigger": "^3.7.1",
48+
"@rc-component/trigger": "^3.10.0",
4949
"@rc-component/util": "^1.11.1",
5050
"clsx": "^2.1.1"
5151
},

src/Tooltip.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface TooltipProps extends Pick<
1919
TriggerProps,
2020
| 'onPopupAlign'
2121
| 'builtinPlacements'
22+
| 'disabled'
2223
| 'fresh'
2324
| 'mouseLeaveDelay'
2425
| 'mouseEnterDelay'

tests/index.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,32 @@ describe('rc-tooltip', () => {
304304
expect(container.querySelector('.x-content')).toBeTruthy();
305305
});
306306

307+
it('temporarily hides while disabled and restores without mouse leave', () => {
308+
const App = () => {
309+
const [disabled, setDisabled] = React.useState(false);
310+
311+
return (
312+
<Tooltip disabled={disabled} overlay="Tooltip content">
313+
<button type="button" onClick={() => setDisabled((value) => !value)}>
314+
Toggle disabled
315+
</button>
316+
</Tooltip>
317+
);
318+
};
319+
320+
const { container } = render(<App />);
321+
const button = container.querySelector('button');
322+
323+
fireEvent.mouseEnter(button);
324+
expect(container.querySelector('.rc-tooltip')).not.toHaveClass('rc-tooltip-hidden');
325+
326+
fireEvent.click(button);
327+
expect(container.querySelector('.rc-tooltip')).toHaveClass('rc-tooltip-hidden');
328+
329+
fireEvent.click(button);
330+
expect(container.querySelector('.rc-tooltip')).not.toHaveClass('rc-tooltip-hidden');
331+
});
332+
307333
it('ref support nativeElement', () => {
308334
const nodeRef = React.createRef<TooltipRef>();
309335

0 commit comments

Comments
 (0)