Skip to content

Commit 320e2b2

Browse files
aojunhao123zombieJ
andauthored
feat: better a11y (#509)
* chore: add @testing-library/jest-dom as a dev dependency and update tsconfig types * feat: improve a11y * fix test * update * update * fix badge urls * only set aria-describedby when tooltip visible * remove unused urls * use renderProps * clean code * chore: adjust * chore: clean up --------- Co-authored-by: 二货机器人 <smith3816@gmail.com>
1 parent f6b1a8f commit 320e2b2

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rc-tooltip
1+
# @rc-component/tooltip
22

33
React Tooltip
44

@@ -9,18 +9,16 @@ React Tooltip
99
[![bundle size][bundlephobia-image]][bundlephobia-url]
1010
[![dumi][dumi-image]][dumi-url]
1111

12-
[npm-image]: http://img.shields.io/npm/v/rc-tooltip.svg?style=flat-square
13-
[npm-url]: http://npmjs.org/package/rc-tooltip
14-
[travis-image]: https://img.shields.io/travis/react-component/tooltip/master?style=flat-square
15-
[travis-url]: https://travis-ci.com/react-component/tooltip
12+
[npm-image]: http://img.shields.io/npm/v/@rc-component/tooltip.svg?style=flat-square
13+
[npm-url]: http://npmjs.org/package/@rc-component/tooltip
1614
[github-actions-image]: https://github.com/react-component/tooltip/actions/workflows/react-component-ci.yml/badge.svg
1715
[github-actions-url]: https://github.com/react-component/tooltip/actions/workflows/react-component-ci.yml
1816
[codecov-image]: https://img.shields.io/codecov/c/github/react-component/tooltip/master.svg?style=flat-square
1917
[codecov-url]: https://app.codecov.io/gh/react-component/tooltip
20-
[download-image]: https://img.shields.io/npm/dm/rc-tooltip.svg?style=flat-square
21-
[download-url]: https://npmjs.org/package/rc-tooltip
22-
[bundlephobia-url]: https://bundlephobia.com/package/rc-tooltip
23-
[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-tooltip
18+
[download-image]: https://img.shields.io/npm/dm/@rc-component/tooltip.svg?style=flat-square
19+
[download-url]: https://npmjs.org/package/@rc-component/tooltip
20+
[bundlephobia-url]: https://bundlephobia.com/package/@rc-component/tooltip
21+
[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/@rc-component/tooltip
2422
[dumi-url]: https://github.com/umijs/dumi
2523
[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square
2624

@@ -36,18 +34,18 @@ React Tooltip
3634

3735
## Install
3836

39-
[![rc-tooltip](https://nodei.co/npm/rc-tooltip.png)](https://npmjs.org/package/rc-tooltip)
37+
[![@rc-component/tooltip](https://nodei.co/npm/@rc-component/tooltip.png)](https://npmjs.org/package/@rc-component/tooltip)
4038

4139
## Usage
4240

4341
```js
44-
var Tooltip = require('rc-tooltip');
42+
var Tooltip = require('@rc-component/tooltip');
4543
var React = require('react');
4644
var ReactDOM = require('react-dom');
4745

4846
// By default, the tooltip has no style.
4947
// Consider importing the stylesheet it comes with:
50-
// 'rc-tooltip/assets/bootstrap_white.css'
48+
// '@rc-component/tooltip/assets/bootstrap_white.css'
5149

5250
ReactDOM.render(
5351
<Tooltip placement="left" trigger={['click']} overlay={<span>tooltip</span>}>
@@ -135,4 +133,4 @@ npm run coverage
135133

136134
## License
137135

138-
`rc-tooltip` is released under the MIT license.
136+
`@rc-component/tooltip` is released under the MIT license.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@
4141
"test": "rc-test"
4242
},
4343
"dependencies": {
44-
"@rc-component/trigger": "^3.6.15",
44+
"@rc-component/trigger": "^3.7.1",
4545
"@rc-component/util": "^1.3.0",
4646
"clsx": "^2.1.1"
4747
},
4848
"devDependencies": {
4949
"@rc-component/father-plugin": "^2.0.1",
5050
"@rc-component/np": "^1.0.3",
51+
"@testing-library/jest-dom": "^6.9.1",
5152
"@testing-library/react": "^16.3.0",
5253
"@types/jest": "^29.4.0",
5354
"@types/node": "^22.15.18",

src/Tooltip.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export interface TooltipProps
1616
| 'onPopupAlign'
1717
| 'builtinPlacements'
1818
| 'fresh'
19-
| 'children'
2019
| 'mouseLeaveDelay'
2120
| 'mouseEnterDelay'
2221
| 'prefixCls'
2322
| 'forceRender'
2423
| 'popupVisible'
2524
> {
25+
children: React.ReactElement;
2626
// Style
2727
classNames?: Partial<Record<SemanticName, string>>;
2828
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
@@ -113,14 +113,12 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
113113
}, [showArrow, classNames?.arrow, styles?.arrow, arrowContent]);
114114

115115
// ======================== Children ========================
116-
const getChildren = () => {
116+
const getChildren: TriggerProps['children'] = ({ open }) => {
117117
const child = React.Children.only(children);
118-
const originalProps = child?.props || {};
119-
const childProps = {
120-
...originalProps,
121-
'aria-describedby': overlay ? mergedId : null,
118+
const ariaProps: React.AriaAttributes = {
119+
'aria-describedby': overlay && open ? mergedId : undefined,
122120
};
123-
return React.cloneElement<any>(children, childProps) as any;
121+
return React.cloneElement(child, ariaProps);
124122
};
125123

126124
// ========================= Render =========================
@@ -158,7 +156,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
158156
uniqueContainerStyle={styles?.uniqueContainer}
159157
{...extraProps}
160158
>
161-
{getChildren()}
159+
{getChildren}
162160
</Trigger>
163161
);
164162
});

tests/index.test.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,28 @@ describe('rc-tooltip', () => {
502502
});
503503

504504
describe('children handling', () => {
505-
it('should pass aria-describedby to child element when overlay exists', () => {
505+
it('should set aria-describedby immediately when defaultVisible is true', () => {
506506
const { container } = render(
507-
<Tooltip id="test-id" overlay="tooltip content">
507+
<Tooltip defaultVisible overlay="tooltip content">
508508
<button>Click me</button>
509509
</Tooltip>,
510510
);
511511

512-
expect(container.querySelector('button')).toHaveAttribute('aria-describedby', 'test-id');
512+
expect(container.querySelector('button')).toHaveAttribute('aria-describedby');
513513
});
514514

515-
it('should not pass aria-describedby when overlay is empty', () => {
516-
const { container } = render(
517-
<Tooltip id="test-id" overlay={null}>
515+
it('should remove aria-describedby when controlled hidden', () => {
516+
const overlay = 'tooltip content';
517+
const { container, rerender } = render(
518+
<Tooltip overlay={overlay} visible>
519+
<button>Click me</button>
520+
</Tooltip>,
521+
);
522+
523+
expect(container.querySelector('button')).toHaveAttribute('aria-describedby');
524+
525+
rerender(
526+
<Tooltip overlay={overlay} visible={false}>
518527
<button>Click me</button>
519528
</Tooltip>,
520529
);

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"declaration": true,
88
"skipLibCheck": true,
99
"esModuleInterop": true,
10+
"types": ["@testing-library/jest-dom"],
1011
"paths": {
1112
"@/*": [
1213
"src/*"

0 commit comments

Comments
 (0)