Skip to content

Commit 881a259

Browse files
saulveM-i-k-e-l
andauthored
Migrate Hint component to TypeScript (#1281)
* Move Hint component to TS * Update HintPosition static, move Hint example screen to TS * Remove TODO comment * Remove the use of getThemeProps * PR review changes * Fix any type on refs * update generatedTypes * Apply suggestions from code review Co-authored-by: Miki Leib <[email protected]> * rename import Co-authored-by: Miki Leib <[email protected]>
1 parent 5208133 commit 881a259

File tree

5 files changed

+374
-184
lines changed

5 files changed

+374
-184
lines changed

demo/src/screens/componentScreens/HintsScreen.js renamed to demo/src/screens/componentScreens/HintsScreen.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import React, {Component} from 'react';
22
import {View, Text, Hint, Button, RadioGroup, RadioButton, Switch} from 'react-native-ui-lib'; //eslint-disable-line
33

4-
54
const settingsIcon = require('../../assets/icons/settings.png');
65

7-
export default class HintsScreen extends Component {
8-
constructor(props) {
6+
type HintScreenProps = {};
7+
type HintScreenState = {
8+
showHint: boolean,
9+
useShortMessage: boolean,
10+
showBottomHint: boolean,
11+
showIcon: boolean,
12+
targetPosition: string,
13+
useTargetFrame?: boolean,
14+
useSideTip?: boolean
15+
};
16+
17+
export default class HintsScreen extends Component<HintScreenProps, HintScreenState> {
18+
constructor(props: HintScreenProps) {
919
super(props);
1020
this.state = {
1121
showHint: true,
@@ -14,7 +24,7 @@ export default class HintsScreen extends Component {
1424
showIcon: false,
1525
targetPosition: 'flex-start',
1626
// useTargetFrame: true,
17-
useSideTip: null
27+
useSideTip: false
1828
};
1929
}
2030

@@ -109,7 +119,7 @@ export default class HintsScreen extends Component {
109119
centerV
110120
marginB-20
111121
initialValue={targetPosition}
112-
onValueChange={value => this.setState({targetPosition: value})}
122+
onValueChange={(value: string) => this.setState({targetPosition: value})}
113123
>
114124
<Text marginR-10>Button Position:</Text>
115125
<RadioButton value={'flex-start'} label={'Left'} marginR-10/>
@@ -122,10 +132,9 @@ export default class HintsScreen extends Component {
122132
centerV
123133
marginB-20
124134
initialValue={useSideTip}
125-
onValueChange={value => this.setState({useSideTip: value})}
135+
onValueChange={(value: boolean) => this.setState({useSideTip: value})}
126136
>
127137
<Text marginR-10>Tip:</Text>
128-
<RadioButton value={null} label={'Default'} marginR-10/>
129138
<RadioButton value label={'Side Tip'} marginR-10/>
130139
<RadioButton value={false} label={'Middle Tip'} marginR-10/>
131140
</RadioGroup>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import React, { Component, ReactElement, ElementRef } from 'react';
2+
import { Animated, GestureResponderEvent, ImageSourcePropType, ImageStyle, StyleProp, TextStyle, ViewStyle, LayoutChangeEvent, View as ViewRN } from 'react-native';
3+
declare enum TARGET_POSITIONS {
4+
LEFT = "left",
5+
RIGHT = "right",
6+
CENTER = "center"
7+
}
8+
declare enum HintPositions {
9+
TOP = "top",
10+
BOTTOM = "bottom"
11+
}
12+
interface HintTargetFrame {
13+
x?: number;
14+
y?: number;
15+
width?: number;
16+
height?: number;
17+
}
18+
interface Position {
19+
top?: number;
20+
bottom?: number;
21+
left?: number;
22+
right?: number;
23+
}
24+
interface HintPositionStyle extends Position {
25+
alignItems?: string;
26+
}
27+
interface Paddings {
28+
paddingLeft?: number;
29+
paddingRight?: number;
30+
paddingVertical?: number;
31+
paddingHorizontal?: number;
32+
}
33+
export interface HintProps {
34+
/**
35+
* Control the visibility of the hint
36+
*/
37+
visible?: boolean;
38+
/**
39+
* The hint background color
40+
*/
41+
color?: string;
42+
/**
43+
* The hint message
44+
*/
45+
message?: string | ReactElement;
46+
/**
47+
* The hint message custom style
48+
*/
49+
messageStyle?: StyleProp<TextStyle>;
50+
/**
51+
* Icon to show next to the hint's message
52+
*/
53+
icon?: ImageSourcePropType;
54+
/**
55+
* The icon's style
56+
*/
57+
iconStyle?: StyleProp<ImageStyle>;
58+
/**
59+
* The hint's position
60+
*/
61+
position?: HintPositions;
62+
/**
63+
* Provide custom target position instead of wrapping a child
64+
*/
65+
targetFrame?: HintTargetFrame;
66+
/**
67+
* Show side tips instead of the middle tip
68+
*/
69+
useSideTip?: boolean;
70+
/**
71+
* The hint's border radius
72+
*/
73+
borderRadius?: number;
74+
/**
75+
* Hint margins from screen edges
76+
*/
77+
edgeMargins?: number;
78+
/**
79+
* Hint offset from target
80+
*/
81+
offset?: number;
82+
/**
83+
* Callback for the background press
84+
*/
85+
onBackgroundPress?: (event: GestureResponderEvent) => void;
86+
/**
87+
* The hint container width
88+
*/
89+
containerWidth?: number;
90+
/**
91+
* The hint's test identifier
92+
*/
93+
testID?: string;
94+
/**
95+
* Additional styling
96+
*/
97+
style?: StyleProp<ViewStyle>;
98+
}
99+
interface HintState {
100+
targetLayout?: HintTargetFrame;
101+
targetLayoutInWindow?: HintTargetFrame;
102+
}
103+
/**
104+
* @description: Hint component for displaying a tooltip over wrapped component
105+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/HintsScreen.js
106+
* @notes: You can either wrap a component or pass a specific targetFrame
107+
*/
108+
declare class Hint extends Component<HintProps, HintState> {
109+
static displayName: string;
110+
static defaultProps: {
111+
position: HintPositions;
112+
};
113+
static positions: typeof HintPositions;
114+
targetRef: ElementRef<typeof ViewRN> | null;
115+
hintRef: ElementRef<typeof ViewRN> | null;
116+
state: {
117+
targetLayoutInWindow: undefined;
118+
targetLayout: HintTargetFrame | undefined;
119+
};
120+
visibleAnimated: Animated.Value;
121+
componentDidUpdate(prevProps: HintProps): void;
122+
focusAccessibilityOnHint: () => void;
123+
setTargetRef: (ref: ElementRef<typeof ViewRN>) => void;
124+
setHintRef: (ref: ElementRef<typeof ViewRN>) => void;
125+
onTargetLayout: ({ nativeEvent: { layout } }: LayoutChangeEvent) => void;
126+
getAccessibilityInfo(): {
127+
accessible: boolean;
128+
accessibilityLabel: string;
129+
} | undefined;
130+
get containerWidth(): number;
131+
get targetLayout(): HintTargetFrame | undefined;
132+
get showHint(): boolean;
133+
get tipSize(): {
134+
width: number;
135+
height: number;
136+
};
137+
get hintOffset(): number;
138+
get edgeMargins(): number;
139+
get useSideTip(): boolean;
140+
getTargetPositionOnScreen(): TARGET_POSITIONS;
141+
getContainerPosition(): {
142+
top: number | undefined;
143+
left: number | undefined;
144+
} | undefined;
145+
getHintPosition(): HintPositionStyle;
146+
getHintPadding(): Paddings;
147+
getHintAnimatedStyle: () => {
148+
opacity: Animated.Value;
149+
transform: {
150+
translateY: Animated.AnimatedInterpolation;
151+
}[];
152+
};
153+
getTipPosition(): Position;
154+
renderHintTip(): JSX.Element;
155+
renderHint(): JSX.Element | undefined;
156+
renderHintContainer(): JSX.Element;
157+
renderChildren(): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, any> | null) | (new (props: any) => React.Component<any, any, any>)> | undefined;
158+
render(): {} | null | undefined;
159+
}
160+
declare const _default: React.ComponentClass<HintProps & {
161+
useCustomTheme?: boolean | undefined;
162+
}, any> & typeof Hint;
163+
export default _default;

0 commit comments

Comments
 (0)