Skip to content

Commit 8c882b4

Browse files
Daksh Bhardwajfacebook-github-bot
Daksh Bhardwaj
authored andcommitted
feat: added enterKeyHint prop to textInput (#34482)
Summary: This adds the `enterKeyHint` prop to the TextInput component as requested on #34424, mapping web [enterKeyHint types](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint) to equivalent [returnKeyType](https://reactnative.dev/docs/textinput#returnkeytype) values. This PR also updates RNTester TextInputExample in order to facilitate the manual QA. ## Open questions - What should be the `returnType` in the case of `previous` in iOS? - what should happen if `enterKeyHint` and `returnKeyType` props are passed together? ## Changelog [General] [Added] - Add enterKeyHint prop to TextInput component Pull Request resolved: #34482 Test Plan: - Open the RNTester app and navigate to the TextInput page - Test the TextInput component through the `enterKeyHint modes section` <image src="https://user-images.githubusercontent.com/22423684/186191205-9c04732e-568c-4cce-9564-50a84d70dca3.png" height="600px" width="300px" /> Reviewed By: GijsWeterings Differential Revision: D38957275 Pulled By: necolas fbshipit-source-id: d75f2c2000df5d9606a005083b20bf3a23b48831
1 parent 38e1e25 commit 8c882b4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ export type TextContentType =
207207
| 'newPassword'
208208
| 'oneTimeCode';
209209

210+
export type enterKeyHintType =
211+
| 'enter'
212+
| 'done'
213+
| 'go'
214+
| 'next'
215+
| 'previous'
216+
| 'search'
217+
| 'send';
218+
210219
type PasswordRules = string;
211220

212221
type IOSProps = $ReadOnly<{|
@@ -543,6 +552,21 @@ export type Props = $ReadOnly<{|
543552
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
544553
>,
545554

555+
/**
556+
* `enterKeyHint` defines what action label (or icon) to present for the enter key on virtual keyboards.
557+
*
558+
* The following values is supported:
559+
*
560+
* - `enter`
561+
* - `done`
562+
* - `go`
563+
* - `next`
564+
* - `previous`
565+
* - `search`
566+
* - `send`
567+
*/
568+
enterKeyHint?: ?enterKeyHintType,
569+
546570
/**
547571
* Determines which keyboard to open, e.g.`numeric`.
548572
*
@@ -1388,6 +1412,16 @@ function InternalTextInput(props: Props): React.Node {
13881412
);
13891413
}
13901414

1415+
const enterKeyHintToReturnTypeMap = {
1416+
enter: 'default',
1417+
done: 'done',
1418+
go: 'go',
1419+
next: 'next',
1420+
previous: 'previous',
1421+
search: 'search',
1422+
send: 'send',
1423+
};
1424+
13911425
const ExportedForwardRef: React.AbstractComponent<
13921426
React.ElementConfig<typeof InternalTextInput>,
13931427
React.ElementRef<HostComponent<mixed>> & ImperativeMethods,
@@ -1398,6 +1432,8 @@ const ExportedForwardRef: React.AbstractComponent<
13981432
underlineColorAndroid = 'transparent',
13991433
readOnly,
14001434
editable,
1435+
enterKeyHint,
1436+
returnKeyType,
14011437
...restProps
14021438
},
14031439
forwardedRef: ReactRefSetter<
@@ -1410,6 +1446,9 @@ const ExportedForwardRef: React.AbstractComponent<
14101446
rejectResponderTermination={rejectResponderTermination}
14111447
underlineColorAndroid={underlineColorAndroid}
14121448
editable={readOnly !== undefined ? !readOnly : editable}
1449+
returnKeyType={
1450+
enterKeyHint ? enterKeyHintToReturnTypeMap[enterKeyHint] : returnKeyType
1451+
}
14131452
{...restProps}
14141453
forwardedRef={forwardedRef}
14151454
/>

packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,29 @@ module.exports = ([
727727
return <BlurOnSubmitExample />;
728728
},
729729
},
730+
{
731+
title: 'enterKeyHint modes',
732+
name: 'enterKeyHintTypes',
733+
render: function (): React.Node {
734+
const enterKeyHintTypesHints = [
735+
'enter',
736+
'done',
737+
'go',
738+
'next',
739+
'previous',
740+
'search',
741+
'send',
742+
];
743+
const examples = enterKeyHintTypesHints.map(hint => {
744+
return (
745+
<WithLabel key={hint} label={hint}>
746+
<TextInput enterKeyHint={hint} style={styles.default} />
747+
</WithLabel>
748+
);
749+
});
750+
return <View>{examples}</View>;
751+
},
752+
},
730753
{
731754
title: 'Submit behavior',
732755
render: function (): React.Element<any> {

0 commit comments

Comments
 (0)