Skip to content

Commit 82ac55f

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Refactor platform common TextInput tests
Summary: Android and iOS have different RNTester examples even though they should be mostly the same. Pull out the common ones into their own file. This should also be useful for other platforms. Changelog: [Internal] (Note: this ignores all push blocking failures!) Reviewed By: JoshuaGross Differential Revision: D18628890 fbshipit-source-id: 6f1312973aebcfc687fdd8807bf942e48172f216
1 parent dff490d commit 82ac55f

File tree

3 files changed

+260
-302
lines changed

3 files changed

+260
-302
lines changed

RNTester/js/examples/TextInput/TextInputExample.android.js

Lines changed: 7 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const {
2121
Switch,
2222
} = require('react-native');
2323

24+
const TextInputSharedExamples = require('./TextInputSharedExamples.js');
25+
26+
import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes';
27+
2428
class TextEventsExample extends React.Component<{...}, $FlowFixMeState> {
2529
state = {
2630
curText: '<No Event>',
@@ -82,41 +86,6 @@ class TextEventsExample extends React.Component<{...}, $FlowFixMeState> {
8286
}
8387
}
8488

85-
class RewriteExample extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
86-
constructor(props) {
87-
super(props);
88-
this.state = {text: ''};
89-
}
90-
render() {
91-
const limit = 20;
92-
const remainder = limit - this.state.text.length;
93-
const remainderColor = remainder > 5 ? 'blue' : 'red';
94-
return (
95-
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
96-
* when making Flow check .android.js files. */
97-
<View style={styles.rewriteContainer}>
98-
<TextInput
99-
multiline={false}
100-
maxLength={limit}
101-
onChangeText={text => {
102-
text = text.replace(/ /g, '_');
103-
this.setState({text});
104-
}}
105-
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
106-
* found when making Flow check .android.js files. */
107-
style={styles.default}
108-
value={this.state.text}
109-
/>
110-
{/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
111-
* found when making Flow check .android.js files. */}
112-
<Text style={[styles.remainder, {color: remainderColor}]}>
113-
{remainder}
114-
</Text>
115-
</View>
116-
);
117-
}
118-
}
119-
12089
class TokenizedTextExample extends React.Component<
12190
$FlowFixMeProps,
12291
$FlowFixMeState,
@@ -443,86 +412,8 @@ const styles = StyleSheet.create({
443412

444413
exports.title = '<TextInput>';
445414
exports.description = 'Single and multi-line text inputs.';
446-
exports.examples = [
447-
{
448-
title: 'Auto-focus',
449-
render: function(): React.Node {
450-
return (
451-
<TextInput
452-
autoFocus={true}
453-
multiline={true}
454-
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
455-
* found when making Flow check .android.js files. */
456-
style={styles.input}
457-
accessibilityLabel="I am the accessibility label for text input"
458-
/>
459-
);
460-
},
461-
},
462-
{
463-
title: "Live Re-Write (<sp> -> '_')",
464-
render: function(): React.Node {
465-
return <RewriteExample />;
466-
},
467-
},
468-
{
469-
title: 'Auto-capitalize',
470-
render: function(): React.Node {
471-
const autoCapitalizeTypes = ['none', 'sentences', 'words', 'characters'];
472-
const examples = autoCapitalizeTypes.map(type => {
473-
return (
474-
<TextInput
475-
key={type}
476-
autoCapitalize={type}
477-
placeholder={'autoCapitalize: ' + type}
478-
style={styles.singleLine}
479-
/>
480-
);
481-
});
482-
return <View>{examples}</View>;
483-
},
484-
},
485-
{
486-
title: 'Auto-correct',
487-
render: function(): React.Node {
488-
return (
489-
<View>
490-
<TextInput
491-
autoCorrect={true}
492-
placeholder="This has autoCorrect"
493-
style={styles.singleLine}
494-
/>
495-
<TextInput
496-
autoCorrect={false}
497-
placeholder="This does not have autoCorrect"
498-
style={styles.singleLine}
499-
/>
500-
</View>
501-
);
502-
},
503-
},
504-
{
505-
title: 'Keyboard types',
506-
render: function(): React.Node {
507-
const keyboardTypes = [
508-
'default',
509-
'email-address',
510-
'numeric',
511-
'phone-pad',
512-
];
513-
const examples = keyboardTypes.map(type => {
514-
return (
515-
<TextInput
516-
key={type}
517-
keyboardType={type}
518-
placeholder={'keyboardType: ' + type}
519-
style={styles.singleLine}
520-
/>
521-
);
522-
});
523-
return <View>{examples}</View>;
524-
},
525-
},
415+
exports.examples = ([
416+
...TextInputSharedExamples,
526417
{
527418
title: 'Blur on submit',
528419
render: function(): React.Element<any> {
@@ -873,4 +764,4 @@ exports.examples = [
873764
);
874765
},
875766
},
876-
];
767+
]: Array<RNTesterExampleModuleItem>);

RNTester/js/examples/TextInput/TextInputExample.ios.js

Lines changed: 7 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const {
2424
Alert,
2525
} = require('react-native');
2626

27+
const TextInputSharedExamples = require('./TextInputSharedExamples.js');
28+
29+
import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes';
30+
2731
class WithLabel extends React.Component<$FlowFixMeProps> {
2832
render() {
2933
return (
@@ -128,96 +132,6 @@ class TextInputAccessoryViewExample extends React.Component<{...}, *> {
128132
}
129133
}
130134

131-
class RewriteExample extends React.Component<$FlowFixMeProps, any> {
132-
constructor(props) {
133-
super(props);
134-
this.state = {text: ''};
135-
}
136-
render() {
137-
const limit = 20;
138-
const remainder = limit - this.state.text.length;
139-
const remainderColor = remainder > 5 ? 'blue' : 'red';
140-
return (
141-
<View style={styles.rewriteContainer}>
142-
<TextInput
143-
multiline={false}
144-
maxLength={limit}
145-
onChangeText={text => {
146-
text = text.replace(/ /g, '_');
147-
this.setState({text});
148-
}}
149-
style={styles.default}
150-
value={this.state.text}
151-
/>
152-
<Text style={[styles.remainder, {color: remainderColor}]}>
153-
{remainder}
154-
</Text>
155-
</View>
156-
);
157-
}
158-
}
159-
160-
class RewriteExampleInvalidCharacters extends React.Component<
161-
$FlowFixMeProps,
162-
any,
163-
> {
164-
constructor(props) {
165-
super(props);
166-
this.state = {text: ''};
167-
}
168-
render() {
169-
return (
170-
<View style={styles.rewriteContainer}>
171-
<TextInput
172-
multiline={false}
173-
onChangeText={text => {
174-
this.setState({text: text.replace(/\s/g, '')});
175-
}}
176-
style={styles.default}
177-
value={this.state.text}
178-
/>
179-
</View>
180-
);
181-
}
182-
}
183-
184-
class RewriteInvalidCharactersAndClearExample extends React.Component<
185-
$FlowFixMeProps,
186-
any,
187-
> {
188-
inputRef: ?React.ElementRef<typeof TextInput> = null;
189-
190-
constructor(props) {
191-
super(props);
192-
this.state = {text: ''};
193-
}
194-
render() {
195-
return (
196-
<View style={styles.rewriteContainer}>
197-
<TextInput
198-
ref={ref => {
199-
this.inputRef = ref;
200-
}}
201-
multiline={false}
202-
onChangeText={text => {
203-
this.setState({text: text.replace(/\s/g, '')});
204-
}}
205-
style={styles.default}
206-
value={this.state.text}
207-
/>
208-
<Button
209-
onPress={() => {
210-
if (this.inputRef != null) {
211-
this.inputRef.clear();
212-
}
213-
}}
214-
title="Clear"
215-
/>
216-
</View>
217-
);
218-
}
219-
}
220-
221135
class RewriteExampleKana extends React.Component<$FlowFixMeProps, any> {
222136
constructor(props) {
223137
super(props);
@@ -610,37 +524,8 @@ const styles = StyleSheet.create({
610524
exports.displayName = (undefined: ?string);
611525
exports.title = '<TextInput>';
612526
exports.description = 'Single and multi-line text inputs.';
613-
exports.examples = [
614-
{
615-
title: 'Auto-focus',
616-
render: function(): React.Node {
617-
return (
618-
<TextInput
619-
autoFocus={true}
620-
style={styles.default}
621-
accessibilityLabel="I am the accessibility label for text input"
622-
/>
623-
);
624-
},
625-
},
626-
{
627-
title: "Live Re-Write (<sp> -> '_') + maxLength",
628-
render: function(): React.Node {
629-
return <RewriteExample />;
630-
},
631-
},
632-
{
633-
title: 'Live Re-Write (no spaces allowed)',
634-
render: function(): React.Node {
635-
return <RewriteExampleInvalidCharacters />;
636-
},
637-
},
638-
{
639-
title: 'Rewrite (no spaces allowed) and clear',
640-
render: function(): React.Node {
641-
return <RewriteInvalidCharactersAndClearExample />;
642-
},
643-
},
527+
exports.examples = ([
528+
...TextInputSharedExamples,
644529
{
645530
title: 'Live Re-Write (ひ -> 日)',
646531
render: function(): React.Node {
@@ -653,42 +538,6 @@ exports.examples = [
653538
return <TextInputAccessoryViewExample />;
654539
},
655540
},
656-
{
657-
title: 'Auto-capitalize',
658-
render: function(): React.Node {
659-
return (
660-
<View>
661-
<WithLabel label="none">
662-
<TextInput autoCapitalize="none" style={styles.default} />
663-
</WithLabel>
664-
<WithLabel label="sentences">
665-
<TextInput autoCapitalize="sentences" style={styles.default} />
666-
</WithLabel>
667-
<WithLabel label="words">
668-
<TextInput autoCapitalize="words" style={styles.default} />
669-
</WithLabel>
670-
<WithLabel label="characters">
671-
<TextInput autoCapitalize="characters" style={styles.default} />
672-
</WithLabel>
673-
</View>
674-
);
675-
},
676-
},
677-
{
678-
title: 'Auto-correct',
679-
render: function(): React.Node {
680-
return (
681-
<View>
682-
<WithLabel label="true">
683-
<TextInput autoCorrect={true} style={styles.default} />
684-
</WithLabel>
685-
<WithLabel label="false">
686-
<TextInput autoCorrect={false} style={styles.default} />
687-
</WithLabel>
688-
</View>
689-
);
690-
},
691-
},
692541
{
693542
title: 'Nested content and `value` property',
694543
render: function(): React.Node {
@@ -715,34 +564,6 @@ exports.examples = [
715564
);
716565
},
717566
},
718-
{
719-
title: 'Keyboard types',
720-
render: function(): React.Node {
721-
const keyboardTypes = [
722-
'default',
723-
'ascii-capable',
724-
'numbers-and-punctuation',
725-
'url',
726-
'number-pad',
727-
'phone-pad',
728-
'name-phone-pad',
729-
'email-address',
730-
'decimal-pad',
731-
'twitter',
732-
'web-search',
733-
'ascii-capable-number-pad',
734-
'numeric',
735-
];
736-
const examples = keyboardTypes.map(type => {
737-
return (
738-
<WithLabel key={type} label={type}>
739-
<TextInput keyboardType={type} style={styles.default} />
740-
</WithLabel>
741-
);
742-
});
743-
return <View>{examples}</View>;
744-
},
745-
},
746567
{
747568
title: 'Keyboard appearance',
748569
render: function(): React.Node {
@@ -1221,4 +1042,4 @@ exports.examples = [
12211042
);
12221043
},
12231044
},
1224-
];
1045+
]: Array<RNTesterExampleModuleItem>);

0 commit comments

Comments
 (0)