1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00
metamask-extension/docs/form_persisting_architecture.md

29 lines
1.4 KiB
Markdown
Raw Normal View History

2016-08-25 19:33:01 +02:00
# Form Persisting Architecture
Since:
- The popup is torn down completely on every click outside of it.
- We have forms with multiple fields (like passwords & seed phrases) that might encourage a user to leave our panel to refer to a password manager.
We cause user friction when we lose the contents of certain forms.
This calls for an architecture of a form component that can completely persist its values to LocalStorage on every relevant change, and restore those values on reopening.
2016-08-25 23:03:34 +02:00
To achieve this, we have defined a class, a subclass of `React.Component`, called `PersistentForm`, and it's stored at `ui/lib/persistent-form.js`.
2016-08-25 19:33:01 +02:00
2016-08-25 23:03:34 +02:00
To use this class, simply take your form component (the component that renders `input`, `select`, or `textarea` elements), and make it subclass from `PersistentForm` instead of `React.Component`.
2016-08-25 21:17:09 +02:00
2016-08-25 23:03:34 +02:00
You can see an example of this in use in `ui/app/first-time/restore-vault.js`.
2016-08-25 21:17:09 +02:00
2016-08-25 23:03:34 +02:00
Additionally, any field whose value should be persisted, should have a `persistentFormId` attribute, which needs to be assigned under a `dataset` key on the main `attributes` hash. For example:
2016-08-25 21:17:09 +02:00
2016-08-25 23:03:34 +02:00
```javascript
return h('textarea.twelve-word-phrase.letter-spacey', {
dataset: {
persistentFormId: 'wallet-seed',
},
})
2016-08-25 21:17:09 +02:00
```
2016-08-25 23:03:34 +02:00
That's it! This field should be persisted to `localStorage` on each `keyUp`, those values should be restored on view load, and the cached values should be cleared when navigating deliberately away from the form.
2016-08-25 19:33:01 +02:00