Skip to content
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
79 changes: 0 additions & 79 deletions src/components/Badge.js

This file was deleted.

65 changes: 65 additions & 0 deletions src/components/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, {useCallback} from 'react';
import {GestureResponderEvent, PressableStateCallbackType, StyleProp, TextStyle, View, ViewStyle} from 'react-native';
import styles from '@styles/styles';
import * as StyleUtils from '@styles/StyleUtils';
import CONST from '@src/CONST';
import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback';
import Text from './Text';

type BadgeProps = {
/** Is Success type */
success?: boolean;

/** Is Error type */
error?: boolean;

/** Whether badge is clickable */
pressable?: boolean;

/** Text to display in the Badge */
text: string;

/** Text to display in the Badge */
environment?: string;

/** Styles for Badge */
badgeStyles?: StyleProp<ViewStyle>;

/** Styles for Badge Text */
textStyles?: StyleProp<TextStyle>;

/** Callback to be called on onPress */
onPress: (event?: GestureResponderEvent | KeyboardEvent) => void;
};

function Badge({success = false, error = false, pressable = false, text, environment = CONST.ENVIRONMENT.DEV, badgeStyles, textStyles, onPress = () => {}}: BadgeProps) {
const textColorStyles = success || error ? styles.textWhite : undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textColorStyles shouldn't be a style? With this logic is boolean or object style. Maybe worth creating a function and return undefined when the success prop is true? Nested inline conditions can be confused

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be an style object if success or error is true, otherwise it will be undefined, TS is inferring correctly. 🙂

Screenshot 2023-11-13 at 09 48 13

const Wrapper = pressable ? PressableWithoutFeedback : View;

const wrapperStyles: (state: PressableStateCallbackType) => StyleProp<ViewStyle> = useCallback(
({pressed}) => [styles.badge, styles.ml2, StyleUtils.getBadgeColorStyle(success, error, pressed, environment === CONST.ENVIRONMENT.ADHOC), badgeStyles],
[success, error, environment, badgeStyles],
);

return (
<Wrapper
style={pressable ? wrapperStyles : wrapperStyles({focused: false, hovered: false, isDisabled: false, isScreenReaderActive: false, pressed: false})}
onPress={onPress}
role={pressable ? CONST.ACCESSIBILITY_ROLE.BUTTON : CONST.ACCESSIBILITY_ROLE.TEXT}
accessibilityLabel={pressable ? text : undefined}
aria-label={!pressable ? text : undefined}
accessible={false}
>
<Text
style={[styles.badgeText, textColorStyles, textStyles]}
numberOfLines={1}
>
{text}
</Text>
</Wrapper>
);
}

Badge.displayName = 'Badge';

export default Badge;