Skip to content

Commit

Permalink
refactor: correct disabling RadioButtonItem, add missing a11y props (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak authored Jun 9, 2022
1 parent bd9f012 commit 83b70da
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
8 changes: 8 additions & 0 deletions example/src/Examples/RadioButtonItemExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const RadioButtonItemExample = () => {
const [checkedIOS, setCheckedIOS] = React.useState(true);
const [checkedLeadingControl, setCheckedLeadingControl] =
React.useState(true);
const [checkedDisabled, setCheckedDisabled] = React.useState<boolean>(true);
const [checkedLabelVariant, setCheckedLabelVariant] = React.useState(true);

const { isV3 } = useTheme();
Expand Down Expand Up @@ -42,6 +43,13 @@ const RadioButtonItemExample = () => {
value="iOS"
position="leading"
/>
<RadioButton.Item
label="Disabled checkbox"
status={checkedDisabled ? 'checked' : 'unchecked'}
onPress={() => setCheckedDisabled(!checkedDisabled)}
value="iOS"
disabled
/>
{isV3 && (
<RadioButton.Item
label="Default with titleLarge title variant"
Expand Down
7 changes: 6 additions & 1 deletion src/components/Checkbox/CheckboxItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type Props = {
* Function to execute on press.
*/
onPress?: () => void;
/**
* Accessibility label for the touchable. This is read by the screen reader when the user taps the touchable.
*/
accessibilityLabel?: string;
/**
* Custom color for unchecked checkbox.
*/
Expand Down Expand Up @@ -114,6 +118,7 @@ const CheckboxItem = ({
testID,
mode,
position = 'trailing',
accessibilityLabel = label,
disabled,
labelVariant = 'bodyLarge',
...props
Expand Down Expand Up @@ -143,7 +148,7 @@ const CheckboxItem = ({

return (
<TouchableRipple
accessibilityLabel={label}
accessibilityLabel={accessibilityLabel}
accessibilityRole="checkbox"
accessibilityState={{
checked: status === 'checked',
Expand Down
36 changes: 24 additions & 12 deletions src/components/RadioButton/RadioButtonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'react-native';
import { withTheme } from '../../core/theming';
import { RadioButtonContext, RadioButtonContextType } from './RadioButtonGroup';
import { handlePress } from './utils';
import { handlePress, isChecked } from './utils';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import RadioButton from './RadioButton';
import Text from '../Typography/Text';
Expand Down Expand Up @@ -133,7 +133,7 @@ const RadioButtonItem = ({
uncheckedColor,
status,
theme,
accessibilityLabel,
accessibilityLabel = label,
testID,
mode,
position = 'trailing',
Expand All @@ -152,30 +152,42 @@ const RadioButtonItem = ({
}

const textColor = theme.isV3 ? theme.colors.onSurface : theme.colors.text;
const disabledTextColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme.colors.disabled;
const textAlign = isLeading ? 'right' : 'left';

const computedStyle = {
color: textColor,
color: disabled ? disabledTextColor : textColor,
textAlign,
} as TextStyle;

return (
<RadioButtonContext.Consumer>
{(context?: RadioButtonContextType) => {
const checked =
isChecked({
contextValue: context?.value,
status,
value,
}) === 'checked';
return (
<TouchableRipple
onPress={
disabled
? undefined
: () =>
handlePress({
onPress: onPress,
onValueChange: context?.onValueChange,
value,
})
onPress={() =>
handlePress({
onPress: onPress,
onValueChange: context?.onValueChange,
value,
})
}
accessibilityLabel={accessibilityLabel}
accessibilityRole="radio"
accessibilityState={{
checked,
disabled,
}}
testID={testID}
disabled={disabled}
>
<View style={[styles.container, style]} pointerEvents="none">
{isLeading && radioButton}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

exports[`can render leading radio button control 1`] = `
<View
accessibilityLabel="Default with leading control"
accessibilityRole="radio"
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
Expand Down Expand Up @@ -128,6 +136,14 @@ exports[`can render leading radio button control 1`] = `

exports[`can render the Android radio button on different platforms 1`] = `
<View
accessibilityLabel="iOS Checkbox"
accessibilityRole="radio"
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
Expand Down Expand Up @@ -237,6 +253,14 @@ exports[`can render the Android radio button on different platforms 1`] = `

exports[`can render the iOS radio button on different platforms 1`] = `
<View
accessibilityLabel="iOS Radio button"
accessibilityRole="radio"
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
Expand Down Expand Up @@ -363,6 +387,14 @@ exports[`can render the iOS radio button on different platforms 1`] = `

exports[`renders unchecked 1`] = `
<View
accessibilityLabel="Unchecked Button"
accessibilityRole="radio"
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
Expand Down

0 comments on commit 83b70da

Please sign in to comment.