Skip to content

Commit

Permalink
chore: rename decoupleFormElement to decoupleForm (#4341)
Browse files Browse the repository at this point in the history
Fixes a missing commit for PR #4332
  • Loading branch information
tujoworker authored Nov 25, 2024
1 parent ebad212 commit aa71556
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function MyForm() {

## Decoupling the form element

For more flexibility, you can decouple the form element from the form context by using the `decoupleFormElement` property. It is recommended to use the `Form.Element` to wrap your rest of your form:
For more flexibility, you can decouple the form element from the form context by using the `decoupleForm` property. It is recommended to use the `Form.Element` to wrap your rest of your form:

```jsx
import { Form } from '@dnb/eufemia/extensions/forms'

function MyApp() {
return (
<Form.Handler decoupleFormElement>
<Form.Handler decoupleForm>
<AppRelatedThings>
<Form.Element>
<Form.MainHeading>Heading</Form.MainHeading>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export interface ContextState {
required?: boolean
submitState: Partial<EventStateObject>
prerenderFieldProps?: boolean
decoupleFormElement?: boolean
decoupleForm?: boolean
hasElementRef?: React.MutableRefObject<boolean>
restHandlerProps?: Record<string, unknown>
props: ProviderProps<unknown>
Expand Down
20 changes: 8 additions & 12 deletions packages/dnb-eufemia/src/extensions/forms/Form/Handler/Handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export type Props = FormElementProps & {
/**
* Will decouple the form element from rendering
*/
decoupleFormElement?: boolean
decoupleForm?: boolean
}

type AllowedProviderContextProps = ProviderProps<unknown> &
Pick<Props, 'decoupleFormElement' | 'autoComplete' | 'disabled'> &
Pick<Props, 'decoupleForm' | 'autoComplete' | 'disabled'> &
Pick<ContextState, 'restHandlerProps' | 'hasElementRef'>

const allowedProviderContextProps: Array<
Expand Down Expand Up @@ -51,21 +51,21 @@ const allowedProviderContextProps: Array<
'autoComplete',
'disabled',
'required',
'decoupleFormElement',
'decoupleForm',
'restHandlerProps',
]

export default function FormHandler<Data extends JsonObject>(
props: ProviderProps<Data> & Omit<Props, keyof ProviderProps<Data>>
) {
const { decoupleFormElement, children } = props
const { decoupleForm, children } = props

const hasElementRef = useRef(false)
useEffect(() => {
if (decoupleFormElement && !hasElementRef.current) {
warn('Please include a Form.Element when using decoupleFormElement!')
if (decoupleForm && !hasElementRef.current) {
warn('Please include a Form.Element when using decoupleForm!')
}
}, [decoupleFormElement])
}, [decoupleForm])

const providerProps = {
hasElementRef,
Expand All @@ -86,11 +86,7 @@ export default function FormHandler<Data extends JsonObject>(

return (
<DataContextProvider {...providerProps}>
{decoupleFormElement ? (
children
) : (
<FormElement>{children}</FormElement>
)}
{decoupleForm ? children : <FormElement>{children}</FormElement>}
</DataContextProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1161,10 +1161,10 @@ describe('Form.Handler', () => {
})
})

describe('decoupleFormElement', () => {
it('should contain one form element', () => {
describe.only('decoupleForm', () => {

Check failure on line 1164 in packages/dnb-eufemia/src/extensions/forms/Form/Handler/__tests__/Handler.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

Unexpected focused test
it.only('should contain one form element', () => {

Check failure on line 1165 in packages/dnb-eufemia/src/extensions/forms/Form/Handler/__tests__/Handler.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

Unexpected focused test
render(
<Form.Handler decoupleFormElement>
<Form.Handler>
<Form.Element>content</Form.Element>
</Form.Handler>
)
Expand All @@ -1177,7 +1177,7 @@ describe('Form.Handler', () => {
const onSubmit = jest.fn()

render(
<Form.Handler decoupleFormElement onSubmit={onSubmit}>
<Form.Handler decoupleForm onSubmit={onSubmit}>
<Form.Element>content</Form.Element>
</Form.Handler>
)
Expand All @@ -1189,7 +1189,7 @@ describe('Form.Handler', () => {

it('should spread rest props to form element', () => {
render(
<Form.Handler decoupleFormElement aria-label="Aria Label">
<Form.Handler decoupleForm aria-label="Aria Label">
<Form.Element>content</Form.Element>
</Form.Handler>
)
Expand All @@ -1202,7 +1202,7 @@ describe('Form.Handler', () => {

it('should overwrite rest props from handler', () => {
render(
<Form.Handler decoupleFormElement aria-label="Aria Label">
<Form.Handler decoupleForm aria-label="Aria Label">
<Form.Element aria-label="Overwrite">content</Form.Element>
</Form.Handler>
)
Expand All @@ -1215,7 +1215,7 @@ describe('Form.Handler', () => {

it('should render form element inside wrapper', () => {
render(
<Form.Handler decoupleFormElement>
<Form.Handler decoupleForm>
<div className="wrapper">
<Form.Element>content</Form.Element>
</div>
Expand All @@ -1229,12 +1229,12 @@ describe('Form.Handler', () => {
it('should warn when no form element is found', () => {
const log = jest.spyOn(global.console, 'log').mockImplementation()

render(<Form.Handler decoupleFormElement>content</Form.Handler>)
render(<Form.Handler decoupleForm>content</Form.Handler>)

expect(log).toHaveBeenCalledTimes(1)
expect(log).toHaveBeenCalledWith(
expect.any(String),
'Please include a Form.Element when using decoupleFormElement!'
'Please include a Form.Element when using decoupleForm!'
)

log.mockRestore()
Expand Down

0 comments on commit aa71556

Please sign in to comment.