Skip to content

Commit 2b32efb

Browse files
committed
feat: support disabled prop
1 parent 8e70761 commit 2b32efb

6 files changed

Lines changed: 69 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Online demo: https://tooltip-react-component.vercel.app/
7171
| trigger | string \| string\[] | 'hover' | which actions cause tooltip shown. enum of 'hover','click','focus' |
7272
| visible | boolean | false | whether tooltip is visible |
7373
| defaultVisible | boolean | false | whether tooltip is visible by default |
74+
| disabled | boolean | false | temporarily hide tooltip without resetting its current visible state |
7475
| placement | string | 'right' | tooltip placement. enum of 'top','left','right','bottom', 'topLeft', 'topRight', 'bottomLeft', 'bottomRight', 'leftTop', 'leftBottom', 'rightTop', 'rightBottom' |
7576
| motion | object | | Config popup motion. Please ref demo for example |
7677
| onVisibleChange | (visible: boolean) => void; | | Callback when visible change |

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"test": "rc-test"
4242
},
4343
"dependencies": {
44-
"@rc-component/trigger": "^3.7.1",
45-
"@rc-component/util": "^1.3.0",
44+
"@rc-component/trigger": "^3.10.0",
45+
"@rc-component/util": "^1.11.0",
4646
"clsx": "^2.1.1"
4747
},
4848
"devDependencies": {

src/Tooltip.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import type { ArrowType, TriggerProps, TriggerRef } from '@rc-component/trigger';
1+
import type {
2+
ActionType,
3+
AlignType,
4+
ArrowType,
5+
TriggerProps,
6+
TriggerRef,
7+
} from '@rc-component/trigger';
28
import Trigger from '@rc-component/trigger';
3-
import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface';
4-
import useId from '@rc-component/util/lib/hooks/useId';
9+
import { useId } from '@rc-component/util';
510
import { clsx } from 'clsx';
611
import * as React from 'react';
712
import { useImperativeHandle, useRef } from 'react';
@@ -15,6 +20,7 @@ export interface TooltipProps
1520
TriggerProps,
1621
| 'onPopupAlign'
1722
| 'builtinPlacements'
23+
| 'disabled'
1824
| 'fresh'
1925
| 'mouseLeaveDelay'
2026
| '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)