forked from teslamotors/informed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated docs code to have keep state section
- Loading branch information
Showing
8 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Keep | ||
|
||
Sometimes you need to keep the state of a field even when it gets unmounted ( no longer rendered on screen ). In the below example we have a field that gets hidden when the hide button is clicked, when it gets unmounted its state would normally get removed, by passing `keep={{ value: true }}` we can control what state ( if any ) should be kept. | ||
|
||
Note: Click on the "Submit" button. See how the state shows an error and the value. | ||
|
||
Next: Click on the "Hide" button and note fields error state gets removed but NOT the value. | ||
|
||
Next: Click on the "Show" button and note the fields value state comes back but NOT the error state. | ||
|
||
<!-- STORY --> | ||
|
||
```jsx | ||
import { Form, Input, Checkbox, Relevant, Debug } from 'informed'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
return ( | ||
<Form> | ||
{show ? ( | ||
<Input | ||
name="name" | ||
label="Name:" | ||
defaultValue="Hello" | ||
minLength={10} | ||
keep={{ value: true }} | ||
/> | ||
) : null} | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<button type="submit">Submit</button> | ||
<Debug values errors /> | ||
</Form> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useState } from 'react'; | ||
import withDocs from '../../utils/withDocs'; | ||
import readme from './README.md'; | ||
|
||
import { Form, Input, Checkbox, Relevant, Debug } from '../../../src'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
const toggle = () => setShow(prev => !prev); | ||
|
||
return ( | ||
<Form> | ||
{show ? ( | ||
<Input | ||
name="name" | ||
label="Name:" | ||
defaultValue="Hello" | ||
minLength={10} | ||
keep={{ value: true }} | ||
/> | ||
) : null} | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<button type="submit">Submit</button> | ||
<Debug values errors /> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default withDocs(readme, Example); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Keep State | ||
|
||
Sometimes you need to keep the state of a field even when it gets unmounted ( no longer rendered on screen ). In the below example, when you toggle showing the fields the `name1` field will get removed from the form state but the name2 fields state will be kept. | ||
|
||
Note: Fill in both fields then click on the hide button and then show. Pay attention to how name2's state stays but name1's gets removed | ||
|
||
<!-- STORY --> | ||
|
||
```jsx | ||
import { Form, Input, Debug } from 'informed'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
return ( | ||
<Form> | ||
{show ? <Input name="name1" label="Name:" /> : null} | ||
{show ? <Input name="name2" label="Name:" keepState /> : null} | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<Debug values /> | ||
</Form> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { useState } from 'react'; | ||
import withDocs from '../../utils/withDocs'; | ||
import readme from './README.md'; | ||
|
||
import { Form, Input, Debug } from '../../../src'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
const toggle = () => setShow(prev => !prev); | ||
|
||
return ( | ||
<Form> | ||
{show ? <Input name="name1" label="Name:" /> : null} | ||
{show ? <Input name="name2" label="Name:" keepState /> : null} | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<Debug values /> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default withDocs(readme, Example); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Keep State If Relevant | ||
|
||
Sometimes you need to keep the state of a field even when it gets unmounted ( no longer rendered on screen ). In the below example, when you toggle showing the fields the `name1` field will get removed from the form state but the name2 fields state will be kept. However if that field is irrelevant it will NOT be kept. | ||
|
||
Note: Fill in both fields then click on the hide button and then show. Pay attention to how name2's state stays but name1's gets removed | ||
|
||
Next: Click on the toggle input and note how name2's state gets removed as its irrelevant. | ||
|
||
<!-- STORY --> | ||
|
||
```jsx | ||
import { Form, Input, Checkbox, Relevant, Debug } from 'informed'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
return ( | ||
<Form> | ||
{show ? <Input name="name1" label="Name:" /> : null} | ||
<Checkbox name="show" label="Show" defaultValue={true} /> | ||
<Relevant when={({ formState }) => formState.values.show}> | ||
{show ? <Input name="name2" label="Name:" keepStateIfRelevant /> : null} | ||
</Relevant> | ||
<br /> | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<Debug values /> | ||
</Form> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React, { useState } from 'react'; | ||
import withDocs from '../../utils/withDocs'; | ||
import readme from './README.md'; | ||
|
||
import { Form, Input, Checkbox, Relevant, Debug } from '../../../src'; | ||
|
||
const Example = () => { | ||
const [show, setShow] = useState(true); | ||
|
||
const toggle = () => setShow(prev => !prev); | ||
|
||
return ( | ||
<Form> | ||
{show ? <Input name="name1" label="Name:" /> : null} | ||
<Checkbox name="show" label="Show" defaultValue={true} /> | ||
<Relevant when={({ formState }) => formState.values.show}> | ||
{show ? <Input name="name2" label="Name:" keepStateIfRelevant /> : null} | ||
</Relevant> | ||
<br /> | ||
<button type="button" onClick={toggle}> | ||
{show ? 'Hide' : 'Show'} | ||
</button> | ||
<Debug values /> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default withDocs(readme, Example); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters