From 8bed520f7f6856a0abaa739ee71c1f180f90372b Mon Sep 17 00:00:00 2001 From: jameswilddev Date: Tue, 28 May 2024 19:31:04 +0100 Subject: [PATCH] Fix broken autofocus functionality. --- .../components/createInputComponent/index.tsx | 21 +++-- .../components/createInputComponent/unit.tsx | 92 ++++++++----------- 2 files changed, 48 insertions(+), 65 deletions(-) diff --git a/react-native/components/createInputComponent/index.tsx b/react-native/components/createInputComponent/index.tsx index 2243f76..a0ca3a9 100644 --- a/react-native/components/createInputComponent/index.tsx +++ b/react-native/components/createInputComponent/index.tsx @@ -426,7 +426,6 @@ export function createInputComponent ( const valid = tryParse(editing.current, context) !== undefined - const ref = React.useRef(null) const firstLayout = React.useRef(true) if (!autoFocus) { @@ -451,17 +450,19 @@ export function createInputComponent ( > {leftIcon} { + if (element !== null) { + if (autoFocus) { + if (firstLayout.current) { + console.error('a') + element.focus() + firstLayout.current = false } + } else { + firstLayout.current = true } - : {})} + } + }} style={ disabled ? valid diff --git a/react-native/components/createInputComponent/unit.tsx b/react-native/components/createInputComponent/unit.tsx index 083c5a7..ee8289f 100644 --- a/react-native/components/createInputComponent/unit.tsx +++ b/react-native/components/createInputComponent/unit.tsx @@ -1,5 +1,5 @@ import * as React from 'react' -import { Text, TextInput, View } from 'react-native' +import { TextInput, View, Text } from 'react-native' import * as TestRenderer from 'react-test-renderer' import { createInputComponent, @@ -8891,7 +8891,6 @@ test('does nothing when auto-focus is enabled', () => { nodeType: 'component', type: TextInput, props: { - onLayout: expect.any(Function), style: { flexGrow: 1, color: '#FFEE00', @@ -9022,29 +9021,22 @@ test('focuses the text input on layout when the ref is ready', () => { const onSubmit = jest.fn() const renderer = TestRenderer.create( - - ); - - ( - ( - (renderer.toTree()?.rendered as TestRenderer.ReactTestRendererTree) - .rendered as TestRenderer.ReactTestRendererTree - ).rendered as readonly TestRenderer.ReactTestRendererTree[] - )[0]?.props['onLayout']() + + ) expect( ( @@ -9170,21 +9162,25 @@ test('does not focus the text input a second time', () => { character: 'G' }} /> - ); - - ( - ( - (renderer.toTree()?.rendered as TestRenderer.ReactTestRendererTree) - .rendered as TestRenderer.ReactTestRendererTree - ).rendered as readonly TestRenderer.ReactTestRendererTree[] - )[0]?.props['onLayout'](); + ) - ( - ( - (renderer.toTree()?.rendered as TestRenderer.ReactTestRendererTree) - .rendered as TestRenderer.ReactTestRendererTree - ).rendered as readonly TestRenderer.ReactTestRendererTree[] - )[0]?.props['onLayout']() + renderer.update( + + ) expect( ( @@ -9985,14 +9981,7 @@ test('handles a change from auto focus to non-auto focus to auto focus', () => { character: 'G' }} /> - ); - - ( - ( - (renderer.toTree()?.rendered as TestRenderer.ReactTestRendererTree) - .rendered as TestRenderer.ReactTestRendererTree - ).rendered as readonly TestRenderer.ReactTestRendererTree[] - )[0]?.props['onLayout']() + ) renderer.update( { character: 'G' }} /> - ); - - ( - ( - (renderer.toTree()?.rendered as TestRenderer.ReactTestRendererTree) - .rendered as TestRenderer.ReactTestRendererTree - ).rendered as readonly TestRenderer.ReactTestRendererTree[] - )[0]?.props['onLayout']() + ) expect( (