-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed issue where resetting array field with no values would never se…
…t to new initial values
- Loading branch information
Showing
6 changed files
with
158 additions
and
0 deletions.
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
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
38 changes: 38 additions & 0 deletions
38
...cs/Pages/Examples/Gotchas/ChangeInitialValuesArrayField/ChangeInitialValuesArrayField.jsx
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 @@ | ||
import { useEffect } from 'react'; | ||
import Example from './Example'; | ||
import exampleCode from './Example.jsx?raw'; | ||
import Code from '../../../../YourComponents/Code'; | ||
import { SideBySide } from '../../../../SideBySide'; | ||
import { Info } from '../../../../Info'; | ||
|
||
export default function ChangeInitialValuesArrayField() { | ||
useEffect(() => { | ||
window.scrollTo(0, 0); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<h1> | ||
<code>Changing initialValues in Array Fields</code> | ||
</h1> | ||
<Info> | ||
Commonly you will have use cases where you want the values to change | ||
when the users has made a different selection. | ||
<br /> | ||
Below we have example where there is a profile editor. | ||
<br /> | ||
Depending on which profile you would like to edit, you want to pre | ||
populate the profile form with that users profile information. | ||
</Info> | ||
|
||
<SideBySide | ||
leftHeader={<h3>Example:</h3>} | ||
rightHeader={<h3>Code:</h3>} | ||
left={<Example />} | ||
right={<Code links input1={exampleCode} />} | ||
/> | ||
<br /> | ||
<br /> | ||
</> | ||
); | ||
} |
92 changes: 92 additions & 0 deletions
92
vitedocs/Pages/Examples/Gotchas/ChangeInitialValuesArrayField/Example.jsx
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,92 @@ | ||
import { Input, Debug, Select, ArrayField } from 'informed'; | ||
import { Form, Button } from 'YourComponents'; | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
|
||
const profiles = [ | ||
{ | ||
name: 'Bob', | ||
age: 25 | ||
}, | ||
{ | ||
name: 'Kimbal', | ||
age: 47, | ||
friends: [{ name: 'Bill' }, { name: 'Bob' }] | ||
}, | ||
{ | ||
name: 'Elon', | ||
age: 48, | ||
friends: [{ name: 'Shiban' }, { name: 'Talula' }] | ||
} | ||
]; | ||
|
||
const ProfileForm = ({ profile: initialValues }) => { | ||
// Ref to the form api | ||
const formApiRef = useRef(); | ||
|
||
// Reset the form whenever initial values change ( happens when user selects profile ) | ||
useEffect( | ||
() => { | ||
formApiRef.current.reset(); | ||
}, | ||
[initialValues] | ||
); | ||
|
||
return ( | ||
// Remember to get access to the formApi and pass in the initial values | ||
<Form formApiRef={formApiRef} initialValues={initialValues}> | ||
<Input name="name" label="First Name" /> | ||
<Input type="number" name="age" label="Age" /> | ||
<ArrayField name="friends"> | ||
{({ add }) => { | ||
return ( | ||
<> | ||
<Button | ||
onClick={add} | ||
type="button" | ||
variant="accent" | ||
style="outline"> | ||
Add | ||
</Button> | ||
<ArrayField.Items> | ||
{({ remove, name }) => ( | ||
<> | ||
<h4>{name}</h4> | ||
<Input name="name" label="Name" required /> | ||
<Button type="button" onClick={remove} variant="negative"> | ||
Remove | ||
</Button> | ||
</> | ||
)} | ||
</ArrayField.Items> | ||
</> | ||
); | ||
}} | ||
</ArrayField> | ||
<Button type="submit">submit</Button> | ||
<Debug values /> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default function Profiles() { | ||
// Select the first profile by default | ||
const [selectedProfile, setSelectedProfile] = useState(profiles[0]); | ||
|
||
const selectProfile = ({ value }) => setSelectedProfile(profiles[value]); | ||
|
||
return ( | ||
<React.Fragment> | ||
<h2>Select Profile</h2> | ||
<Form> | ||
{/* A rare case where we want to track the value instead of rcf ( onChange ) */} | ||
<Select name="profile" label="Profile" onChange={selectProfile}> | ||
{profiles.map((profile, i) => ( | ||
<option value={i}>{profile.name}</option> | ||
))} | ||
</Select> | ||
</Form> | ||
<h2>Edit {selectedProfile.name}</h2> | ||
<ProfileForm profile={selectedProfile} /> | ||
</React.Fragment> | ||
); | ||
} |
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