-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form.useData): add hook to get forms data outside of the context (…
…#3218) How to use it: ```jsx import { Form } from '@dnb/eufemia/extensions/forms' function Component() { const { data, update } = Form.useData('unique', initialData = undefined) return ( <Form.Handler id="unique" ... > ... </Form.Handler> ) } ```
- Loading branch information
1 parent
9d199bd
commit 58c77cd
Showing
25 changed files
with
1,006 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,21 @@ Example using the [Form.Handler](/uilib/extensions/forms/extended-features/Form/ | |
|
||
```jsx | ||
import { Form, Field, Value } from '@dnb/eufemia/extensions/forms' | ||
render( | ||
<Form.Handler data={existingData} onSubmit={submitHandler}> | ||
<Field.Email path="/email" /> | ||
<Value.Date path="/date" /> | ||
<Form.SubmitButton /> | ||
</Form.Handler>, | ||
) | ||
const existingData={ | ||
email: '[email protected]' | ||
date: '2024-01-01' | ||
} | ||
function Component() { | ||
const { data } = Form.useData('unique') | ||
|
||
return ( | ||
<Form.Handler id="unique" data={existingData} onSubmit={submitHandler}> | ||
<Field.Email path="/email" /> | ||
<Value.Date path="/date" /> | ||
<Form.SubmitButton /> | ||
</Form.Handler> | ||
) | ||
} | ||
``` | ||
|
||
### Schema validation | ||
|
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
27 changes: 27 additions & 0 deletions
27
...ystem-portal/src/docs/uilib/extensions/forms/extended-features/Form/useData.mdx
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,27 @@ | ||
--- | ||
title: 'useData' | ||
description: '`useData` lets you access or modify your form data outside of the form context within your application' | ||
componentType: 'advanced-api' | ||
hideInMenu: true | ||
showTabs: true | ||
tabs: | ||
- title: Info | ||
key: '/info' | ||
- title: Demos | ||
key: '/demos' | ||
breadcrumb: | ||
- text: Forms | ||
href: /uilib/extensions/forms/ | ||
- text: Extended features | ||
href: /uilib/extensions/forms/extended-features/ | ||
- text: Form | ||
href: /uilib/extensions/forms/extended-features/Form/ | ||
- text: useData | ||
href: /uilib/extensions/forms/extended-features/Form/useData/ | ||
--- | ||
|
||
import Info from 'Docs/uilib/extensions/forms/extended-features/Form/useData/info' | ||
import Demos from 'Docs/uilib/extensions/forms/extended-features/Form/useData/demos' | ||
|
||
<Info /> | ||
<Demos /> |
98 changes: 98 additions & 0 deletions
98
...system-portal/src/docs/uilib/extensions/forms/extended-features/Form/useData/Examples.tsx
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,98 @@ | ||
import React from 'react' | ||
import ComponentBox from '../../../../../../../shared/tags/ComponentBox' | ||
import { Button, Flex } from '@dnb/eufemia/src' | ||
import { Form, Field } from '@dnb/eufemia/src/extensions/forms' | ||
|
||
export function Default() { | ||
return ( | ||
<ComponentBox> | ||
{() => { | ||
const existingData = { foo: 'bar' } | ||
|
||
const Component = () => { | ||
const { data } = Form.useData('default-id', existingData) | ||
|
||
return ( | ||
<Form.Handler id="default-id"> | ||
<Field.String path="/foo" label={data.foo} /> | ||
</Form.Handler> | ||
) | ||
} | ||
|
||
return <Component /> | ||
}} | ||
</ComponentBox> | ||
) | ||
} | ||
|
||
export function Update() { | ||
return ( | ||
<ComponentBox> | ||
{() => { | ||
const existingData = { count: 1 } | ||
|
||
const Component = () => { | ||
const { data, update } = Form.useData('update-id', existingData) | ||
|
||
const increment = React.useCallback(() => { | ||
update('/count', (count) => { | ||
return count + 1 | ||
}) | ||
}, [update]) | ||
|
||
return ( | ||
<Form.Handler id="update-id"> | ||
<Flex.Horizontal> | ||
<Field.Number path="/count" showStepControls /> | ||
<Form.SubmitButton | ||
onClick={increment} | ||
text={'Increment ' + data.count} | ||
/> | ||
</Flex.Horizontal> | ||
</Form.Handler> | ||
) | ||
} | ||
|
||
return <Component /> | ||
}} | ||
</ComponentBox> | ||
) | ||
} | ||
|
||
export function WithoutFormHandler() { | ||
return ( | ||
<ComponentBox> | ||
{() => { | ||
const existingData = { count: 1 } | ||
|
||
const Component = () => { | ||
const { data, update } = Form.useData( | ||
'idependent-id', | ||
existingData, | ||
) | ||
|
||
const increment = React.useCallback(() => { | ||
update('/count', (count) => { | ||
return count + 1 | ||
}) | ||
}, [update]) | ||
|
||
return ( | ||
<Button | ||
on_click={increment} | ||
text={'Increment ' + data.count} | ||
variant="secondary" | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<Flex.Vertical> | ||
<Component /> | ||
<Component /> | ||
</Flex.Vertical> | ||
) | ||
}} | ||
</ComponentBox> | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
...portal/src/docs/uilib/extensions/forms/extended-features/Form/useData/demos.mdx
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,21 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
import * as Examples from './Examples' | ||
|
||
## Demos | ||
|
||
### Set data outside of the form | ||
|
||
<Examples.Default /> | ||
|
||
### Update the data outside of the form | ||
|
||
The update function `update('/count', (count) => count + 1)` has TypeScript support and returns the correct type for `count` (number). | ||
|
||
<Examples.Update /> | ||
|
||
### Shared state without a Form.Handler | ||
|
||
<Examples.WithoutFormHandler /> |
Oops, something went wrong.