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

[No QA][TS migration] Migrate 'RadioButtons.js' component to TypeScript #29970

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
import React, {useState} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import RadioButtonWithLabel from './RadioButtonWithLabel';
import styles from '../styles/styles';

const propTypes = {
type Choice = {
label: string;
value: string;
};

type RadioButtonsProps = {
/** List of choices to display via radio buttons */
items: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
}),
).isRequired,
items: Choice[];

/** Callback to fire when selecting a radio button */
onPress: PropTypes.func.isRequired,
onPress: (value: string) => void;
};

function RadioButtons(props) {
function RadioButtons({items, onPress}: RadioButtonsProps) {
const [checkedValue, setCheckedValue] = useState('');

return (
<View>
{_.map(props.items, (item, index) => (
{items.map((item) => (
<RadioButtonWithLabel
key={`${item.label}-${index}`}
key={item.value}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are values always unique?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blazejkustra Since it's radio buttons where only one option can be selected, and selecting logic is based on the item.value isChecked={item.value === checkedValue}, I assume so. Otherwise, there will be two same values in radio buttons, which will cause problems anyway.

isChecked={item.value === checkedValue}
style={[styles.mt4]}
style={styles.mt4}
onPress={() => {
setCheckedValue(item.value);
return props.onPress(item.value);
return onPress(item.value);
}}
label={item.label}
/>
Expand All @@ -39,7 +37,6 @@ function RadioButtons(props) {
);
}

RadioButtons.propTypes = propTypes;
RadioButtons.displayName = 'RadioButtons';

export default RadioButtons;
Loading