Skip to content

Commit f151456

Browse files
Cassio Zenfacebook-github-bot
Cassio Zen
authored andcommitted
Add autoComplete prop (#21575)
Summary: TL;DR: Setting `autoComplete` will allow the system to suggest autofill options for the `<TextInput>` component. Android Oreo introduced the AutoFill Framework, for secure communication between an app and autofill services (e.g. Password managers). When using `<TextInput>` on Android Oreo+, the system already tries to autofill (based on heuristics), but there is no way to set configuring options or disable. The quick solution would be to just add the same Android attributes (`autofillHints` & `importantForAutofill`) in React Native TextInput, but that doesn't bond well with the cross-platform nature of the library. Introduces an `autoComplete` prop based on HTML's `autocomplete` attribute, mapping to Android `autofillHints` & `importantForAutofill` and serving as a proper placeholder for autofill/autocomplete in other platforms: Also gives you the ability to disable autofill by setting autocomplete="off". Pull Request resolved: #21575 Differential Revision: D14102949 Pulled By: hramos fbshipit-source-id: 7601aeaca0332a1f3ce8da8020dba037b700853a
1 parent 53b87dc commit f151456

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Libraries/Components/TextInput/TextInput.js

+54
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ type IOSProps = $ReadOnly<{|
223223
|}>;
224224

225225
type AndroidProps = $ReadOnly<{|
226+
autoCompleteType?: ?(
227+
| 'cc-csc'
228+
| 'cc-exp'
229+
| 'cc-exp-month'
230+
| 'cc-exp-year'
231+
| 'cc-number'
232+
| 'email'
233+
| 'name'
234+
| 'password'
235+
| 'postal-code'
236+
| 'street-address'
237+
| 'tel'
238+
| 'username'
239+
| 'off'
240+
),
226241
returnKeyLabel?: ?string,
227242
numberOfLines?: ?number,
228243
disableFullscreenUI?: ?boolean,
@@ -413,6 +428,45 @@ const TextInput = createReactClass({
413428
'words',
414429
'characters',
415430
]),
431+
/**
432+
* Determines which content to suggest on auto complete, e.g.`username`.
433+
* To disable auto complete, use `off`.
434+
*
435+
* *Android Only*
436+
*
437+
* The following values work on Android only:
438+
*
439+
* - `username`
440+
* - `password`
441+
* - `email`
442+
* - `name`
443+
* - `tel`
444+
* - `street-address`
445+
* - `postal-code`
446+
* - `cc-number`
447+
* - `cc-csc`
448+
* - `cc-exp`
449+
* - `cc-exp-month`
450+
* - `cc-exp-year`
451+
* - `off`
452+
*
453+
* @platform android
454+
*/
455+
autoCompleteType: PropTypes.oneOf([
456+
'cc-csc',
457+
'cc-exp',
458+
'cc-exp-month',
459+
'cc-exp-year',
460+
'cc-number',
461+
'email',
462+
'name',
463+
'password',
464+
'postal-code',
465+
'street-address',
466+
'tel',
467+
'username',
468+
'off',
469+
]),
416470
/**
417471
* If `false`, disables auto-correct. The default value is `true`.
418472
*/

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+35
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,41 @@ public void setMaxLength(ReactEditText view, @Nullable Integer maxLength) {
529529
view.setFilters(newFilters);
530530
}
531531

532+
@ReactProp(name = "autoComplete")
533+
public void setTextContentType(ReactEditText view, @Nullable String autocomplete) {
534+
if (autocomplete == null) {
535+
view.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
536+
} else if ("username".equals(autocomplete)) {
537+
view.setAutofillHints(View.AUTOFILL_HINT_USERNAME);
538+
} else if ("password".equals(autocomplete)) {
539+
view.setAutofillHints(View.AUTOFILL_HINT_PASSWORD);
540+
} else if ("email".equals(autocomplete)) {
541+
view.setAutofillHints(View.AUTOFILL_HINT_EMAIL_ADDRESS);
542+
} else if ("name".equals(autocomplete)) {
543+
view.setAutofillHints(View.AUTOFILL_HINT_NAME);
544+
} else if ("tel".equals(autocomplete)) {
545+
view.setAutofillHints(View.AUTOFILL_HINT_PHONE);
546+
} else if ("street-address".equals(autocomplete)) {
547+
view.setAutofillHints(View.AUTOFILL_HINT_POSTAL_ADDRESS);
548+
} else if ("postal-code".equals(autocomplete)) {
549+
view.setAutofillHints(View.AUTOFILL_HINT_POSTAL_CODE);
550+
} else if ("cc-number".equals(autocomplete)) {
551+
view.setAutofillHints(View.AUTOFILL_HINT_CREDIT_CARD_NUMBER);
552+
} else if ("cc-csc".equals(autocomplete)) {
553+
view.setAutofillHints(View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE);
554+
} else if ("cc-exp".equals(autocomplete)) {
555+
view.setAutofillHints(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE);
556+
} else if ("cc-exp-month".equals(autocomplete)) {
557+
view.setAutofillHints(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH);
558+
} else if ("cc-exp-year".equals(autocomplete)) {
559+
view.setAutofillHints(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR);
560+
} else if ("off".equals(autocomplete)) {
561+
view.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
562+
} else {
563+
throw new JSApplicationIllegalArgumentException("Invalid autocomplete option: " + autocomplete);
564+
}
565+
}
566+
532567
@ReactProp(name = "autoCorrect")
533568
public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
534569
// clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value

0 commit comments

Comments
 (0)