Skip to content

Commit 2a14569

Browse files
committed
touched
1 parent 5132804 commit 2a14569

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

demoApp/App.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class App extends Component {
7676

7777
renderError(id) {
7878
const {inputs} = this.state;
79-
if (inputs[id].errorLabel && inputs[id].touched) {
79+
if (inputs[id].errorLabel) {
8080
return <Text style={styles.error}>{inputs[id].errorLabel}</Text>;
8181
}
8282
return null;
@@ -94,6 +94,7 @@ export default class App extends Component {
9494
this.onInputChange({id: 'first_name', value});
9595
}}
9696
errorLabel={inputs.first_name.errorLabel}
97+
touched={inputs.first_name.touched}
9798
onLayout={({nativeEvent}) => {
9899
this.setInputPosition({
99100
ids: ['first_name'],
@@ -108,6 +109,7 @@ export default class App extends Component {
108109
this.onInputChange({id: 'last_name', value});
109110
}}
110111
errorLabel={inputs.last_name.errorLabel}
112+
touched={inputs.last_name.touched}
111113
onLayout={({nativeEvent}) => {
112114
this.setInputPosition({
113115
ids: ['last_name'],
@@ -132,6 +134,7 @@ export default class App extends Component {
132134
this.onInputChange({id: 'birthday_month', value});
133135
}}
134136
errorLabel={inputs.birthday_month.errorLabel}
137+
touched={inputs.birthday_month.touched}
135138
placeholder="Month"
136139
keyboardType="number-pad"
137140
/>
@@ -142,6 +145,7 @@ export default class App extends Component {
142145
this.onInputChange({id: 'birthday_day', value});
143146
}}
144147
errorLabel={inputs.birthday_day.errorLabel}
148+
touched={inputs.birthday_day.touched}
145149
placeholder="Day"
146150
keyboardType="number-pad"
147151
/>
@@ -152,6 +156,7 @@ export default class App extends Component {
152156
this.onInputChange({id: 'birthday_year', value});
153157
}}
154158
errorLabel={inputs.birthday_year.errorLabel}
159+
touched={inputs.birthday_year.touched}
155160
placeholder="Year"
156161
keyboardType="number-pad"
157162
/>
@@ -163,6 +168,7 @@ export default class App extends Component {
163168
this.onInputChange({id: 'state', value});
164169
}}
165170
errorLabel={inputs.state.errorLabel}
171+
touched={inputs.state.touched}
166172
onLayout={({nativeEvent}) => {
167173
this.setInputPosition({
168174
ids: ['state'],
@@ -179,6 +185,7 @@ export default class App extends Component {
179185
this.onInputChange({id: 'zip', value});
180186
}}
181187
errorLabel={inputs.zip.errorLabel}
188+
touched={inputs.zip.touched}
182189
onLayout={({nativeEvent}) => {
183190
this.setInputPosition({
184191
ids: ['zip'],
@@ -242,4 +249,4 @@ const styles = StyleSheet.create({
242249
color: 'red',
243250
fontSize: 12,
244251
},
245-
});
252+
});

demoApp/components/FormInput.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import {StyleSheet, Text, TextInput, View} from 'react-native';
44
export default class FormInput extends Component {
55
constructor(props) {
66
super(props);
7-
this.state = {};
7+
this.state = {
8+
touched: false,
9+
};
10+
11+
this.onBlur = this.onBlur.bind(this);
812
}
913

1014
renderError() {
11-
const {errorLabel} = this.props;
12-
if (errorLabel) {
15+
const {errorLabel, touched} = this.props;
16+
if (errorLabel && (touched || this.state.touched)) {
1317
return (
1418
<View>
1519
<Text style={styles.error}>{errorLabel}</Text>
@@ -19,12 +23,18 @@ export default class FormInput extends Component {
1923
return null;
2024
}
2125

26+
onBlur() {
27+
this.setState({
28+
touched: true,
29+
});
30+
}
31+
2232
render() {
2333
const {label} = this.props;
2434
return (
2535
<Fragment>
2636
<Text>{label}</Text>
27-
<TextInput style={styles.input} {...this.props} />
37+
<TextInput style={styles.input} {...this.props} onBlur={this.onBlur} />
2838
{this.renderError()}
2939
</Fragment>
3040
);

demoApp/validation/service.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ function onInputChange({id, value, cb = () => {}}) {
2727
);
2828
}
2929

30-
function getInputValidationState({input, value}) {
30+
function getInputValidationState({input, value, touched}) {
3131
return {
3232
...input,
3333
value,
3434
errorLabel: input.optional
3535
? null
3636
: validateInput({type: input.type, value}),
37+
touched: touched || input.touched,
3738
};
3839
}
3940

@@ -63,6 +64,7 @@ function getFormValidation() {
6364
updatedInputs[key] = getInputValidationState({
6465
input,
6566
value: input.value,
67+
touched: true,
6668
});
6769
}
6870

0 commit comments

Comments
 (0)