Skip to content

Commit e182c98

Browse files
authored
Merge pull request #2 from bamlab/fix/setFormikInitialValue
fix: set input initial value by default
2 parents d20b9fc + d2695dc commit e182c98

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This repository is a set of high order components designed to help you take cont
1818
- [Advanced Example](#advanced-example)
1919
- [API](#api)
2020
- [makeReactNativeField](#makereactnativefield)
21+
- [setFormikInitialValue](#setFormikInitialValue)
2122
- [withError](#witherror)
2223
- [withFocus](#withfocus)
2324
- [withFormik](#withformik)
@@ -83,7 +84,7 @@ export default class MaterialTextInput extends React.PureComponent {
8384
### Create our form logic
8485

8586
Compose our input with high order components to make it awesome.
86-
`react-native-formik` exports as default `compose(withInputTypeProps, withError, withTouched, makeReactNativeField);`.
87+
`react-native-formik` exports as default `compose(withInputTypeProps, setFormikInitialValue, withError, withTouched, makeReactNativeField);`.
8788

8889
Let's add in `withNextInputAutoFocusInput`:
8990

@@ -254,6 +255,12 @@ export default props => {
254255
};
255256
```
256257

258+
### setFormikInitialValue
259+
260+
Set Input initial value to `""` to Formik without having to use `initialValues` prop.
261+
262+
Especially it allows validation of untouched inputs when pressing submit.
263+
257264
### withError
258265

259266
Pass in the Formik error for the input as a prop.

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { compose } from "recompose";
2+
import setFormikInitialValue from "./src/setFormikInitialValue";
23
import withError from "./src/withError";
34
import withFocus from "./src/withFocus";
45
import withFormik from "./src/withFormik";
@@ -7,13 +8,14 @@ import withTouched from "./src/withTouched";
78
import makeReactNativeField from "./src/makeReactNativeField";
89
import { withNextInputAutoFocusForm, withNextInputAutoFocusInput } from "./src/withNextInputAutoFocus";
910

10-
const makeInputsGreatAgain = compose(withInputTypeProps, withError, withTouched, makeReactNativeField);
11+
const makeInputsGreatAgain = compose(withInputTypeProps, setFormikInitialValue, withError, withTouched, makeReactNativeField);
1112

1213
export default makeInputsGreatAgain;
1314

1415
export {
1516
makeInputsGreatAgain,
1617
makeReactNativeField,
18+
setFormikInitialValue,
1719
withError,
1820
withFocus,
1921
withFormik,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import { compose, withContext } from "recompose";
4+
import { TextInput } from "react-native";
5+
import { mount } from "enzyme";
6+
7+
import { setFormikInitialValue } from "../..";
8+
9+
console.error = jest.fn();
10+
11+
const setFieldValue = jest.fn();
12+
13+
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
14+
formik: {
15+
setFieldValue
16+
}
17+
}));
18+
const Input = compose(withFormikMock, setFormikInitialValue)(TextInput);
19+
20+
describe("setFormikInitialValue", () => {
21+
it("sets the initial value to ''", () => {
22+
const touchedInput = mount(<Input name="inputName" />);
23+
expect(setFieldValue).toBeCalledWith("inputName", "");
24+
});
25+
26+
it("keeps other props", () => {
27+
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
28+
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");
29+
});
30+
});

src/setFormikInitialValue.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
import { compose, mapProps } from "recompose";
3+
import withFormik from "./withFormik";
4+
5+
const setFormikInitialValue = WrappedInput => {
6+
return class WithFocusProp extends React.PureComponent {
7+
constructor(props) {
8+
super(props);
9+
props.formik.setFieldValue(props.name, "");
10+
}
11+
12+
render() {
13+
return <WrappedInput {...this.props} />;
14+
}
15+
};
16+
};
17+
18+
export default compose(withFormik, setFormikInitialValue);

0 commit comments

Comments
 (0)