Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing of default tooltip formatter #2924

Merged
merged 2 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/component/DefaultTooltipContent.tsx
Expand Up @@ -6,8 +6,8 @@ import React, { PureComponent, CSSProperties, ReactNode } from 'react';
import classNames from 'classnames';
import { isNumOrStr } from '../util/DataUtils';

function defaultFormatter<T>(value: T) {
return _.isArray(value) && isNumOrStr(value[0]) && isNumOrStr(value[1]) ? value.join(' ~ ') : value;
function defaultFormatter<TValue extends ValueType>(value: TValue) {
return _.isArray(value) && isNumOrStr(value[0]) && isNumOrStr(value[1]) ? (value.join(' ~ ') as TValue) : value;
}

export type TooltipType = 'none';
Expand All @@ -19,7 +19,7 @@ export type Formatter<TValue extends ValueType, TName extends NameType> = (
item: Payload<TValue, TName>,
index: number,
payload: Array<Payload<TValue, TName>>,
) => [ReactNode, ReactNode] | ReactNode;
) => [TValue, TName] | TValue;

export interface Payload<TValue extends ValueType, TName extends NameType> {
type?: TooltipType;
Expand Down Expand Up @@ -82,7 +82,7 @@ export class DefaultTooltipContent<TValue extends ValueType, TName extends NameT
if (finalFormatter) {
const formatted = finalFormatter(value, name, entry, i, payload);
if (Array.isArray(formatted)) {
[value, name] = formatted;
[value, name] = formatted as [TValue, TName];
} else {
value = formatted;
}
Expand Down
6 changes: 5 additions & 1 deletion src/component/Tooltip.tsx
Expand Up @@ -258,8 +258,12 @@ export class Tooltip<TValue extends ValueType, TName extends NameType> extends P
});

return (
// ESLint is disabled to allow listening to the `Escape` key. Refer to
// https://github.com/recharts/recharts/pull/2925
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<div
tabIndex={0}
tabIndex={-1}
role="dialog"
onKeyDown={event => {
if (event.key === 'Escape') {
this.setState({
Expand Down