Skip to content

Commit b0bc97c

Browse files
authored
Migrate Badge to TS (#937)
* Migrate Badge to TS * Add extractAnimationProps to modifiers * Adding extractAnimationProps to modifiers * Update types post Badge TS migration * Fix tests * Use BadgeProps
1 parent 7efaccb commit b0bc97c

File tree

13 files changed

+559
-121
lines changed

13 files changed

+559
-121
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class BadgesScreen extends Component {
3333
</Text>
3434
<View row center style={{alignItems: 'flex-start'}}>
3535
<View center paddingH-10>
36-
<Badge label={this.state.value.toString()} backgroundColor={Colors.red30}/>
36+
<Badge size={'default'} label={this.state.value.toString()} backgroundColor={Colors.red30}/>
3737
<Badge
3838
label={this.state.value.toString()}
3939
containerStyle={{marginTop: BadgesSpace}}
@@ -133,7 +133,7 @@ export default class BadgesScreen extends Component {
133133
<View style={styles.iconBadgeColumnContainer}>
134134
<Badge icon={star} borderRadius={6} iconStyle={{backgroundColor: Colors.red30}}/>
135135
<Text text80 style={{marginTop: 10}}>
136-
border radius (6)
136+
border radius
137137
</Text>
138138
</View>
139139
</View>

generatedTypes/commons/modifiers.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export declare function extractPositionStyle(props: Dictionary<any>): {
8585
} | undefined;
8686
export declare function extractFlexStyle(props: Dictionary<any>): Partial<Record<NativeFlexModifierKeyType, number>> | undefined;
8787
export declare function extractAccessibilityProps(props?: any): Partial<any>;
88+
export declare function extractAnimationProps(props?: any): Pick<any, "onAnimationEnd" | "animation" | "duration" | "delay" | "direction" | "easing" | "iterationCount" | "transition" | "onAnimationBegin" | "useNativeDriver">;
8889
export declare function extractBorderRadiusValue(props: Dictionary<any>): number | undefined;
8990
export declare function extractModifierProps(props: Dictionary<any>): _.Dictionary<any>;
9091
export declare function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]): Pick<Partial<Dictionary<any>>, number>;

generatedTypes/components/avatar/index.d.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { PureComponent } from 'react';
22
import { ImageSourcePropType, StyleProp, ViewStyle, ImagePropsBase, ImageStyle, TextStyle } from 'react-native';
3+
import { BadgeProps } from '../badge';
34
import { ImageProps } from '../image';
45
export declare enum StatusModes {
56
ONLINE = "ONLINE",
@@ -29,7 +30,7 @@ export declare type AvatarPropTypes = {
2930
/**
3031
* Badge props passed down to Badge component
3132
*/
32-
badgeProps?: object;
33+
badgeProps?: BadgeProps;
3334
/**
3435
* Additional spacing styles for the container
3536
*/
@@ -134,11 +135,8 @@ declare class Avatar extends PureComponent<AvatarPropTypes> {
134135
getStatusBadgeColor(status: StatusModes | undefined): string | null;
135136
getBadgeBorderWidth: () => any;
136137
getBadgeColor(): any;
137-
getBadgeSize: () => any;
138-
getBadgePosition(): {
139-
[x: string]: string | number;
140-
position: string;
141-
};
138+
getBadgeSize: () => number;
139+
getBadgePosition: () => object;
142140
renderBadge(): JSX.Element | undefined;
143141
renderRibbon(): JSX.Element | undefined;
144142
renderImage(): JSX.Element | undefined;

generatedTypes/components/badge/index.d.ts

Lines changed: 412 additions & 0 deletions
Large diffs are not rendered by default.

generatedTypes/components/chip/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { StyleProp, ViewStyle, ViewProps, TouchableOpacityProps, ImageStyle, ImageProps, TextStyle, ImageSourcePropType } from 'react-native';
3-
import { BadgeProps } from 'typings';
43
import { AvatarPropTypes } from '../avatar';
4+
import { BadgeProps } from '../badge';
55
export declare type ChipPropTypes = ViewProps & TouchableOpacityProps & {
66
/**
77
* Chip's size. Number or a width and height object.

generatedTypes/components/tabBar/TabBarItem.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ declare const _default: React.ComponentClass<BaseComponentInjectedProps & {
9494
/**
9595
* Badge component props to display next the item label
9696
*/
97-
badge?: any;
97+
badge?: BadgeProps | undefined;
9898
/**
9999
* maximum number of lines the label can break
100100
*/

generatedTypes/components/tabBar/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ declare const _default: React.ComponentClass<BaseComponentInjectedProps & ViewPr
8989
iconSelectedColor?: string | undefined;
9090
label?: string | undefined;
9191
labelStyle?: import("react-native").TextStyle | undefined;
92-
badge?: any;
92+
badge?: import("../badge").BadgeProps | undefined;
9393
maxLines?: number | undefined;
9494
selectedLabelStyle: import("react-native").TextStyle;
9595
selected?: boolean | undefined;

src/commons/modifiers.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ export function extractAccessibilityProps(props: any = this.props) {
260260
});
261261
}
262262

263+
//@ts-ignore
264+
export function extractAnimationProps(props: any = this.props) {
265+
return _.pick(props, [
266+
'animation',
267+
'duration',
268+
'delay',
269+
'direction',
270+
'easing',
271+
'iterationCount',
272+
'transition',
273+
'onAnimationBegin',
274+
'onAnimationEnd',
275+
'useNativeDriver'
276+
]);
277+
}
278+
263279
export function extractBorderRadiusValue(props: Dictionary<any>) {
264280
let borderRadius;
265281

@@ -367,7 +383,7 @@ export function generateModifiersStyle(options = {
367383
return style;
368384
// clean empty objects and undefined
369385
// (!) This change is currently breaking UI layout for some reason - worth investigating
370-
// return _.omitBy(style, value => _.isUndefined(value) || (_.isPlainObject(value) && _.isEmpty(value)));
386+
// return _.omitBy(style, value => _.isUndefined(value) || (_.isPlainObject(value) && _.isEmpty(value)));
371387
}
372388

373389
export function getAlteredModifiersOptions(currentProps: any, nextProps: any) {

src/components/avatar/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Colors} from '../../style';
55
import {forwardRef, asBaseComponent} from '../../commons/new';
66
import {extractAccessibilityProps} from '../../commons/modifiers';
77
//@ts-ignore
8-
import Badge, {BADGE_SIZES} from '../badge';
8+
import Badge, {BadgeProps, BADGE_SIZES} from '../badge';
99
import View from '../view';
1010
import Text from '../text';
1111
import Image, {ImageProps} from '../image';
@@ -50,7 +50,7 @@ export type AvatarPropTypes = {
5050
/**
5151
* Badge props passed down to Badge component
5252
*/
53-
badgeProps?: object;
53+
badgeProps?: BadgeProps;
5454
/**
5555
* Additional spacing styles for the container
5656
*/
@@ -222,16 +222,16 @@ class Avatar extends PureComponent<AvatarPropTypes> {
222222
return _.get(this.props, 'badgeProps.backgroundColor') || statusColor || onlineColor;
223223
}
224224

225-
getBadgeSize = () => {
226-
const badgeSize = _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
225+
getBadgeSize = (): number => {
226+
const badgeSize: BadgeProps['size'] = _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
227227

228228
if (_.isString(badgeSize)) {
229229
return BADGE_SIZES[badgeSize] || BADGE_SIZES[DEFAULT_BADGE_SIZE];
230230
}
231231
return badgeSize;
232232
}
233233

234-
getBadgePosition() {
234+
getBadgePosition = (): object => {
235235
const {size, badgePosition} = this.props;
236236
const radius = size / 2;
237237
const x = Math.sqrt(radius ** 2 * 2);

src/components/badge/__tests__/index.specs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Badge from '../index';
1+
import {Badge} from '../index';
22

33
describe('Badge Label', () => {
44
it('Should return the label sent (unformatted)', () => {

0 commit comments

Comments
 (0)