Skip to content

Commit f5e975b

Browse files
committed
fix: compatibility with formik 1.0.1+
1 parent 5fa0ae2 commit f5e975b

File tree

10 files changed

+117
-64
lines changed

10 files changed

+117
-64
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"cz-conventional-changelog": "^2.1.0",
4444
"enzyme": "^3.3.0",
4545
"enzyme-adapter-react-16": "^1.0.0",
46+
"formik": "^1.0.2",
4647
"husky": "^0.14.3",
4748
"jest": "^22.4.2",
4849
"react": "16.2.0",

src/__tests__/makeReactNativeField.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ import { TextInput } from "react-native";
55
import { mount } from "enzyme";
66

77
import { makeReactNativeField } from "../..";
8+
import withFormikMock from "../testUtils/withFormikMock";
89

910
console.error = jest.fn();
1011

1112
const setFieldValue = jest.fn();
1213
const setFieldTouched = jest.fn();
1314

14-
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
15-
formik: {
16-
setFieldValue,
17-
setFieldTouched,
18-
values: {
19-
email: "contact@bam.tech",
20-
user: {
21-
username: "bam-dev",
22-
password: 'goodchallenge',
23-
}
15+
const formikContext = {
16+
setFieldValue,
17+
setFieldTouched,
18+
values: {
19+
email: "contact@bam.tech",
20+
user: {
21+
username: "bam-dev",
22+
password: 'goodchallenge',
2423
}
2524
}
26-
}));
27-
const Input = compose(withFormikMock, makeReactNativeField)(TextInput);
25+
};
26+
const Input = compose(withFormikMock(formikContext), makeReactNativeField)(TextInput);
2827

2928
describe("makeReactNativeField", () => {
3029
it("sets the input value", () => {

src/__tests__/setFormikInitialValue.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TextInput } from "react-native";
55
import { mount } from "enzyme";
66

77
import { setFormikInitialValue } from "../..";
8+
import withFormikMock from "../testUtils/withFormikMock";
89

910
console.error = jest.fn();
1011

@@ -14,15 +15,14 @@ let Input;
1415
beforeEach(() => {
1516
setFieldValue = jest.fn();
1617

17-
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
18-
formik: {
19-
setFieldValue,
20-
values: {
21-
"this input has been set by formik": "set value"
22-
}
18+
const formikContext = {
19+
setFieldValue,
20+
values: {
21+
"this input has been set by formik": "set value"
2322
}
24-
}));
25-
Input = compose(withFormikMock, setFormikInitialValue)(TextInput);
23+
};
24+
25+
Input = compose(withFormikMock(formikContext), setFormikInitialValue)(TextInput);
2626
});
2727

2828
describe("setFormikInitialValue", () => {

src/__tests__/withError.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import { TextInput } from "react-native";
55
import { mount } from "enzyme";
66

77
import { withError } from "../..";
8+
import withFormikMock from "../testUtils/withFormikMock";
89

910
console.error = jest.fn();
1011

11-
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
12-
formik: {
13-
errors: {
14-
email: "This is not a valid email.",
15-
user: {
16-
password: "Password is too short!"
17-
}
12+
const formikContext = {
13+
errors: {
14+
email: "This is not a valid email.",
15+
user: {
16+
password: "Password is too short!"
1817
}
1918
}
20-
}));
21-
const Input = compose(withFormikMock, withError)(TextInput);
19+
};
20+
const Input = compose(withFormikMock(formikContext), withError)(TextInput);
2221

2322
describe("withError", () => {
2423
it("sets the error prop", () => {

src/__tests__/withNextInputAutoFocus.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { Button, View } from "react-native";
55
import { mount } from "enzyme";
66

77
import { withNextInputAutoFocusForm, withNextInputAutoFocusInput } from "../..";
8+
import withFormikMock from "../testUtils/withFormikMock";
89

910
const submitForm = jest.fn();
10-
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
11-
formik: {
12-
submitForm
13-
}
14-
}));
1511

16-
const Form = compose(withFormikMock, withNextInputAutoFocusForm)(View);
12+
const Form = compose(withFormikMock({
13+
submitForm
14+
}), withNextInputAutoFocusForm)(View);
1715

1816
const focusInput = jest.fn();
1917
class TextInput extends React.PureComponent {

src/__tests__/withTouched.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import { TextInput } from "react-native";
55
import { mount } from "enzyme";
66

77
import { withTouched } from "../..";
8+
import withFormikMock from "../testUtils/withFormikMock";
89

910
console.error = jest.fn();
1011

11-
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
12-
formik: {
13-
touched: {
14-
email: true,
15-
user: {
16-
username: true,
17-
}
12+
const formikContext = {
13+
touched: {
14+
email: true,
15+
user: {
16+
username: true
1817
}
1918
}
20-
}));
21-
const Input = compose(withFormikMock, withTouched)(TextInput);
19+
};
20+
21+
const Input = compose(
22+
withFormikMock(formikContext),
23+
withTouched
24+
)(TextInput);
2225

2326
describe("withTouched", () => {
2427
it("sets the touched prop", () => {

src/testUtils/withFormikMock.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
import { FormikProvider } from "formik";
3+
4+
export default formikContextValue => Component => props => (
5+
<FormikProvider value={formikContextValue}>
6+
<Component {...props} />
7+
</FormikProvider>
8+
);

src/withFormik.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { getContext } from "recompose";
2-
import PropTypes from "prop-types";
1+
import { connect } from "formik";
32

4-
const withFormik = getContext({ formik: PropTypes.object });
5-
6-
export default withFormik;
3+
export default connect;

src/withNextInputAutoFocus.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react";
2-
import { compose, mapProps } from "recompose";
32
import PropTypes from "prop-types";
43
import { isArray } from "lodash";
54
import withFormik from "./withFormik";
@@ -12,7 +11,7 @@ const withNextInputAutoFocusContextType = {
1211

1312
const getInputs = children =>
1413
(isArray(children) ? children : [children]).reduce((partialInputs, child) => {
15-
if (child.props && child.props.children) {
14+
if (child && child.props && child.props.children) {
1615
return partialInputs.concat(getInputs(child.props.children));
1716
}
1817
if (child && child.props && !!child.props.name) return partialInputs.concat(child);
@@ -22,12 +21,11 @@ const getInputs = children =>
2221
export const withNextInputAutoFocusForm = WrappedComponent => {
2322
class WithNextInputAutoFocusForm extends React.PureComponent {
2423
static childContextTypes = withNextInputAutoFocusContextType;
25-
static contextTypes = { formik: PropTypes.object };
2624

2725
constructor(props) {
2826
super(props);
2927
const { children } = props;
30-
this.inputs = getInputs(children);
28+
this.inputs = getInputs(children || []);
3129
}
3230

3331
inputs;
@@ -46,7 +44,7 @@ export const withNextInputAutoFocusForm = WrappedComponent => {
4644
const isLastInput = inputPosition === this.inputs.length - 1;
4745

4846
if (isLastInput) {
49-
this.context.formik.submitForm();
47+
this.props.formik.submitForm();
5048
} else {
5149
const nextInputs = this.inputs.slice(inputPosition + 1);
5250
const nextFocusableInput = nextInputs.find(
@@ -70,7 +68,7 @@ export const withNextInputAutoFocusForm = WrappedComponent => {
7068
}
7169
}
7270

73-
return WithNextInputAutoFocusForm;
71+
return withFormik(WithNextInputAutoFocusForm);
7472
};
7573

7674
export const withNextInputAutoFocusInput = Input => {

yarn.lock

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,13 @@ create-react-class@^15.5.2, create-react-class@^15.6.0:
18391839
loose-envify "^1.3.1"
18401840
object-assign "^4.1.1"
18411841

1842+
create-react-context@^0.2.2:
1843+
version "0.2.2"
1844+
resolved "https://registry.yarnpkg.com/create-react-context/-/create-react-context-0.2.2.tgz#9836542f9aaa22868cd7d4a6f82667df38019dca"
1845+
dependencies:
1846+
fbjs "^0.8.0"
1847+
gud "^1.0.0"
1848+
18421849
cross-spawn@^5.0.1, cross-spawn@^5.1.0:
18431850
version "5.1.0"
18441851
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -1999,6 +2006,10 @@ deep-is@~0.1.3:
19992006
version "0.1.3"
20002007
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
20012008

2009+
deepmerge@^2.1.1:
2010+
version "2.1.1"
2011+
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-2.1.1.tgz#e862b4e45ea0555072bf51e7fd0d9845170ae768"
2012+
20022013
default-require-extensions@^1.0.0:
20032014
version "1.0.0"
20042015
resolved "https://registry.yarnpkg.com/default-require-extensions/-/default-require-extensions-1.0.0.tgz#f37ea15d3e13ffd9b437d33e1a75b5fb97874cb8"
@@ -2565,6 +2576,18 @@ fbjs-scripts@^0.8.1:
25652576
semver "^5.1.0"
25662577
through2 "^2.0.0"
25672578

2579+
fbjs@^0.8.0:
2580+
version "0.8.17"
2581+
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.17.tgz#c4d598ead6949112653d6588b01a5cdcd9f90fdd"
2582+
dependencies:
2583+
core-js "^1.0.0"
2584+
isomorphic-fetch "^2.1.1"
2585+
loose-envify "^1.0.0"
2586+
object-assign "^4.1.0"
2587+
promise "^7.1.1"
2588+
setimmediate "^1.0.5"
2589+
ua-parser-js "^0.7.18"
2590+
25682591
fbjs@^0.8.1, fbjs@^0.8.14, fbjs@^0.8.16, fbjs@^0.8.4, fbjs@^0.8.9:
25692592
version "0.8.16"
25702593
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.16.tgz#5e67432f550dc41b572bf55847b8aca64e5337db"
@@ -2719,14 +2742,18 @@ form-data@~2.3.1:
27192742
combined-stream "1.0.6"
27202743
mime-types "^2.1.12"
27212744

2722-
formik@^0.11.11:
2723-
version "0.11.11"
2724-
resolved "https://registry.yarnpkg.com/formik/-/formik-0.11.11.tgz#4b02838133c0196b1ef443aa973766cd097ec4a5"
2745+
formik@^1.0.2:
2746+
version "1.0.2"
2747+
resolved "https://registry.yarnpkg.com/formik/-/formik-1.0.2.tgz#333962ab3f09f137992b13198fefa4ec91b15af5"
27252748
dependencies:
2749+
create-react-context "^0.2.2"
2750+
deepmerge "^2.1.1"
2751+
hoist-non-react-statics "^2.5.5"
27262752
lodash.clonedeep "^4.5.0"
2727-
lodash.isequal "4.5.0"
27282753
lodash.topath "4.5.2"
2729-
prop-types "^15.5.10"
2754+
prop-types "^15.6.1"
2755+
react-fast-compare "^1.0.0"
2756+
tslib "^1.9.3"
27302757
warning "^3.0.0"
27312758

27322759
fragment-cache@^0.2.1:
@@ -3014,6 +3041,10 @@ growly@^1.3.0:
30143041
version "1.3.0"
30153042
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
30163043

3044+
gud@^1.0.0:
3045+
version "1.0.0"
3046+
resolved "https://registry.yarnpkg.com/gud/-/gud-1.0.0.tgz#a489581b17e6a70beca9abe3ae57de7a499852c0"
3047+
30173048
gulp-util@^3.0.4:
30183049
version "3.0.8"
30193050
resolved "https://registry.yarnpkg.com/gulp-util/-/gulp-util-3.0.8.tgz#0054e1e744502e27c04c187c3ecc505dd54bbb4f"
@@ -3176,6 +3207,10 @@ hoist-non-react-statics@^2.3.1:
31763207
version "2.5.0"
31773208
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.0.tgz#d2ca2dfc19c5a91c5a6615ce8e564ef0347e2a40"
31783209

3210+
hoist-non-react-statics@^2.5.5:
3211+
version "2.5.5"
3212+
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47"
3213+
31793214
home-or-tmp@^2.0.0:
31803215
version "2.0.0"
31813216
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
@@ -4342,10 +4377,6 @@ lodash.isarray@^3.0.0:
43424377
version "3.0.4"
43434378
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55"
43444379

4345-
lodash.isequal@4.5.0:
4346-
version "4.5.0"
4347-
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
4348-
43494380
lodash.keys@^3.0.0:
43504381
version "3.1.2"
43514382
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"
@@ -5408,6 +5439,13 @@ prop-types@^15.5.10, prop-types@^15.5.8, prop-types@^15.6.0:
54085439
loose-envify "^1.3.1"
54095440
object-assign "^4.1.1"
54105441

5442+
prop-types@^15.6.1:
5443+
version "15.6.2"
5444+
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
5445+
dependencies:
5446+
loose-envify "^1.3.1"
5447+
object-assign "^4.1.1"
5448+
54115449
protocols@^1.1.0, protocols@^1.4.0:
54125450
version "1.4.6"
54135451
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.6.tgz#f8bb263ea1b5fd7a7604d26b8be39bd77678bf8a"
@@ -5525,6 +5563,10 @@ react-dom@16.2.0:
55255563
object-assign "^4.1.1"
55265564
prop-types "^15.6.0"
55275565

5566+
react-fast-compare@^1.0.0:
5567+
version "1.0.0"
5568+
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-1.0.0.tgz#813a039155e49b43ceffe99528fe5e9d97a6c938"
5569+
55285570
react-native@0.53.0:
55295571
version "0.53.0"
55305572
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.53.0.tgz#d57b49b1683f3bbfffd098fd07f7be0ff9e9df74"
@@ -6693,6 +6735,10 @@ trim-right@^1.0.1:
66936735
version "1.0.1"
66946736
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
66956737

6738+
tslib@^1.9.3:
6739+
version "1.9.3"
6740+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
6741+
66966742
tsscmp@1.0.5:
66976743
version "1.0.5"
66986744
resolved "https://registry.yarnpkg.com/tsscmp/-/tsscmp-1.0.5.tgz#7dc4a33af71581ab4337da91d85ca5427ebd9a97"
@@ -6724,6 +6770,10 @@ typedarray@^0.0.6:
67246770
version "0.0.6"
67256771
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
67266772

6773+
ua-parser-js@^0.7.18:
6774+
version "0.7.18"
6775+
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.18.tgz#a7bfd92f56edfb117083b69e31d2aa8882d4b1ed"
6776+
67276777
ua-parser-js@^0.7.9:
67286778
version "0.7.17"
67296779
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.17.tgz#e9ec5f9498b9ec910e7ae3ac626a805c4d09ecac"

0 commit comments

Comments
 (0)