Skip to content

Commit de75a7a

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
feat: Add readOnly prop to TextInput component (#34444)
Summary: This adds the `readOnly` prop to TextInput as requested on #34424 mapping the existing `editable` prop to `readOnly` so that `readOnly={false}` maps to `editable={true}` and `readOnly={true}` represents ` editable={false}`. This PR also updates the TextInputExample on the RNTest in order to facilitate the manual QA of this. ## Changelog [General] [Added] - Add readOnly prop to TextInput component Pull Request resolved: #34444 Test Plan: 1. Open the RNTester app and navigate to the TextInput page 2. Test the `TextInput` component through the `Editable and Read only` section https://user-images.githubusercontent.com/11707729/185295132-036443c8-1d5e-4567-a15e-5f1173cb0526.mov Reviewed By: lunaleaps Differential Revision: D38912786 Pulled By: necolas fbshipit-source-id: faeb59ed8695732be682ec55406a2de0cb7e619a
1 parent 32af9e6 commit de75a7a

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,13 @@ export type Props = $ReadOnly<{|
709709
*/
710710
placeholderTextColor?: ?ColorValue,
711711

712+
/** `readOnly` works like the `readonly` attribute in HTML.
713+
* If `true`, text is not editable. The default value is `false`.
714+
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly
715+
* for more details.
716+
*/
717+
readOnly?: ?boolean,
718+
712719
/**
713720
* Determines how the return key should look. On Android you can also use
714721
* `returnKeyLabel`.
@@ -1381,6 +1388,8 @@ const ExportedForwardRef: React.AbstractComponent<
13811388
allowFontScaling = true,
13821389
rejectResponderTermination = true,
13831390
underlineColorAndroid = 'transparent',
1391+
readOnly,
1392+
editable,
13841393
...restProps
13851394
},
13861395
forwardedRef: ReactRefSetter<
@@ -1392,6 +1401,7 @@ const ExportedForwardRef: React.AbstractComponent<
13921401
allowFontScaling={allowFontScaling}
13931402
rejectResponderTermination={rejectResponderTermination}
13941403
underlineColorAndroid={underlineColorAndroid}
1404+
editable={readOnly !== undefined ? !readOnly : editable}
13951405
{...restProps}
13961406
forwardedRef={forwardedRef}
13971407
/>

packages/rn-tester/js/examples/TextInput/TextInputExample.android.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ const styles = StyleSheet.create({
148148
singleLineWithHeightTextInput: {
149149
height: 30,
150150
},
151+
default: {
152+
borderWidth: StyleSheet.hairlineWidth,
153+
borderColor: '#0f0f0f',
154+
flex: 1,
155+
fontSize: 13,
156+
padding: 4,
157+
},
151158
});
152159

153160
exports.title = 'TextInput';
@@ -347,6 +354,35 @@ exports.examples = ([
347354
);
348355
},
349356
},
357+
{
358+
title: 'Editable and Read only',
359+
render: function (): React.Node {
360+
return (
361+
<View>
362+
<TextInput
363+
placeholder="editable text input using editable prop"
364+
style={styles.default}
365+
editable
366+
/>
367+
<TextInput
368+
placeholder="uneditable text input using editable prop"
369+
style={styles.default}
370+
editable={false}
371+
/>
372+
<TextInput
373+
placeholder="editable text input using readOnly prop"
374+
style={styles.default}
375+
readOnly={false}
376+
/>
377+
<TextInput
378+
placeholder="uneditable text input using readOnly prop"
379+
style={styles.default}
380+
readOnly
381+
/>
382+
</View>
383+
);
384+
},
385+
},
350386
{
351387
title: 'Fixed number of lines',
352388
platform: 'android',

packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,35 @@ exports.examples = ([
621621
);
622622
},
623623
},
624+
{
625+
title: 'Editable and Read only',
626+
render: function (): React.Node {
627+
return (
628+
<View>
629+
<TextInput
630+
placeholder="editable text input using editable prop"
631+
style={styles.default}
632+
editable
633+
/>
634+
<TextInput
635+
placeholder="uneditable text input using editable prop"
636+
style={styles.default}
637+
editable={false}
638+
/>
639+
<TextInput
640+
placeholder="editable text input using readOnly prop"
641+
style={styles.default}
642+
readOnly={false}
643+
/>
644+
<TextInput
645+
placeholder="uneditable text input using readOnly prop"
646+
style={styles.default}
647+
readOnly
648+
/>
649+
</View>
650+
);
651+
},
652+
},
624653
{
625654
title: 'TextInput Intrinsic Size',
626655
render: function (): React.Node {

0 commit comments

Comments
 (0)