Skip to content

Fix Badge's size default prop #1212

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

Merged
merged 3 commits into from
Mar 4, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions generatedTypes/components/badge/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export declare type BadgeProps = ViewProps & TouchableOpacityProps & {
/**
* the badge size (default, small)
*/
size: BadgeSizes | number;
size?: BadgeSizes | number;
/**
* Press handler
*/
Expand Down Expand Up @@ -91,10 +91,8 @@ export declare type BadgeProps = ViewProps & TouchableOpacityProps & {
declare class Badge extends PureComponent<BadgeProps> {
styles: ReturnType<typeof createStyles>;
static displayName: string;
static defaultProps: {
size: string;
};
constructor(props: BadgeProps);
get size(): number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large";
getAccessibilityProps(): {
accessible: boolean;
accessibilityRole: string;
Expand Down Expand Up @@ -359,7 +357,7 @@ declare const _default: React.ComponentClass<ViewProps & TouchableOpacityProps &
/**
* the badge size (default, small)
*/
size: number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large";
size?: number | "small" | "default" | "pimpleSmall" | "pimpleBig" | "pimpleHuge" | "large" | undefined;
/**
* Press handler
*/
Expand Down
4 changes: 2 additions & 2 deletions src/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ class Avatar extends PureComponent<AvatarProps> {
return _.get(this.props, 'badgeProps.backgroundColor') || statusColor || onlineColor;
}

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

if (_.isString(badgeSize)) {
return BADGE_SIZES[badgeSize] || BADGE_SIZES[DEFAULT_BADGE_SIZE];
Expand Down
16 changes: 8 additions & 8 deletions src/components/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type BadgeProps = ViewProps &
/**
* the badge size (default, small)
*/
size: BadgeSizes | number;
size?: BadgeSizes | number;
/**
* Press handler
*/
Expand Down Expand Up @@ -117,9 +117,6 @@ class Badge extends PureComponent<BadgeProps> {
styles: ReturnType<typeof createStyles>;

static displayName = 'Badge';
static defaultProps = {
size: 'default'
};

constructor(props: BadgeProps) {
super(props);
Expand All @@ -130,6 +127,10 @@ class Badge extends PureComponent<BadgeProps> {
}
}

get size() {
return this.props.size || 'default';
}

getAccessibilityProps() {
const {onPress, icon, label} = this.props;

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

isSmallBadge() {
const {size} = this.props;
return size === 'small';
return this.size === 'small';
}

getBadgeSizeStyle() {
const {borderWidth, size, icon, customElement} = this.props;
const {borderWidth, icon, customElement} = this.props;
const label = this.getFormattedLabel();
const badgeHeight = _.isNumber(size) ? size : BADGE_SIZES[size];
const badgeHeight = _.isNumber(this.size) ? this.size : BADGE_SIZES[this.size];

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