Skip to content

Commit

Permalink
feat(Forms): add Form.Handler component (#2644)
Browse files Browse the repository at this point in the history
- [x] Add `Form.Handler` that combines `Form.Element` and
`DataContext.Provider`.
- [x] Add `Form.Handler` for a vanilla HTML form element.
- [x] Align docs to use `Form.Handler` over `DataContext.Provider`.
- [x] Move `advanced-blocks` to `extended-features`.

---------

Co-authored-by: Anders <[email protected]>
  • Loading branch information
tujoworker and langz authored Sep 13, 2023
1 parent 56a1867 commit 82873c7
Show file tree
Hide file tree
Showing 109 changed files with 1,139 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ The goal of the Forms extension is to make it easy to build forms and other data
### Additional features

- [Feature fields](/uilib/extensions/forms/feature-fields) such as Email and PhoneNumber.
- [Extended features](/uilib/extensions/forms/extended-features) such as the main [Form.Handler](/uilib/extensions/forms/extended-features/Form/Handler) and [StepsLayout](/uilib/extensions/forms/extended-features/StepsLayout) for creating stepped forms.
- [Layout](/uilib/extensions/forms/Layout) to apply default styles for application forms.
- [StepsLayout](/uilib/extensions/forms/advanced-blocks/StepsLayout) to create stepped forms.

### Value components

Expand Down Expand Up @@ -75,9 +75,9 @@ Or save time and code when handling your forms:

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { DataContext, Field } from '@dnb/eufemia/extensions/forms'
import { Form, Field } from '@dnb/eufemia/extensions/forms'
render(
<DataContext.Provider
<Form.Handler
data={existingData}
onChange={...}
onSubmit={...}
Expand All @@ -100,21 +100,21 @@ render(
<Field.Option value="other" title="Annet" />
</Field.Selection>
</Layout.Card>
</DataContext.Provider>,
</Form.Handler>,
)
```

But the real strong parts about Eufemia Forms is the composability when you create your own components:

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { DataContext, Field } from '@dnb/eufemia/extensions/forms'
import { Form, Field } from '@dnb/eufemia/extensions/forms'
render(
<DataContext.Provider data={existingData} onSubmit={...}>
<Form.Handler data={existingData} onSubmit={...}>
<Layout.Card spacing="medium">
<MyCustomComponent path="/companyName" required />
</Layout.Card>
</DataContext.Provider>,
</Form.Handler>,
)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import { useCallback } from 'react'
import ComponentBox from '../../../../shared/tags/ComponentBox'
import { Input, Slider } from '@dnb/eufemia/src'
import {
DataContext,
Form,
Layout,
StepsLayout,
Field,
Value,
Visibility,
FieldBlock,
useDataValue,
DataContext,
} from '@dnb/eufemia/src/extensions/forms'

export const CreateBasicFieldComponent = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
Field,
FieldBlock,
Expand Down Expand Up @@ -158,7 +159,7 @@ export const BaseFieldComponents = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Expand Down Expand Up @@ -191,7 +192,7 @@ export const FeatureFields = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Expand All @@ -214,7 +215,7 @@ export const LayoutComponents = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Expand Down Expand Up @@ -248,15 +249,15 @@ export const VisibilityBasedOnData = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Value,
Visibility,
}}
>
<DataContext.Provider
<Form.Handler
data={{
firstName: undefined,
lastName: 'Smith',
Expand Down Expand Up @@ -297,24 +298,24 @@ export const VisibilityBasedOnData = () => {
</Layout.Card>
</Layout.Section>
</Visibility>
</DataContext.Provider>
</Form.Handler>
</ComponentBox>
)
}

export const UsingDataContextProvider = () => {
export const UsingFormHandler = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Value,
Visibility,
}}
>
<DataContext.Provider
<Form.Handler
data={{
firstName: 'John',
lastName: 'Smith',
Expand All @@ -338,10 +339,10 @@ export const UsingDataContextProvider = () => {
<Field.PhoneNumber path="/phone" />

<Layout.ButtonRow>
<DataContext.SubmitButton />
<Form.SubmitButton />
</Layout.ButtonRow>
</Layout.Card>
</DataContext.Provider>
</Form.Handler>
</ComponentBox>
)
}
Expand All @@ -350,15 +351,15 @@ export const Validation = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Value,
Visibility,
}}
>
<DataContext.Provider
<Form.Handler
data={{
firstName: undefined,
lastName: 'Smith',
Expand All @@ -381,7 +382,7 @@ export const Validation = () => {
<Field.Email path="/email" validateInitially />
<Field.PhoneNumber path="/phone" validateInitially />
</Layout.Card>
</DataContext.Provider>
</Form.Handler>
</ComponentBox>
)
}
Expand All @@ -390,15 +391,15 @@ export const WithSteps = () => {
return (
<ComponentBox
scope={{
DataContext,
Form,
Layout,
StepsLayout,
Field,
Value,
Visibility,
}}
>
<DataContext.Provider
<Form.Handler
data={{
firstName: undefined,
lastName: 'Smith',
Expand Down Expand Up @@ -462,11 +463,11 @@ export const WithSteps = () => {

<Layout.ButtonRow>
<StepsLayout.PreviousButton />
<DataContext.SubmitButton />
<Form.SubmitButton />
</Layout.ButtonRow>
</StepsLayout.Step>
</StepsLayout>
</DataContext.Provider>
</Form.Handler>
</ComponentBox>
)
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 'Create your own component'
order: 3
order: 4
breadcrumb:
- text: Forms
href: /uilib/extensions/forms/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Example of using the `DataContext.Provider`:
</DataContext.Provider>
```

Please use [Form.Handler](/uilib/extensions/forms/extended-features/Form/Handler) for application forms.

## Components

<ListDataContextComponents />
Expand Down
Loading

0 comments on commit 82873c7

Please sign in to comment.