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

added session storage option, if user wants to clear the form if brow… #22

Merged
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export const Signup = () =>

### Props

Only two props!
Only three 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


## Author
Expand All @@ -48,5 +49,5 @@ Only two props!

## Todo

- Alternative storages (localForage, sessionStorage)
- Alternative storages (localForage)
- Support AsyncStorage for React Native
46 changes: 0 additions & 46 deletions package-lock.json

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

11 changes: 9 additions & 2 deletions src/formik-persist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isEqual from 'react-fast-compare';
export interface PersistProps {
name: string;
debounce?: number;
isSessionStorage?: boolean;
}

class PersistImpl extends React.Component<
Expand All @@ -17,7 +18,11 @@ class PersistImpl extends React.Component<
};

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

componentDidUpdate(prevProps: PersistProps & { formik: FormikProps<any> }) {
Expand All @@ -27,7 +32,9 @@ class PersistImpl extends React.Component<
}

componentDidMount() {
const maybeState = window.localStorage.getItem(this.props.name);
const maybeState = this.props.isSessionStorage
? window.sessionStorage.getItem(this.props.name)
: window.localStorage.getItem(this.props.name);
if (maybeState && maybeState !== null) {
this.props.formik.setFormikState(JSON.parse(maybeState));
}
Expand Down
31 changes: 30 additions & 1 deletion test/formik-persist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Formik, FormikProps, Form } from 'formik';
const noop = () => {};

describe('Formik Persist', () => {
let node = document.createElement('div');
it('attempts to rehydrate on mount', () => {
let node = document.createElement('div');
(window as any).localStorage = {
getItem: jest.fn(),
setItem: jest.fn(),
Expand All @@ -35,4 +35,33 @@ describe('Formik Persist', () => {
injected.setValues({ name: 'ian' });
expect(injected.values.name).toEqual('ian');
});

it('attempts to rehydrate on mount if session storage is true on props', () => {
let node = document.createElement('div');
(window as any).sessionStorage = {
getItem: jest.fn(),
setItem: jest.fn(),
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} isSessionStorage />
</div>
);
}}
/>,
node
);
expect(window.sessionStorage.getItem).toHaveBeenCalled();
injected.setValues({ name: 'Anuj' });
expect(injected.values.name).toEqual('Anuj');
});
});