Skip to content

Commit 76717f3

Browse files
feat: Add readOnly prop to TextInput
1 parent 545c82b commit 76717f3

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,13 @@ export type Props = $ReadOnly<{|
532532
*/
533533
editable?: ?boolean,
534534

535+
/** `readOnly` works like the `readonly` attribute in HTML.
536+
* If `true`, text is not editable. The default value is `false`.
537+
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly
538+
* for more details.
539+
*/
540+
readOnly?: ?boolean,
541+
535542
forwardedRef?: ?ReactRefSetter<
536543
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
537544
>,
@@ -1381,6 +1388,7 @@ const ExportedForwardRef: React.AbstractComponent<
13811388
allowFontScaling = true,
13821389
rejectResponderTermination = true,
13831390
underlineColorAndroid = 'transparent',
1391+
readOnly = false,
13841392
...restProps
13851393
},
13861394
forwardedRef: ReactRefSetter<
@@ -1392,6 +1400,7 @@ const ExportedForwardRef: React.AbstractComponent<
13921400
allowFontScaling={allowFontScaling}
13931401
rejectResponderTermination={rejectResponderTermination}
13941402
underlineColorAndroid={underlineColorAndroid}
1403+
editable={!readOnly}
13951404
{...restProps}
13961405
forwardedRef={forwardedRef}
13971406
/>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,35 @@ exports.examples = ([
347347
);
348348
},
349349
},
350+
{
351+
title: 'Editable and Read only',
352+
render: function (): React.Node {
353+
return (
354+
<View>
355+
<TextInput
356+
placeholder="editable text input using editable prop"
357+
style={styles.default}
358+
editable
359+
/>
360+
<TextInput
361+
placeholder="uneditable text input using editable prop"
362+
style={styles.default}
363+
editable={false}
364+
/>
365+
<TextInput
366+
placeholder="editable text input using readOnly prop"
367+
style={styles.default}
368+
readOnly={false}
369+
/>
370+
<TextInput
371+
placeholder="uneditable text input using readOnly prop"
372+
style={styles.default}
373+
readOnly
374+
/>
375+
</View>
376+
);
377+
},
378+
},
350379
{
351380
title: 'Fixed number of lines',
352381
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)