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 inputMode prop to TextInput component #34460

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
43 changes: 43 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ export type KeyboardType =
// Android-only
| 'visible-password';

export type InputMode =
| 'none'
| 'text'
| 'decimal'
| 'numeric'
| 'tel'
| 'search'
| 'email'
| 'url';
Copy link
Contributor

Choose a reason for hiding this comment

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

'none' should be added to the allowed values even if we do nothing with it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good, I've just added none there and mapped it to default


export type ReturnKeyType =
// Cross Platform
| 'done'
Expand Down Expand Up @@ -567,6 +577,23 @@ export type Props = $ReadOnly<{|
*/
enterKeyHint?: ?enterKeyHintType,

/**
* `inputMode` works like the `inputmode` attribute in HTML, it determines which
* keyboard to open, e.g.`numeric` and has precedence over keyboardType
*
* Support the following values:
*
* - `none`
* - `text`
* - `decimal`
* - `numeric`
* - `tel`
* - `search`
* - `email`
* - `url`
*/
inputMode?: ?InputMode,

/**
* Determines which keyboard to open, e.g.`numeric`.
*
Expand Down Expand Up @@ -1422,6 +1449,17 @@ const enterKeyHintToReturnTypeMap = {
send: 'send',
};

const inputModeToKeyboardTypeMap = {
none: 'default',
text: 'default',
decimal: 'decimal-pad',
numeric: 'number-pad',
tel: 'phone-pad',
search: Platform.OS === 'ios' ? 'web-search' : 'default',
email: 'email-address',
url: 'url',
};

const ExportedForwardRef: React.AbstractComponent<
React.ElementConfig<typeof InternalTextInput>,
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
Expand All @@ -1434,6 +1472,8 @@ const ExportedForwardRef: React.AbstractComponent<
editable,
enterKeyHint,
returnKeyType,
inputMode,
keyboardType,
...restProps
},
forwardedRef: ReactRefSetter<
Expand All @@ -1449,6 +1489,9 @@ const ExportedForwardRef: React.AbstractComponent<
returnKeyType={
enterKeyHint ? enterKeyHintToReturnTypeMap[enterKeyHint] : returnKeyType
}
keyboardType={
inputMode ? inputModeToKeyboardTypeMap[inputMode] : keyboardType
}
{...restProps}
forwardedRef={forwardedRef}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,30 @@ module.exports = ([
return <View>{examples}</View>;
},
},
{
title: 'Input modes',
name: 'inputModes',
render: function (): React.Node {
const inputMode = [
'none',
'text',
'decimal',
'numeric',
'tel',
'search',
'email',
'url',
];
const examples = inputMode.map(mode => {
return (
<WithLabel key={mode} label={mode}>
<TextInput inputMode={mode} style={styles.default} />
</WithLabel>
);
});
return <View>{examples}</View>;
},
},
{
title: 'Blur on submit',
render: function (): React.Element<any> {
Expand Down