Skip to content

Commit 0fc2e95

Browse files
hannojgfacebook-github-bot
authored andcommitted
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: - #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: #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
1 parent e8f8ee3 commit 0fc2e95

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,40 @@ function MultilineStyledTextInput({
846846
);
847847
}
848848

849+
function DynamicContentWidth() {
850+
const [text, setText] = useState('');
851+
const update = () => {
852+
const randomNumber = Math.floor(Math.random() * 10);
853+
setText(text + randomNumber);
854+
};
855+
856+
return (
857+
<View>
858+
<Text>Uncontrolled:</Text>
859+
<TextInput
860+
placeholder="Type..."
861+
style={{
862+
fontSize: 16,
863+
alignSelf: 'center',
864+
backgroundColor: 'orange',
865+
}}
866+
/>
867+
<Text>Controlled:</Text>
868+
<TextInput
869+
placeholder="..."
870+
value={text}
871+
onChangeText={setText}
872+
style={{
873+
fontSize: 16,
874+
alignSelf: 'center',
875+
backgroundColor: 'orange',
876+
}}
877+
/>
878+
<Button title="Update controlled Input" onPress={update} />
879+
</View>
880+
);
881+
}
882+
849883
module.exports = ([
850884
{
851885
title: 'Auto-focus & select text on focus',
@@ -1149,4 +1183,11 @@ module.exports = ([
11491183
);
11501184
},
11511185
},
1186+
{
1187+
title: 'Dynamic content width',
1188+
name: 'dynamicWidth',
1189+
render: function (): React.Node {
1190+
return <DynamicContentWidth />;
1191+
},
1192+
},
11521193
]: Array<RNTesterModuleExample>);

0 commit comments

Comments
 (0)