Skip to content

Commit 7fd4096

Browse files
TketaAlmouro
authored andcommitted
fix: Handle nested field name
1 parent da1108d commit 7fd4096

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

src/__tests__/makeReactNativeField.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
1515
formik: {
1616
setFieldValue,
1717
setFieldTouched,
18-
values: { email: "contact@bam.tech" }
18+
values: {
19+
email: "contact@bam.tech",
20+
user: {
21+
username: "bam-dev",
22+
password: 'goodchallenge',
23+
}
24+
}
1925
}
2026
}));
2127
const Input = compose(withFormikMock, makeReactNativeField)(TextInput);
@@ -28,6 +34,13 @@ describe("makeReactNativeField", () => {
2834
expect(otherInput.find(TextInput).props().value).toEqual(undefined);
2935
});
3036

37+
it('sets the input value for nested key name', () => {
38+
const emailInput = mount(<Input name="user.username" />);
39+
expect(emailInput.find(TextInput).props().value).toEqual("bam-dev");
40+
const otherInput = mount(<Input name="user.other" />);
41+
expect(otherInput.find(TextInput).props().value).toEqual(undefined);
42+
});
43+
3144
it("handles input value change", () => {
3245
const wrapper = mount(<Input name="email" />);
3346
wrapper

src/__tests__/withError.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,31 @@ console.error = jest.fn();
1010

1111
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
1212
formik: {
13-
errors: { errored: "This is an error" }
13+
errors: {
14+
email: "This is not a valid email.",
15+
user: {
16+
password: "Password is too short!"
17+
}
18+
}
1419
}
1520
}));
1621
const Input = compose(withFormikMock, withError)(TextInput);
1722

1823
describe("withError", () => {
1924
it("sets the error prop", () => {
20-
const erroredInput = mount(<Input name="errored" />);
21-
expect(erroredInput.find(TextInput).props().error).toEqual("This is an error");
25+
const erroredInput = mount(<Input name="email" />);
26+
expect(erroredInput.find(TextInput).props().error).toEqual("This is not a valid email.");
2227
const validInput = mount(<Input name="valid" />);
2328
expect(validInput.find(TextInput).props().error).toBeFalsy();
2429
});
2530

31+
it('sets the error prop for nested key name', () => {
32+
const emailInput = mount(<Input name="user.password" />);
33+
expect(emailInput.find(TextInput).props().error).toEqual("Password is too short!");
34+
const otherInput = mount(<Input name="user.username" />);
35+
expect(otherInput.find(TextInput).props().error).toEqual(undefined);
36+
});
37+
2638
it("keeps other props", () => {
2739
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
2840
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");

src/__tests__/withTouched.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,31 @@ console.error = jest.fn();
1010

1111
const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
1212
formik: {
13-
touched: { touchedInput: true }
13+
touched: {
14+
email: true,
15+
user: {
16+
username: true,
17+
}
18+
}
1419
}
1520
}));
1621
const Input = compose(withFormikMock, withTouched)(TextInput);
1722

1823
describe("withTouched", () => {
1924
it("sets the touched prop", () => {
20-
const touchedInput = mount(<Input name="touchedInput" />);
25+
const touchedInput = mount(<Input name="email" />);
2126
expect(touchedInput.find(TextInput).props().touched).toEqual(true);
2227
const untouchedInput = mount(<Input name="untouched" />);
2328
expect(untouchedInput.find(TextInput).props().touched).toBeFalsy();
2429
});
2530

31+
it("sets the touched prop for nested key name", () => {
32+
const touchedInput = mount(<Input name="user.username" />);
33+
expect(touchedInput.find(TextInput).props().touched).toEqual(true);
34+
const untouchedInput = mount(<Input name="user.password" />);
35+
expect(untouchedInput.find(TextInput).props().touched).toBeFalsy();
36+
});
37+
2638
it("keeps other props", () => {
2739
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
2840
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");

src/makeReactNativeField.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { compose, mapProps } from "recompose";
22
import withFormik from "./withFormik";
3+
import { selectValue } from './utils';
34

45
const makeReactNativeField = compose(
56
withFormik,
67
mapProps(({ formik: { setFieldValue, setFieldTouched, values }, name, ...props }) => ({
7-
value: values[name],
8+
value: selectValue(values, name),
89
...props,
910
name,
1011
onChangeText: text => {

src/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const selectValue = (values, name) => {
2+
if (!name.includes('.')) {
3+
return values[name];
4+
} else {
5+
const nestedKeys = name.split('.');
6+
selectedValue = values[nestedKeys[0]];
7+
for (let i = 1; i < nestedKeys.length; i += 1) {
8+
selectedValue = selectedValue[nestedKeys[i]];
9+
}
10+
11+
return selectedValue;
12+
}
13+
};

src/withError.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { compose, mapProps } from "recompose";
22
import withFormik from "./withFormik";
3+
import { selectValue } from './utils';
34

45
const withError = compose(
56
withFormik,
67
mapProps(({ formik: { errors }, name, ...props }) => ({
7-
error: errors[name],
8+
error: selectValue(errors, name),
89
...props,
910
name
1011
}))

src/withTouched.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { compose, mapProps } from "recompose";
22
import withFormik from "./withFormik";
3+
import { selectValue } from './utils';
34

45
const withError = compose(
56
withFormik,
67
mapProps(({ formik: { touched }, name, ...props }) => ({
7-
touched: touched[name],
8+
touched: selectValue(touched, name),
89
...props,
910
name
1011
}))

0 commit comments

Comments
 (0)