Skip to content

Commit

Permalink
feat: Add readOnly prop to TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldonadel committed Aug 18, 2022
1 parent 545c82b commit 76717f3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,13 @@ export type Props = $ReadOnly<{|
*/
editable?: ?boolean,

/** `readOnly` works like the `readonly` attribute in HTML.
* If `true`, text is not editable. The default value is `false`.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly
* for more details.
*/
readOnly?: ?boolean,

forwardedRef?: ?ReactRefSetter<
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
>,
Expand Down Expand Up @@ -1381,6 +1388,7 @@ const ExportedForwardRef: React.AbstractComponent<
allowFontScaling = true,
rejectResponderTermination = true,
underlineColorAndroid = 'transparent',
readOnly = false,
...restProps
},
forwardedRef: ReactRefSetter<
Expand All @@ -1392,6 +1400,7 @@ const ExportedForwardRef: React.AbstractComponent<
allowFontScaling={allowFontScaling}
rejectResponderTermination={rejectResponderTermination}
underlineColorAndroid={underlineColorAndroid}
editable={!readOnly}
{...restProps}
forwardedRef={forwardedRef}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,35 @@ exports.examples = ([
);
},
},
{
title: 'Editable and Read only',
render: function (): React.Node {
return (
<View>
<TextInput
placeholder="editable text input using editable prop"
style={styles.default}
editable
/>
<TextInput
placeholder="uneditable text input using editable prop"
style={styles.default}
editable={false}
/>
<TextInput
placeholder="editable text input using readOnly prop"
style={styles.default}
readOnly={false}
/>
<TextInput
placeholder="uneditable text input using readOnly prop"
style={styles.default}
readOnly
/>
</View>
);
},
},
{
title: 'Fixed number of lines',
platform: 'android',
Expand Down
29 changes: 29 additions & 0 deletions packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,35 @@ exports.examples = ([
);
},
},
{
title: 'Editable and Read only',
render: function (): React.Node {
return (
<View>
<TextInput
placeholder="editable text input using editable prop"
style={styles.default}
editable
/>
<TextInput
placeholder="uneditable text input using editable prop"
style={styles.default}
editable={false}
/>
<TextInput
placeholder="editable text input using readOnly prop"
style={styles.default}
readOnly={false}
/>
<TextInput
placeholder="uneditable text input using readOnly prop"
style={styles.default}
readOnly
/>
</View>
);
},
},
{
title: 'TextInput Intrinsic Size',
render: function (): React.Node {
Expand Down

0 comments on commit 76717f3

Please sign in to comment.