From ee53c70f1019582652b784a1d4a422e92a1fabb5 Mon Sep 17 00:00:00 2001 From: Bao Nguyen Date: Sun, 23 Aug 2026 11:15:57 +0700 Subject: [PATCH] fix(switch): forward ref to the root view --- src/components/Switch/Switch.tsx | 4 +++- src/components/__tests__/Switch.test.tsx | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/components/Switch/Switch.tsx b/src/components/Switch/Switch.tsx index b0ca4b4bd7..37591a55d6 100644 --- a/src/components/Switch/Switch.tsx +++ b/src/components/Switch/Switch.tsx @@ -61,6 +61,7 @@ export type Props = { * Accessibility label for the switch. This is read by the screen reader when the user focuses the switch. */ 'aria-label'?: string; + ref?: React.RefObject; }; const { @@ -136,6 +137,7 @@ const Switch = ({ testID, theme: themeOverrides, 'aria-label': ariaLabel, + ref, }: Props) => { const theme = useInternalTheme(themeOverrides); const reduceMotion = useReduceMotion(); @@ -346,7 +348,7 @@ const Switch = ({ const iconSize = checked ? SELECTED_ICON : UNSELECTED_ICON; return ( - + onValueChange?.(!checked)} diff --git a/src/components/__tests__/Switch.test.tsx b/src/components/__tests__/Switch.test.tsx index ceff77a0e6..55f71d9820 100644 --- a/src/components/__tests__/Switch.test.tsx +++ b/src/components/__tests__/Switch.test.tsx @@ -1,3 +1,6 @@ +import * as React from 'react'; +import type { View } from 'react-native'; + import { describe, expect, it, jest } from '@jest/globals'; import { render, screen, userEvent } from '../../test-utils'; @@ -74,3 +77,21 @@ describe('Switch interaction', () => { expect(onValueChange).not.toHaveBeenCalled(); }); }); + +describe('Switch ref', () => { + it('forwards ref to the root view', async () => { + const ref = React.createRef(); + + await render(); + + expect(ref.current).not.toBeNull(); + }); + + it('exposes the measure methods of the root view', async () => { + const ref = React.createRef(); + + await render(); + + expect(typeof ref.current?.measure).toBe('function'); + }); +});