-
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(Forms): deprecate Form.useError in favor of
Form.useValidation
…
… featuring `setFieldStatus` to handle the status (error) of a single field (#3986)
- Loading branch information
1 parent
0c121f1
commit dd39eef
Showing
19 changed files
with
843 additions
and
394 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
24 changes: 0 additions & 24 deletions
24
...ages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Form/useError.mdx
This file was deleted.
Oops, something went wrong.
86 changes: 0 additions & 86 deletions
86
...dnb-design-system-portal/src/docs/uilib/extensions/forms/Form/useError/info.mdx
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
...dnb-design-system-portal/src/docs/uilib/extensions/forms/Form/useValidation.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,26 @@ | ||
--- | ||
title: 'useValidation' | ||
description: '`Form.useValidation` lets you monitor and modify field status or your form errors outside of the context.' | ||
hideInMenu: true | ||
showTabs: true | ||
tabs: | ||
- title: Info | ||
key: '/info' | ||
- title: Demos | ||
key: '/demos' | ||
breadcrumb: | ||
- text: Forms | ||
href: /uilib/extensions/forms/ | ||
- text: Form | ||
href: /uilib/extensions/forms/Form/ | ||
- text: Form.useValidation | ||
href: /uilib/extensions/forms/Form/useValidation/ | ||
redirect_from: | ||
- /uilib/extensions/forms/Form/useError | ||
--- | ||
|
||
import Info from 'Docs/uilib/extensions/forms/Form/useValidation/info' | ||
import Demos from 'Docs/uilib/extensions/forms/Form/useValidation/demos' | ||
|
||
<Info /> | ||
<Demos /> |
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
129 changes: 129 additions & 0 deletions
129
...esign-system-portal/src/docs/uilib/extensions/forms/Form/useValidation/info.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,129 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
## Description | ||
|
||
The `Form.useValidation` lets you monitor and modify field status or your form errors outside of the context. | ||
|
||
## Usage | ||
|
||
You can use it in several ways. Like within the context of `Form.Handler`: | ||
|
||
```jsx | ||
import { Form } from '@dnb/eufemia/extensions/forms' | ||
|
||
function MyForm() { | ||
return ( | ||
<Form.Handler> | ||
... | ||
<Component /> | ||
... | ||
</Form.Handler> | ||
) | ||
} | ||
|
||
function Component() { | ||
const { hasErrors, hasFieldError, setFormError, setFieldStatus } = | ||
Form.useValidation() | ||
|
||
// True if any error is present | ||
hasErrors() | ||
|
||
// True if the field has an error | ||
hasFieldError('/path/to/field') | ||
|
||
// Report a form error | ||
setFormError(new Error('This is a global form error')) | ||
|
||
// Show a field error | ||
setFieldStatus('/path/to/field', { | ||
error: new Error('This is a field error'), | ||
warning: 'This is a field warning', | ||
info: 'This is a field info', | ||
}) | ||
} | ||
``` | ||
|
||
Or by linking the hook together with the form by using the `id` (string) property: | ||
|
||
```jsx | ||
import { Form } from '@dnb/eufemia/extensions/forms' | ||
|
||
function MyForm() { | ||
return ( | ||
<> | ||
<Form.Handler id="unique">...</Form.Handler> | ||
<Component /> | ||
</> | ||
) | ||
} | ||
|
||
function Component() { | ||
const { hasErrors, hasFieldError } = Form.useValidation('unique') | ||
} | ||
``` | ||
|
||
Or by using it in the form component itself: | ||
|
||
```jsx | ||
import { Form } from '@dnb/eufemia/extensions/forms' | ||
|
||
function MyForm() { | ||
const { hasErrors } = Form.useValidation('unique') | ||
|
||
return <Form.Handler id="unique">...</Form.Handler> | ||
} | ||
``` | ||
|
||
## Report a form error | ||
|
||
You can also report a form error that gets displayed on the bottom of the form by using the `Form.useValidation` hook: | ||
|
||
```jsx | ||
import { Form } from '@dnb/eufemia/extensions/forms' | ||
|
||
function MyForm() { | ||
const { setFormError } = Form.useValidation('unique') | ||
|
||
useEffect(() => { | ||
setFormError('This is a global form error') | ||
}, []) | ||
|
||
return <Form.Handler id="unique">...</Form.Handler> | ||
} | ||
``` | ||
|
||
## Field status | ||
|
||
You can also use the `setFieldStatus` method to report field status. This will update the field with the status and show it in the form. | ||
|
||
```jsx | ||
import { Form, Field } from '@dnb/eufemia/extensions/forms' | ||
|
||
function Component() { | ||
const { setFieldStatus } = Form.useValidation('unique') | ||
|
||
return ( | ||
<Form.Handler | ||
id="unique" | ||
onSubmit={async () => { | ||
// Report a field status | ||
setFieldStatus('/path/to/field', { | ||
error: new Error('This is a field error'), | ||
warning: 'This is a field warning', | ||
info: 'This is a field info', | ||
}) | ||
}} | ||
> | ||
<Field.String path="/path/to/field" /> | ||
</Form.Handler> | ||
) | ||
} | ||
``` | ||
|
||
To remove the field status, you can use `setFieldStatus('/path/to/field', { error: null })`. | ||
|
||
## Accessibility | ||
|
||
The form error is connected with the [Form.Handler](/uilib/extensions/forms/Form/Handler/) itself via `aria-labelledby` for screen reader support. |
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
Oops, something went wrong.