Skip to content

Commit eccce40

Browse files
authored
Fix Badge's size default prop (#1212)
* Fix Badge's size default prop * Fix tests
1 parent 5ed6e96 commit eccce40

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

generatedTypes/components/badge/index.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export declare type BadgeProps = ViewProps & TouchableOpacityProps & {
2424
/**
2525
* the badge size (default, small)
2626
*/
27-
size: BadgeSizes | number;
27+
size?: BadgeSizes | number;
2828
/**
2929
* Press handler
3030
*/
@@ -91,10 +91,8 @@ export declare type BadgeProps = ViewProps & TouchableOpacityProps & {
9191
declare class Badge extends PureComponent<BadgeProps> {
9292
styles: ReturnType<typeof createStyles>;
9393
static displayName: string;
94-
static defaultProps: {
95-
size: string;
96-
};
9794
constructor(props: BadgeProps);
95+
get size(): number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large";
9896
getAccessibilityProps(): {
9997
accessible: boolean;
10098
accessibilityRole: string;
@@ -359,7 +357,7 @@ declare const _default: React.ComponentClass<ViewProps & TouchableOpacityProps &
359357
/**
360358
* the badge size (default, small)
361359
*/
362-
size: number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large";
360+
size?: number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large" | undefined;
363361
/**
364362
* Press handler
365363
*/

src/components/avatar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ class Avatar extends PureComponent<AvatarProps> {
232232
return _.get(this.props, 'badgeProps.backgroundColor') || statusColor || onlineColor;
233233
}
234234

235-
getBadgeSize = (): number => {
236-
const badgeSize: BadgeProps['size'] = _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
235+
getBadgeSize = () => {
236+
const badgeSize = this.props?.badgeProps?.size ?? DEFAULT_BADGE_SIZE;
237237

238238
if (_.isString(badgeSize)) {
239239
return BADGE_SIZES[badgeSize] || BADGE_SIZES[DEFAULT_BADGE_SIZE];

src/components/badge/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type BadgeProps = ViewProps &
4848
/**
4949
* the badge size (default, small)
5050
*/
51-
size: BadgeSizes | number;
51+
size?: BadgeSizes | number;
5252
/**
5353
* Press handler
5454
*/
@@ -117,9 +117,6 @@ class Badge extends PureComponent<BadgeProps> {
117117
styles: ReturnType<typeof createStyles>;
118118

119119
static displayName = 'Badge';
120-
static defaultProps = {
121-
size: 'default'
122-
};
123120

124121
constructor(props: BadgeProps) {
125122
super(props);
@@ -130,6 +127,10 @@ class Badge extends PureComponent<BadgeProps> {
130127
}
131128
}
132129

130+
get size() {
131+
return this.props.size || 'default';
132+
}
133+
133134
getAccessibilityProps() {
134135
const {onPress, icon, label} = this.props;
135136

@@ -142,14 +143,13 @@ class Badge extends PureComponent<BadgeProps> {
142143
}
143144

144145
isSmallBadge() {
145-
const {size} = this.props;
146-
return size === 'small';
146+
return this.size === 'small';
147147
}
148148

149149
getBadgeSizeStyle() {
150-
const {borderWidth, size, icon, customElement} = this.props;
150+
const {borderWidth, icon, customElement} = this.props;
151151
const label = this.getFormattedLabel();
152-
const badgeHeight = _.isNumber(size) ? size : BADGE_SIZES[size];
152+
const badgeHeight = _.isNumber(this.size) ? this.size : BADGE_SIZES[this.size];
153153

154154
const style: any = {
155155
paddingHorizontal: this.isSmallBadge() ? 4 : 6,

0 commit comments

Comments
 (0)