-
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.
useScope will now return current scope when nothing is passed in
- Loading branch information
Showing
6 changed files
with
159 additions
and
2 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
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,67 @@ | ||
# useScope | ||
|
||
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 you are in, this can be done with `useScope`. | ||
|
||
Note: you can also use this to scope a given string! | ||
|
||
<!-- STORY --> | ||
|
||
```jsx | ||
import { Form, Input, Scope, useScope } from 'informed'; | ||
|
||
const initialValues = { | ||
name: 'Elon', | ||
age: 50, | ||
spouse: { | ||
name: 'Talulah', | ||
age: 36 | ||
}, | ||
mother: { | ||
name: 'Maye', | ||
age: 74 | ||
} | ||
}; | ||
|
||
const WhereAmI = () => { | ||
const scope = useScope(); | ||
return ( | ||
<div style={{ margin: '1rem' }}> | ||
You are within <strong>{scope}</strong>s scope. | ||
</div> | ||
); | ||
}; | ||
|
||
const ScopeThis = ({ name }) => { | ||
const scopedName = useScope(name); | ||
return ( | ||
<div style={{ margin: '1rem' }}> | ||
Scoped name <strong>{scopedName}</strong>. | ||
</div> | ||
); | ||
}; | ||
|
||
const ScopeComonent = () => ( | ||
<Form initialValues={initialValues}> | ||
<h3>Your Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
<Scope scope="spouse"> | ||
<h3>Spouses Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
</Scope> | ||
<br /> | ||
<Scope scope="mother"> | ||
<h3>Mothers Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
</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,74 @@ | ||
import React, { useRef } from 'react'; | ||
import withDocs from '../../utils/withDocs'; | ||
import readme from './README.md'; | ||
|
||
import { Form, Input, Scope, useScope } from '../../../src'; | ||
|
||
const initialValues = { | ||
name: 'Elon', | ||
age: 50, | ||
spouse: { | ||
name: 'Talulah', | ||
age: 36 | ||
}, | ||
mother: { | ||
name: 'Maye', | ||
age: 74 | ||
} | ||
}; | ||
|
||
const WhereAmI = () => { | ||
const scope = useScope(); | ||
return ( | ||
<div style={{ margin: '1rem' }}> | ||
You are within <strong>{scope}</strong>s scope. | ||
</div> | ||
); | ||
}; | ||
|
||
const ScopeThis = ({ name }) => { | ||
const scopedName = useScope(name); | ||
return ( | ||
<div style={{ margin: '1rem' }}> | ||
Scoped name <strong>{scopedName}</strong>. | ||
</div> | ||
); | ||
}; | ||
|
||
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" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
<Scope scope="spouse"> | ||
<h3>Spouses Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
</Scope> | ||
<br /> | ||
<Scope scope="mother"> | ||
<h3>Mothers Info</h3> | ||
<Input name="name" label="First name:" /> | ||
<Input name="age" label="Age:" type="number" /> | ||
<WhereAmI /> | ||
<ScopeThis name="hello" /> | ||
</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