-
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.
added useScopedState to allow users to get access to scoped values
- Loading branch information
Showing
7 changed files
with
17,158 additions
and
18,370 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,9 @@ | ||
import { useContext } from 'react'; | ||
import { ScopeContext } from '../Context'; | ||
import { useFieldState } from './useFieldState'; | ||
|
||
/* ----------------------- useScope ----------------------- */ | ||
export function useScopedState() { | ||
const scope = useContext(ScopeContext); | ||
return useFieldState(scope); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# useScopedState | ||
|
||
Sometimes you need to scope specific fields ( group them ). This is easily achieved with the use of `Scope`. But then, you may also want to access that scopes state, this can be done with `useScopedState`. | ||
|
||
<!-- STORY --> | ||
|
||
```jsx | ||
import { Form, Input, Scope, useScopedState } from 'informed'; | ||
|
||
const initialValues = { | ||
name: 'Elon', | ||
age: 50, | ||
spouse: { | ||
name: 'Talulah', | ||
age: 36 | ||
}, | ||
mother: { | ||
name: 'Maye', | ||
age: 74 | ||
} | ||
}; | ||
|
||
const ComponentUsingScopedState = () => { | ||
const state = useScopedState(); | ||
return ( | ||
<pre> | ||
<code>{JSON.stringify(state, null, 2)}</code> | ||
</pre> | ||
); | ||
}; | ||
|
||
const ScopeComonent = () => ( | ||
<Form initialValues={initialValues}> | ||
<h3>Your Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<Scope scope="spouse"> | ||
<h3>Spouses Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<ComponentUsingScopedState /> | ||
</Scope> | ||
<br /> | ||
<Scope scope="mother"> | ||
<h3>Mothers Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<ComponentUsingScopedState /> | ||
</Scope> | ||
</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,61 @@ | ||
import React, { useRef } from 'react'; | ||
import withDocs from '../../utils/withDocs'; | ||
import readme from './README.md'; | ||
|
||
import { Form, Input, Scope, useScopedState } from '../../../src'; | ||
|
||
const initialValues = { | ||
name: 'Elon', | ||
age: 50, | ||
spouse: { | ||
name: 'Talulah', | ||
age: 36 | ||
}, | ||
mother: { | ||
name: 'Maye', | ||
age: 74 | ||
} | ||
}; | ||
|
||
const ComponentUsingScopedState = () => { | ||
const state = useScopedState(); | ||
return ( | ||
<pre> | ||
<code>{JSON.stringify(state, null, 2)}</code> | ||
</pre> | ||
); | ||
}; | ||
|
||
const ScopeComonent = () => { | ||
// const formApiRef = useRef(); | ||
|
||
// const onClick = () => { | ||
// formApiRef.current.resetPath('spouse'); | ||
// }; | ||
|
||
return ( | ||
<Form initialValues={initialValues}> | ||
<h3>Your Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<Scope scope="spouse"> | ||
<h3>Spouses Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<ComponentUsingScopedState /> | ||
</Scope> | ||
<br /> | ||
<Scope scope="mother"> | ||
<h3>Mothers Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<ComponentUsingScopedState /> | ||
</Scope> | ||
{/* <button type="button" onClick={onClick}> | ||
Reset Spouse | ||
</button> */} | ||
</Form> | ||
); | ||
}; | ||
|
||
export default withDocs(readme, ScopeComonent); |
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