add example for dynamic sized text input (#46976)

Summary:
This PR adds an example showcasing a TextInput whose width gets resized dynamically based on the text content width.
This example was added for this PR, which tries to address a flickering bug with dynamic sized TextInputs:

- https://github.com/facebook/react-native/pull/46973

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[INTERNAL] [ADDED] Added dynamic content width TextInput example

Pull Request resolved: https://github.com/facebook/react-native/pull/46976

Test Plan:
Test in RNTester app:

https://github.com/user-attachments/assets/7571b80b-7035-47df-b840-4218ab0802b5

Reviewed By: NickGerleman

Differential Revision: D64271454

Pulled By: arushikesarwani94

fbshipit-source-id: 28fe7bd93891ffe5284340d73e9ee947654f1788
This commit is contained in:
Hanno J. Gödecke authored and Facebook GitHub Bot committed 2024-10-29 12:40:09 -07:00
1 parent e8f8ee3c0f
commit 0fc2e954be
1 file changed
+41
@@ -846,6 +846,40 @@ function MultilineStyledTextInput({
);
}
function DynamicContentWidth() {
const [text, setText] = useState('');
const update = () => {
const randomNumber = Math.floor(Math.random() * 10);
setText(text + randomNumber);
};
return (
<View>
<Text>Uncontrolled:</Text>
<TextInput
placeholder="Type..."
style={{
fontSize: 16,
alignSelf: 'center',
backgroundColor: 'orange',
}}
/>
<Text>Controlled:</Text>
<TextInput
placeholder="..."
value={text}
onChangeText={setText}
style={{
fontSize: 16,
alignSelf: 'center',
backgroundColor: 'orange',
}}
/>
<Button title="Update controlled Input" onPress={update} />
</View>
);
}
module.exports = ([
{
title: 'Auto-focus & select text on focus',
@@ -1149,4 +1183,11 @@ module.exports = ([
);
},
},
{
title: 'Dynamic content width',
name: 'dynamicWidth',
render: function (): React.Node {
return <DynamicContentWidth />;
},
},
]: Array<RNTesterModuleExample>);