From 779ed66a54e0ef169ae8c31ebfd1c9bf462dcf58 Mon Sep 17 00:00:00 2001 From: Doberjohn Date: Mon, 16 Nov 2026 16:43:08 +0200 Subject: [PATCH] fix(checkbox): clear the focus ring when onPress is removed On Android, a keyboard-focused Checkbox that loses and regains `onPress` shows its ring with nothing focused. The destroyed Pressable never sends a blur, and the reset effect cleared hover and press state but not focus. --- src/components/Checkbox/Checkbox.tsx | 4 +- .../__tests__/Checkbox/Checkbox.test.tsx | 80 ++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/components/Checkbox/Checkbox.tsx b/src/components/Checkbox/Checkbox.tsx index f8d8115b40..0aac8cc304 100644 --- a/src/components/Checkbox/Checkbox.tsx +++ b/src/components/Checkbox/Checkbox.tsx @@ -410,10 +410,12 @@ const Checkbox = ({ : null; // Losing the handlers means the matching hover-out or press-out never - // arrives, so the state would otherwise linger. + // arrives, so the state would otherwise linger. The focus is lost too: the + // focused Pressable is replaced, and its blur never arrives either. React.useEffect(() => { if (isInteractive) return; + setFocused(false); setHovered(false); pressedSV.value = 0; rippleHoldSV.value = 0; diff --git a/src/components/__tests__/Checkbox/Checkbox.test.tsx b/src/components/__tests__/Checkbox/Checkbox.test.tsx index 7ae853dbab..b8ecd0e92f 100644 --- a/src/components/__tests__/Checkbox/Checkbox.test.tsx +++ b/src/components/__tests__/Checkbox/Checkbox.test.tsx @@ -1,9 +1,16 @@ -import { type GestureResponderEvent, Platform, Pressable } from 'react-native'; +import { + type GestureResponderEvent, + Platform, + Pressable, + StyleSheet, +} from 'react-native'; import { afterEach, describe, expect, it, jest } from '@jest/globals'; import { fireEvent, render, screen, userEvent } from '../../../test-utils'; +import { tokens } from '../../../theme/tokens'; import Checkbox from '../../Checkbox'; +import { CheckboxTokens } from '../../Checkbox/tokens'; import TouchableRipple from '../../TouchableRipple/TouchableRipple'; afterEach(() => { @@ -376,6 +383,77 @@ describe('Checkbox Space key', () => { ); }); +describe('Checkbox focus ring', () => { + const { thickness, outerOffset } = tokens.md.sys.state.focusIndicator; + const ringSize = + CheckboxTokens.stateLayerSize + 2 * (outerOffset + thickness); + + /* eslint-disable testing-library/no-node-access, no-restricted-syntax -- + Checkbox draws its ring as an unnamed view beside the pressable, with no + role or testID to query it by, so the outer container's children have to + be read to count it. */ + const ringCount = () => + (screen.root?.children ?? []).filter((child) => { + if (typeof child === 'string') return false; + const { width, height, borderWidth } = + StyleSheet.flatten(child.props.style) ?? {}; + return ( + width === ringSize && height === ringSize && borderWidth === thickness + ); + }).length; + /* eslint-enable testing-library/no-node-access, no-restricted-syntax */ + + // A display-only checkbox is not exposed as a checkbox, so it is found by + // testID. + const Box = ({ onPress }: { onPress?: () => void }) => ( + + ); + + // On Android a TouchableRipple without a handler renders a plain View, so + // the focused Pressable is destroyed and its blur never reaches JS. + const renderOnAndroid = async (onPress?: () => void) => { + jest.replaceProperty(Platform, 'OS', 'android'); + jest.replaceProperty(TouchableRipple, 'supported', true); + await render(); + }; + + it('draws one ring on keyboard focus', async () => { + await renderOnAndroid(() => {}); + await fireEvent(screen.getByTestId('checkbox'), 'focus'); + + expect(ringCount()).toBe(1); + }); + + it('drops the ring when onPress is removed', async () => { + const onPress = () => {}; + await renderOnAndroid(onPress); + const pressable = screen.getByTestId('checkbox'); + await fireEvent(screen.getByTestId('checkbox'), 'focus'); + + await screen.rerender(); + + // The focused Pressable was replaced, not just left without a handler. + expect(screen.getByTestId('checkbox')).not.toBe(pressable); + expect(ringCount()).toBe(0); + }); + + it('shows no ring when onPress comes back until it is focused again', async () => { + const onPress = () => {}; + await renderOnAndroid(onPress); + await fireEvent(screen.getByTestId('checkbox'), 'focus'); + + await screen.rerender(); + await screen.rerender(); + + expect(ringCount()).toBe(0); + }); +}); + describe('Checkbox touch target', () => { it('meets the 48dp minimum', async () => { await render(