Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add readOnly prop to TextInput component #34444

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,13 @@ export type Props = $ReadOnly<{|
*/
placeholderTextColor?: ?ColorValue,

/** `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,

/**
* Determines how the return key should look. On Android you can also use
* `returnKeyLabel`.
Expand Down Expand Up @@ -1381,6 +1388,8 @@ const ExportedForwardRef: React.AbstractComponent<
allowFontScaling = true,
rejectResponderTermination = true,
underlineColorAndroid = 'transparent',
readOnly,
editable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a deprecation message on editable if it's used cc @cipolleschi. We could do that in a follow-up PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can open a follow-up PR and also update the docs on /~https://github.com/facebook/react-native-website/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunaleaps here is the follow-up PR #34492 and the PR updating the docs facebook/react-native-website#3278

...restProps
},
forwardedRef: ReactRefSetter<
Expand All @@ -1392,6 +1401,7 @@ const ExportedForwardRef: React.AbstractComponent<
allowFontScaling={allowFontScaling}
rejectResponderTermination={rejectResponderTermination}
underlineColorAndroid={underlineColorAndroid}
editable={readOnly !== undefined ? !readOnly : editable}
{...restProps}
forwardedRef={forwardedRef}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ const styles = StyleSheet.create({
singleLineWithHeightTextInput: {
height: 30,
},
default: {
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#0f0f0f',
flex: 1,
fontSize: 13,
padding: 4,
},
});

exports.title = 'TextInput';
Expand Down Expand Up @@ -347,6 +354,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