Skip to content

Commit 1e65c5f

Browse files
committed
fix: avoid disabled accessibility state for active chips
1 parent 1b2b043 commit 1e65c5f

3 files changed

Lines changed: 47 additions & 20 deletions

File tree

src/components/Chip/Chip.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ const Chip = ({
213213
onPressIn,
214214
onPressOut,
215215
});
216+
const touchableOnPress =
217+
!disabled && !hasPassedTouchHandler ? () => {} : onPress;
216218

217219
const isOutlined = mode === 'outlined';
218220

@@ -300,7 +302,7 @@ const Chip = ({
300302
borderless
301303
background={background}
302304
style={[{ borderRadius }, styles.touchable]}
303-
onPress={onPress}
305+
onPress={touchableOnPress}
304306
onLongPress={onLongPress}
305307
onPressIn={hasPassedTouchHandler ? handlePressIn : undefined}
306308
onPressOut={hasPassedTouchHandler ? handlePressOut : undefined}

src/components/__tests__/Chip.test.tsx

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Animated } from 'react-native';
1+
import { Animated, StyleSheet } from 'react-native';
22

3-
import { describe, expect, it, jest } from '@jest/globals';
3+
import { describe, it, jest } from '@jest/globals';
44
import { act } from '@testing-library/react-native';
55
import color from 'color';
66

@@ -12,6 +12,15 @@ import { getChipColors } from '../Chip/helpers';
1212

1313
const stateOpacity = tokens.md.sys.state.opacity;
1414

15+
const getScaleValue = (testID: string) => {
16+
const style = StyleSheet.flatten(screen.getByTestId(testID).props.style);
17+
const scale = style.transform?.find(
18+
(transform: { scale?: Animated.Value | number }) => 'scale' in transform
19+
)?.scale;
20+
21+
return typeof scale === 'number' ? scale : scale?.__getValue();
22+
};
23+
1524
it('renders chip with onPress', async () => {
1625
const tree = (
1726
await render(<Chip onPress={() => {}}>Example Chip</Chip>)
@@ -70,10 +79,26 @@ it('renders selected chip', async () => {
7079
expect(tree).toMatchSnapshot();
7180
});
7281

73-
it('renders disabled chip if there is no touch handler passed', async () => {
74-
await render(<Chip testID="disabled-chip">Disabled chip</Chip>);
82+
it('does not mark chip disabled if there is no touch handler passed', async () => {
83+
await render(<Chip testID="informational-chip">Informational chip</Chip>);
7584

76-
expect(screen.getByTestId('disabled-chip')).toBeDisabled();
85+
expect(
86+
screen.getByTestId('informational-chip').props.accessibilityState
87+
).toMatchObject({
88+
disabled: false,
89+
});
90+
});
91+
92+
it('marks chip disabled when disabled prop is passed', async () => {
93+
await render(
94+
<Chip disabled testID="disabled-chip">
95+
Disabled chip
96+
</Chip>
97+
);
98+
99+
expect(screen.getByTestId('disabled-chip').props.accessibilityState).toMatchObject({
100+
disabled: true,
101+
});
77102
});
78103

79104
it('renders active chip if only onLongPress handler is passed', async () => {
@@ -83,7 +108,9 @@ it('renders active chip if only onLongPress handler is passed', async () => {
83108
</Chip>
84109
);
85110

86-
expect(screen.getByTestId('active-chip')).toBeEnabled();
111+
expect(screen.getByTestId('active-chip').props.accessibilityState).toMatchObject({
112+
disabled: false,
113+
});
87114
});
88115

89116
it('renders chip with zero border radius', async () => {
@@ -93,9 +120,11 @@ it('renders chip with zero border radius', async () => {
93120
</Chip>
94121
);
95122

96-
expect(screen.getByTestId('active-chip')).toHaveStyle({
97-
borderRadius: 0,
98-
});
123+
expect(StyleSheet.flatten(screen.getByTestId('active-chip').props.style)).toMatchObject(
124+
{
125+
borderRadius: 0,
126+
}
127+
);
99128
});
100129

101130
describe('getChipColors - text color', () => {
@@ -385,9 +414,7 @@ it('animated value changes correctly', async () => {
385414
Example Chip
386415
</Chip>
387416
);
388-
expect(screen.getByTestId('chip-container-outer-layer')).toHaveStyle({
389-
transform: [{ scale: 1 }],
390-
});
417+
expect(getScaleValue('chip-container-outer-layer')).toBe(1);
391418

392419
Animated.timing(value, {
393420
toValue: 1.5,
@@ -398,7 +425,5 @@ it('animated value changes correctly', async () => {
398425
await act(() => {
399426
jest.advanceTimersByTime(200);
400427
});
401-
expect(screen.getByTestId('chip-container-outer-layer')).toHaveStyle({
402-
transform: [{ scale: 1.5 }],
403-
});
428+
expect(getScaleValue('chip-container-outer-layer')).toBe(1.5);
404429
});

src/components/__tests__/__snapshots__/Chip.test.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports[`renders chip with close button 1`] = `
4545
{
4646
"busy": undefined,
4747
"checked": undefined,
48-
"disabled": true,
48+
"disabled": false,
4949
"expanded": undefined,
5050
"selected": false,
5151
}
@@ -343,7 +343,7 @@ exports[`renders chip with custom close button 1`] = `
343343
{
344344
"busy": undefined,
345345
"checked": undefined,
346-
"disabled": true,
346+
"disabled": false,
347347
"expanded": undefined,
348348
"selected": false,
349349
}
@@ -641,7 +641,7 @@ exports[`renders chip with icon 1`] = `
641641
{
642642
"busy": undefined,
643643
"checked": undefined,
644-
"disabled": true,
644+
"disabled": false,
645645
"expanded": undefined,
646646
"selected": false,
647647
}
@@ -1166,7 +1166,7 @@ exports[`renders selected chip 1`] = `
11661166
{
11671167
"busy": undefined,
11681168
"checked": undefined,
1169-
"disabled": true,
1169+
"disabled": false,
11701170
"expanded": undefined,
11711171
"selected": true,
11721172
}

0 commit comments

Comments
 (0)