Skip to content

Commit

Permalink
fix: Handle nested field name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tketa authored and Almouro committed Jul 10, 2018
1 parent da1108d commit 7fd4096
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/__tests__/makeReactNativeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
formik: {
setFieldValue,
setFieldTouched,
values: { email: "[email protected]" }
values: {
email: "[email protected]",
user: {
username: "bam-dev",
password: 'goodchallenge',
}
}
}
}));
const Input = compose(withFormikMock, makeReactNativeField)(TextInput);
Expand All @@ -28,6 +34,13 @@ describe("makeReactNativeField", () => {
expect(otherInput.find(TextInput).props().value).toEqual(undefined);
});

it('sets the input value for nested key name', () => {
const emailInput = mount(<Input name="user.username" />);
expect(emailInput.find(TextInput).props().value).toEqual("bam-dev");
const otherInput = mount(<Input name="user.other" />);
expect(otherInput.find(TextInput).props().value).toEqual(undefined);
});

it("handles input value change", () => {
const wrapper = mount(<Input name="email" />);
wrapper
Expand Down
18 changes: 15 additions & 3 deletions src/__tests__/withError.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ console.error = jest.fn();

const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
formik: {
errors: { errored: "This is an error" }
errors: {
email: "This is not a valid email.",
user: {
password: "Password is too short!"
}
}
}
}));
const Input = compose(withFormikMock, withError)(TextInput);

describe("withError", () => {
it("sets the error prop", () => {
const erroredInput = mount(<Input name="errored" />);
expect(erroredInput.find(TextInput).props().error).toEqual("This is an error");
const erroredInput = mount(<Input name="email" />);
expect(erroredInput.find(TextInput).props().error).toEqual("This is not a valid email.");
const validInput = mount(<Input name="valid" />);
expect(validInput.find(TextInput).props().error).toBeFalsy();
});

it('sets the error prop for nested key name', () => {
const emailInput = mount(<Input name="user.password" />);
expect(emailInput.find(TextInput).props().error).toEqual("Password is too short!");
const otherInput = mount(<Input name="user.username" />);
expect(otherInput.find(TextInput).props().error).toEqual(undefined);
});

it("keeps other props", () => {
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");
Expand Down
16 changes: 14 additions & 2 deletions src/__tests__/withTouched.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ console.error = jest.fn();

const withFormikMock = withContext({ formik: PropTypes.object }, () => ({
formik: {
touched: { touchedInput: true }
touched: {
email: true,
user: {
username: true,
}
}
}
}));
const Input = compose(withFormikMock, withTouched)(TextInput);

describe("withTouched", () => {
it("sets the touched prop", () => {
const touchedInput = mount(<Input name="touchedInput" />);
const touchedInput = mount(<Input name="email" />);
expect(touchedInput.find(TextInput).props().touched).toEqual(true);
const untouchedInput = mount(<Input name="untouched" />);
expect(untouchedInput.find(TextInput).props().touched).toBeFalsy();
});

it("sets the touched prop for nested key name", () => {
const touchedInput = mount(<Input name="user.username" />);
expect(touchedInput.find(TextInput).props().touched).toEqual(true);
const untouchedInput = mount(<Input name="user.password" />);
expect(untouchedInput.find(TextInput).props().touched).toBeFalsy();
});

it("keeps other props", () => {
const wrapper = mount(<Input name="inputName" someProp="someValue" />);
expect(wrapper.find(TextInput).props().someProp).toEqual("someValue");
Expand Down
3 changes: 2 additions & 1 deletion src/makeReactNativeField.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { compose, mapProps } from "recompose";
import withFormik from "./withFormik";
import { selectValue } from './utils';

const makeReactNativeField = compose(
withFormik,
mapProps(({ formik: { setFieldValue, setFieldTouched, values }, name, ...props }) => ({
value: values[name],
value: selectValue(values, name),
...props,
name,
onChangeText: text => {
Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const selectValue = (values, name) => {
if (!name.includes('.')) {
return values[name];
} else {
const nestedKeys = name.split('.');
selectedValue = values[nestedKeys[0]];
for (let i = 1; i < nestedKeys.length; i += 1) {
selectedValue = selectedValue[nestedKeys[i]];
}

return selectedValue;
}
};
3 changes: 2 additions & 1 deletion src/withError.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { compose, mapProps } from "recompose";
import withFormik from "./withFormik";
import { selectValue } from './utils';

const withError = compose(
withFormik,
mapProps(({ formik: { errors }, name, ...props }) => ({
error: errors[name],
error: selectValue(errors, name),
...props,
name
}))
Expand Down
3 changes: 2 additions & 1 deletion src/withTouched.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { compose, mapProps } from "recompose";
import withFormik from "./withFormik";
import { selectValue } from './utils';

const withError = compose(
withFormik,
mapProps(({ formik: { touched }, name, ...props }) => ({
touched: touched[name],
touched: selectValue(touched, name),
...props,
name
}))
Expand Down

0 comments on commit 7fd4096

Please sign in to comment.