Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit selected fields from being persisted #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ export const Signup = () =>

### Props

Only three props!
Only four props!

- `name: string`: LocalStorage key to save form state to
- `debounce:? number`: Default is `300`. Number of ms to debounce the function that saves form state.
- `isSessionStorage:? boolean`: default is `false` . Send if you want Session storage inplace of Local storage
- `ignoreFields:? string[]`: Array of fields that will not be persisted to storage.


## Author
Expand Down
22 changes: 18 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"lodash.debounce": "^4.0.8",
"lodash.omit": "^4.5.0",
"react-fast-compare": "^2.0.1"
},
"peerDependencies": {
Expand All @@ -49,6 +50,7 @@
"devDependencies": {
"@types/jest": "20.0.6",
"@types/lodash.debounce": "^4.0.3",
"@types/lodash.omit": "^4.5.6",
"@types/node": "8.0.19",
"@types/react": "^16.4.7",
"@types/react-dom": "^16.0.6",
Expand Down
23 changes: 21 additions & 2 deletions src/formik-persist.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as React from 'react';
import { FormikProps, connect } from 'formik';
import debounce from 'lodash.debounce';
import omit from 'lodash.omit';
import isEqual from 'react-fast-compare';

export interface PersistProps {
name: string;
ignoreFields?: string[];
debounce?: number;
isSessionStorage?: boolean;
}
Expand All @@ -18,13 +20,30 @@ class PersistImpl extends React.Component<
};

saveForm = debounce((data: FormikProps<{}>) => {
const dataToSave = this.omitIgnoredFields(data);
if (this.props.isSessionStorage) {
window.sessionStorage.setItem(this.props.name, JSON.stringify(data));
window.sessionStorage.setItem(
this.props.name,
JSON.stringify(dataToSave)
);
} else {
window.localStorage.setItem(this.props.name, JSON.stringify(data));
window.localStorage.setItem(this.props.name, JSON.stringify(dataToSave));
}
}, this.props.debounce);

omitIgnoredFields = (data: FormikProps<{}>) => {
const { ignoreFields } = this.props;
const { values, touched, errors } = data;
return ignoreFields
? {
...data,
values: omit(values, ignoreFields),
touched: omit(touched, ignoreFields),
errors: omit(errors, ignoreFields),
}
: data;
};

componentDidUpdate(prevProps: PersistProps & { formik: FormikProps<any> }) {
if (!isEqual(prevProps.formik, this.props.formik)) {
this.saveForm(this.props.formik);
Expand Down
31 changes: 31 additions & 0 deletions test/formik-persist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,35 @@ describe('Formik Persist', () => {
injected.setValues({ name: 'Anuj' });
expect(injected.values.name).toEqual('Anuj');
});

it('omits ignored fields', () => {
let node = document.createElement('div');
jest.useFakeTimers();
let state = null;
let setItem = (key: string, value: any) => (state = value);
(window as any).localStorage = {
getItem: jest.fn(),
setItem,
removeItem: jest.fn(),
};
let injected: any;
ReactDOM.render(
<Formik
initialValues={{ name: 'Anuj Sachan' }}
onSubmit={noop}
render={(props: FormikProps<{ name: string }>) => {
injected = props;
return (
<div>
<Persist name="signup" debounce={0} />
</div>
);
}}
/>,
node
);
injected.setValues({ name: 'ciaran' }, { email: 'ian@example.com' });
jest.runAllTimers();
expect(JSON.parse(state).values).toEqual({ name: 'ciaran' });
});
});