Skip to content

Commit

Permalink
useScope will now return current scope when nothing is passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
joepuzzo committed Feb 23, 2023
1 parent e5501c4 commit 5b6613c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.41.1 (Feb 23rd, 2023)

### Added

- `useScope` will now return current scope when nothing is passed in.

## 4.41.0 (Feb 23rd, 2023)

### Added
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export function useFieldApi(name: string, scoped?: boolean): FieldApi;

export function useFieldState(name: string, scoped?: boolean): FieldState;

export function useScope(name: string): string;
export function useScope(name?: string): string;

export function useScoper(name: string): (name: string) => string;

Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ export function useScope(name) {
if (scope && name) {
return `${scope}.${name}`;
}
return name;

// Return what was passed
if (name) {
return name;
}

// If nothing passed reuturn the scope
return scope;
}
67 changes: 67 additions & 0 deletions stories/Scope/useScope/README.md
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>
);
```
74 changes: 74 additions & 0 deletions stories/Scope/useScope/index.js
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);
3 changes: 3 additions & 0 deletions stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import KeepStateIfRelevant from './KeepState/KeepStateIfRelevant';
import Keep from './KeepState/Keep';
import TableFields from './Conditionals/TableFields';
import useScopedState from './Scope/useScopedState';
import useScope from './Scope/useScope';

addDecorator(StoryWrapper);

Expand Down Expand Up @@ -269,6 +270,7 @@ storiesOf('KeepState', module)

storiesOf('Scope', module)
.add('Scope Comonent', ScopeComponent)
.add('useScope', useScope)
.add('useScopedState', useScopedState);

storiesOf('Schema', module)
Expand Down Expand Up @@ -309,6 +311,7 @@ storiesOf('Hooks!', module)
.add('useForm', UseForm)
.add('useField', UseField)
.add('useFormStateSelector', UseFormStateSelector)
.add('useScope', useScope)
.add('useScopedState', useScopedState);

storiesOf('Multistep Forms', module)
Expand Down

0 comments on commit 5b6613c

Please sign in to comment.