Skip to content

Commit

Permalink
fix(CheckboxItem): properly disable touchable (#3077)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunohkbx authored Mar 23, 2022
1 parent 2876bce commit 807179a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions example/src/Examples/CheckboxItemExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const CheckboxExample = () => {
const [checkedIOS, setCheckedIOS] = React.useState<boolean>(true);
const [checkedLeadingControl, setCheckedLeadingControl] =
React.useState<boolean>(true);
const [checkedDisabled, setCheckedDisabled] = React.useState<boolean>(true);
return (
<ScreenWrapper style={styles.container}>
<Checkbox.Item
Expand All @@ -35,6 +36,12 @@ const CheckboxExample = () => {
mode="ios"
position="leading"
/>
<Checkbox.Item
label="Disabled checkbox"
status={checkedDisabled ? 'checked' : 'unchecked'}
onPress={() => setCheckedDisabled(!checkedDisabled)}
disabled
/>
</ScreenWrapper>
);
};
Expand Down
7 changes: 5 additions & 2 deletions src/components/Checkbox/CheckboxItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ const CheckboxItem = ({
testID,
mode,
position = 'trailing',
disabled,
...props
}: Props) => {
const checkboxProps = { ...props, status, theme };
const checkboxProps = { ...props, status, theme, disabled };
const isLeading = position === 'leading';
let checkbox;

Expand All @@ -116,9 +117,11 @@ const CheckboxItem = ({
accessibilityRole="checkbox"
accessibilityState={{
checked: status === 'checked',
disabled,
}}
onPress={onPress}
testID={testID}
disabled={disabled}
>
<View
style={[styles.container, style]}
Expand All @@ -130,7 +133,7 @@ const CheckboxItem = ({
style={[
styles.label,
{
color: theme.colors.text,
color: disabled ? theme.colors.disabled : theme.colors.text,
textAlign: isLeading ? 'right' : 'left',
},
labelStyle,
Expand Down
12 changes: 12 additions & 0 deletions src/components/__tests__/Checkbox/CheckboxItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ it('should have `accessibilityState={ checked: false }` when `status="indetermin
const element = getByA11yState({ checked: false });
expect(element).toBeTruthy();
});

it('disables the row when the prop disabled is true', () => {
const { getByA11yLabel } = render(
<Checkbox.Item accessibilityLabel="some checkbox" disabled />
);

const touchable = getByA11yLabel('some checkbox');

expect(touchable.props).toMatchObject({
accessibilityState: { disabled: true },
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`can render leading checkbox control 1`] = `
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
Expand Down Expand Up @@ -139,6 +140,7 @@ exports[`can render the Android checkbox on different platforms 1`] = `
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
Expand Down Expand Up @@ -305,6 +307,7 @@ exports[`can render the iOS checkbox on different platforms 1`] = `
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
Expand Down Expand Up @@ -437,6 +440,7 @@ exports[`renders unchecked 1`] = `
accessibilityState={
Object {
"checked": false,
"disabled": undefined,
}
}
accessible={true}
Expand Down

0 comments on commit 807179a

Please sign in to comment.