Skip to content

Commit a413e3e

Browse files
lunaleapskelset
authored andcommitted
Precedent textContentType when set
Summary: Changelog: [iOS][Changed] - Give precedence to `textContentType` property for backwards compat as mentioned in #36229 (comment) Reviewed By: necolas Differential Revision: D44106291 fbshipit-source-id: 5702d7f171735d1abe6cfbc9ca1ad8f21751d51e
1 parent 96dc2c1 commit a413e3e

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ type IOSProps = $ReadOnly<{|
349349
/**
350350
* Give the keyboard and the system information about the
351351
* expected semantic meaning for the content that users enter.
352+
* `autoComplete` property accomplishes same behavior and is recommended as its supported by both platforms.
353+
* Avoid using both `autoComplete` and `textContentType`, you can use `Platform.select` for differing platform behaviors.
354+
* For backwards compatibility, when both set, `textContentType` takes precedence on iOS.
352355
* @platform ios
353356
*/
354357
textContentType?: ?TextContentType,
@@ -1630,16 +1633,20 @@ const ExportedForwardRef: React.AbstractComponent<
16301633
}
16311634
autoComplete={
16321635
Platform.OS === 'android'
1633-
? // $FlowFixMe
1636+
? // $FlowFixMe[invalid-computed-prop]
1637+
// $FlowFixMe[prop-missing]
16341638
autoCompleteWebToAutoCompleteAndroidMap[autoComplete] ??
16351639
autoComplete
16361640
: undefined
16371641
}
16381642
textContentType={
1639-
Platform.OS === 'ios' &&
1640-
autoComplete &&
1641-
autoComplete in autoCompleteWebToTextContentTypeMap
1642-
? // $FlowFixMe
1643+
textContentType != null
1644+
? textContentType
1645+
: Platform.OS === 'ios' &&
1646+
autoComplete &&
1647+
autoComplete in autoCompleteWebToTextContentTypeMap
1648+
? // $FlowFixMe[invalid-computed-prop]
1649+
// $FlowFixMe[prop-missing]
16431650
autoCompleteWebToTextContentTypeMap[autoComplete]
16441651
: textContentType
16451652
}

Libraries/Components/TextInput/__tests__/TextInput-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,42 @@ describe('TextInput tests', () => {
169169
expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRe2.current);
170170
});
171171

172+
it('should give precedence to `textContentType` when set', () => {
173+
const instance = ReactTestRenderer.create(
174+
<TextInput autoComplete="tel" textContentType="emailAddress" />,
175+
);
176+
177+
expect(instance.toJSON()).toMatchInlineSnapshot(`
178+
<RCTSinglelineTextInputView
179+
accessible={true}
180+
allowFontScaling={true}
181+
focusable={true}
182+
forwardedRef={null}
183+
mostRecentEventCount={0}
184+
onBlur={[Function]}
185+
onChange={[Function]}
186+
onChangeSync={null}
187+
onClick={[Function]}
188+
onFocus={[Function]}
189+
onResponderGrant={[Function]}
190+
onResponderMove={[Function]}
191+
onResponderRelease={[Function]}
192+
onResponderTerminate={[Function]}
193+
onResponderTerminationRequest={[Function]}
194+
onScroll={[Function]}
195+
onSelectionChange={[Function]}
196+
onSelectionChangeShouldSetResponder={[Function]}
197+
onStartShouldSetResponder={[Function]}
198+
rejectResponderTermination={true}
199+
selection={null}
200+
submitBehavior="blurAndSubmit"
201+
text=""
202+
textContentType="emailAddress"
203+
underlineColorAndroid="transparent"
204+
/>
205+
`);
206+
});
207+
172208
it('should render as expected', () => {
173209
expectRendersMatchingSnapshot(
174210
'TextInput',

packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,13 @@ exports.examples = ([
824824
<WithLabel label="name">
825825
<TextInput textContentType="name" style={styles.default} />
826826
</WithLabel>
827+
<WithLabel label="postalCode, when autoComplete set">
828+
<TextInput
829+
textContentType="postalCode"
830+
autoComplete="email"
831+
style={styles.default}
832+
/>
833+
</WithLabel>
827834
</View>
828835
);
829836
},

0 commit comments

Comments
 (0)