Skip to content
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

[TS migration] Migrate RadioButton to Typescript #31050

Merged
33 changes: 13 additions & 20 deletions src/components/RadioButton.js → src/components/RadioButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PropTypes from 'prop-types';
import React from 'react';
import {View} from 'react-native';
import useTheme from '@styles/themes/useTheme';
Expand All @@ -8,42 +7,38 @@ import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import PressableWithFeedback from './Pressable/PressableWithFeedback';

const propTypes = {
type RadioButtonProps = {
/** Whether radioButton is checked */
isChecked: PropTypes.bool.isRequired,
isChecked: boolean;

/** A function that is called when the box/label is pressed */
onPress: PropTypes.func.isRequired,
onPress: () => void;

/** Specifies the accessibility label for the radio button */
accessibilityLabel: PropTypes.string.isRequired,
accessibilityLabel: string;

/** Should the input be styled for errors */
hasError: PropTypes.bool,
hasError?: boolean;

/** Should the input be disabled */
disabled: PropTypes.bool,
disabled?: boolean;
};

const defaultProps = {
hasError: false,
disabled: false,
};

function RadioButton(props) {
function RadioButton({isChecked, onPress, accessibilityLabel, disabled = false, hasError = false}: RadioButtonProps) {
const theme = useTheme();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Unnecessary reordering of properties compared to the type declaration (hasError could be before disabled).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense!

const styles = useThemeStyles();

return (
<PressableWithFeedback
disabled={props.disabled}
onPress={props.onPress}
disabled={disabled}
onPress={onPress}
hoverDimmingValue={1}
pressDimmingValue={1}
accessibilityLabel={props.accessibilityLabel}
accessibilityLabel={accessibilityLabel}
role={CONST.ACCESSIBILITY_ROLE.RADIO}
>
<View style={[styles.radioButtonContainer, props.hasError && styles.borderColorDanger, props.disabled && styles.cursorDisabled]}>
{props.isChecked && (
<View style={[styles.radioButtonContainer, hasError && styles.borderColorDanger, disabled && styles.cursorDisabled]}>
{isChecked && (
<Icon
src={Expensicons.Checkmark}
fill={theme.checkBox}
Expand All @@ -56,8 +51,6 @@ function RadioButton(props) {
);
}

RadioButton.propTypes = propTypes;
RadioButton.defaultProps = defaultProps;
RadioButton.displayName = 'RadioButton';

Copy link
Contributor

Choose a reason for hiding this comment

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

Please keep the displayName.

RadioButton.displayName = 'RadioButton';

export default RadioButton;
Loading