Skip to content

Commit 3311b68

Browse files
committed
feat(handleTextInput): add specific HOC for controlling TextInput
1 parent 5576577 commit 3311b68

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/handleTextInput.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { compose, withProps } from "recompose";
2+
import { connect } from "formik";
3+
import withInputTypeProps from "./withInputTypeProps";
4+
import withFormikControl from "./withFormikControl";
5+
6+
const handleTextInput = compose(
7+
withFormikControl,
8+
withInputTypeProps,
9+
connect,
10+
withProps(
11+
({
12+
formik: { isSubmitting },
13+
setFieldValue,
14+
setFieldTouched,
15+
onChangeText,
16+
onBlur
17+
}) => ({
18+
onChangeText: text => {
19+
setFieldValue(text);
20+
if (onChangeText) onChangeText(text);
21+
},
22+
onBlur: () => {
23+
// validate onBlur only while not submitting
24+
// this prevents validating twice in succession when clicking 'done' on keyboard - first onSubmitEditing, then onBlur
25+
setFieldTouched(true, !isSubmitting);
26+
if (onBlur) onBlur();
27+
}
28+
})
29+
)
30+
);
31+
32+
export default handleTextInput;

0 commit comments

Comments
 (0)