Skip to content

Commit

Permalink
fix(forms): solve issue when schema is used in Wizard (#3680)
Browse files Browse the repository at this point in the history
> Cannot access 'errorMessagesRef' before initialization
  • Loading branch information
tujoworker authored Jun 7, 2024
1 parent 437ba83 commit 8699b0c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,60 @@ describe('Wizard.Container', () => {
log.mockRestore()
})

it('should support schema', async () => {
const schema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
bar: {
type: 'string',
},
},
required: ['foo', 'bar'],
} as const

render(
<Form.Handler schema={schema}>
<Wizard.Container>
<Wizard.Step title="Step 1">
<output>Step 1</output>
<Field.String path="/foo" />
<Wizard.Buttons />
</Wizard.Step>
<Wizard.Step title="Step 2">
<output>Step 2</output>
<Field.String path="/bar" />
<Wizard.Buttons />
</Wizard.Step>
</Wizard.Container>
<Form.SubmitButton />
</Form.Handler>
)

expect(output()).toHaveTextContent('Step 1')
expect(screen.queryByRole('alert')).toBeNull()

fireEvent.click(nextButton())

expect(output()).toHaveTextContent('Step 1')
expect(screen.queryByRole('alert')).toBeInTheDocument()

await userEvent.type(document.querySelector('input'), 'valid')
expect(screen.queryByRole('alert')).toBeNull()

await userEvent.click(nextButton())

expect(output()).toHaveTextContent('Step 2')
expect(screen.queryByRole('alert')).toBeNull()

await userEvent.click(submitButton())

expect(output()).toHaveTextContent('Step 2')
expect(screen.queryByRole('alert')).toBeInTheDocument()
})

describe('prerenderFieldProps and filterData', () => {
it('should keep field props in memory during step change', async () => {
const filterDataHandler = jest.fn(({ props }) => {
Expand Down
17 changes: 9 additions & 8 deletions packages/dnb-eufemia/src/extensions/forms/hooks/useFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ export default function useFieldProps<
schema ? dataContext.ajvInstance?.compile(schema) : undefined
)

// Needs to be placed before "prepareError"
const errorMessagesRef = useRef(null)
errorMessagesRef.current = useMemo(() => {
return {
required: translation.Field.errorRequired,
...errorMessages,
}
}, [errorMessages, translation.Field.errorRequired])

// - Async behavior
const asyncBehaviorIsEnabled = useMemo(() => {
return isAsync(onChange) || isAsync(onChangeContext)
Expand Down Expand Up @@ -406,14 +415,6 @@ export default function useFieldProps<
)
}, [errorProp])

const errorMessagesRef = useRef(null)
errorMessagesRef.current = useMemo(() => {
return {
required: translation.Field.errorRequired,
...errorMessages,
}
}, [errorMessages, translation.Field.errorRequired])

/**
* Based on validation, update error state, locally and relevant surrounding contexts
*/
Expand Down

0 comments on commit 8699b0c

Please sign in to comment.