Skip to content

Commit f552f0b

Browse files
committed
chore(props): improve types
1 parent 0e377af commit f552f0b

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
"clean": "rimraf dist",
1919
"copyfiles": "copyfiles ./src/**/*.scss out-tsc/",
2020
"build:rollup": "tsc && yarn copyfiles && yarn rollup -c",
21-
"build": "npm-run-all clean copy:types build:rollup",
21+
"build": "npm-run-all clean build:rollup copy:types",
2222
"build:watch": "rollup -c -w",
2323
"start": "npm-run-all clean build:watch",
2424
"gh-pages": "gh-pages -d example",
2525
"github-changes": "github-changes -o wwayne -r react-tooltip -a --only-pulls --use-commit-body",
26-
"copy:types": "cpy ./react-tooltip.d.ts ./dist --parents --dot",
26+
"copy:types": "copyfiles ./out-tsc/src/**/*.d.ts dist/ --up 2",
2727
"cm": "git cz",
2828
"semantic-release": "semantic-release"
2929
},

src/ReactTooltip.tsx

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { generateUUID } from './utils/uuid';
1818
import baseCss from './index.scss';
1919
import { generateTooltipStyle } from './decorators/styler';
2020
import { BodyModeListener, CustomColor } from './types';
21-
import { ReactTooltipProps } from './ReactTooltipProps';
21+
import { Effect, Offset, Place, TooltipProps, Wrapper } from './ReactTooltipProps';
2222
import { checkStatus } from './decorators/customEvent';
2323

2424
const dispatchGlobalEvent = (eventName, opts) => {
@@ -61,14 +61,14 @@ const customListeners = {
6161

6262
type TooltipState = {
6363
uuid: string;
64-
place: string;
65-
desiredPlace: string;
64+
place: Place;
65+
desiredPlace: Place;
6666
type: string;
67-
effect: string;
67+
effect: Effect;
6868
show: boolean;
6969
border: boolean;
7070
customColors: CustomColor;
71-
offset: string | object;
71+
offset: Offset;
7272
padding: string;
7373
extraClass: string;
7474
html: boolean;
@@ -94,7 +94,7 @@ type GetContentFn = {
9494

9595
export type GetContentTypes = GetContentFn | string;
9696

97-
class ReactTooltip extends React.Component<ReactTooltipProps, TooltipState> {
97+
class ReactTooltip extends React.Component<TooltipProps, TooltipState> {
9898
mount: boolean;
9999
delayShowLoop?: NodeJS.Timeout;
100100
delayHideLoop?: NodeJS.Timeout;
@@ -149,15 +149,15 @@ class ReactTooltip extends React.Component<ReactTooltipProps, TooltipState> {
149149
static defaultProps = {
150150
insecure: true,
151151
resizeHide: true,
152-
wrapper: 'div',
152+
wrapper: 'div' as Wrapper,
153153
clickable: false
154154
};
155155

156156
static supportedWrappers = ['div', 'span'];
157157

158158
static displayName = 'ReactTooltip';
159159

160-
constructor(props: ReactTooltipProps) {
160+
constructor(props: TooltipProps) {
161161
super(props);
162162

163163
this.state = {
@@ -319,8 +319,8 @@ class ReactTooltip extends React.Component<ReactTooltipProps, TooltipState> {
319319
}
320320
}
321321

322-
getEffect(currentTarget: HTMLElement) {
323-
const dataEffect = currentTarget.getAttribute('data-effect');
322+
getEffect(currentTarget: HTMLElement): Effect {
323+
const dataEffect = currentTarget.getAttribute('data-effect') as Effect;
324324
return dataEffect || this.props.effect || 'float';
325325
}
326326

@@ -707,11 +707,11 @@ class ReactTooltip extends React.Component<ReactTooltipProps, TooltipState> {
707707

708708
// Make sure the correct place is set
709709
const desiredPlace =
710-
e.currentTarget.getAttribute('data-place') || this.props.place || 'top';
711-
const effect =
712-
(switchToSolid && 'solid') || this.getEffect(e.currentTarget);
710+
e.currentTarget.getAttribute('data-place') as Place || this.props.place || 'top';
711+
const effect: Effect =
712+
(switchToSolid && 'solid') as Effect || this.getEffect(e.currentTarget) as Effect;
713713
const offset =
714-
e.currentTarget.getAttribute('data-offset') || this.props.offset || {};
714+
e.currentTarget.getAttribute('data-offset') as Offset || this.props.offset || {};
715715
const result = getPosition(
716716
e,
717717
e.currentTarget,

src/ReactTooltipProps.tsx

+48-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
import React, { ReactNode } from 'react';
22
import { GetContentTypes } from './ReactTooltip';
33

4-
export type ReactTooltipProps = {
4+
export interface Offset {
5+
top?: number;
6+
right?: number;
7+
left?: number;
8+
bottom?: number;
9+
}
10+
11+
export type EventFunc = (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
12+
13+
export type Effect = 'float' | 'solid';
14+
export type Place = 'top' | 'right' | 'bottom' | 'left';
15+
export type Type = 'dark' | 'success' | 'warning' | 'error' | 'info' | 'light';
16+
export type Wrapper = 'div' | 'span';
17+
18+
export type TooltipProps = {
519
uuid: string;
620
children: ReactNode;
7-
place: string;
8-
type: string;
9-
effect: string;
10-
offset: object;
21+
// Placement of tooltip
22+
place?: Place;
23+
// Tooltip styling theme
24+
type: Type;
25+
// Behavior of tooltip
26+
effect: Effect;
27+
// Global tooltip offset, e.g., offset={{ top: 10, left: 5 }}
28+
offset?: Offset;
1129
padding: string;
12-
multiline: boolean;
30+
multiline?: boolean;
1331
border: boolean;
1432
textColor: string;
1533
backgroundColor: string;
@@ -28,15 +46,28 @@ export type ReactTooltipProps = {
2846
isCapture: boolean;
2947
globalEventOff: string;
3048
getContent: GetContentTypes | GetContentTypes[];
31-
afterShow(e: React.MouseEvent<HTMLElement, MouseEvent>);
32-
afterHide(e: React.MouseEvent<HTMLElement, MouseEvent>);
33-
overridePosition(a, b, c, d, e, f, g, h);
34-
disable: boolean;
35-
scrollHide: boolean;
36-
resizeHide: boolean;
37-
wrapper: string;
38-
bodyMode: boolean;
39-
possibleCustomEvents: string;
40-
possibleCustomEventsOff: string;
41-
clickable: boolean;
49+
afterShow: EventFunc;
50+
afterHide: EventFunc;
51+
overridePosition?(
52+
position: { left: number; top: number },
53+
currentEvent: React.MouseEvent<HTMLElement, MouseEvent>,
54+
currentTarget: EventTarget,
55+
// node is the ref argument, and the wrapper
56+
// is restricted to: div | span
57+
refNode: null | HTMLDivElement | HTMLSpanElement,
58+
place: Place,
59+
desiredPlace: Place,
60+
effect: Effect,
61+
offset: Offset
62+
): { left: number; top: number };
63+
disable?: boolean;
64+
scrollHide?: boolean;
65+
resizeHide?: boolean;
66+
wrapper?: Wrapper;
67+
bodyMode?: boolean;
68+
possibleCustomEvents?: string;
69+
possibleCustomEventsOff?: string;
70+
clickable?: boolean;
71+
// Aria role for the tooltip
72+
role?: string;
4273
};

src/utils/aria.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ReactTooltipProps } from '../ReactTooltipProps';
1+
import { TooltipProps } from '../ReactTooltipProps';
22

33
/**
44
* Support aria- and role in ReactTooltip
55
*
66
* @params props {Object}
77
* @return {Object}
88
*/
9-
export function parseAria(props: ReactTooltipProps): object {
9+
export function parseAria(props: TooltipProps): object {
1010
const ariaObj = {};
1111
Object.keys(props)
1212
.filter((prop) => {

0 commit comments

Comments
 (0)