Skip to content

Commit

Permalink
fix(Forms): warn on value prop usage on fields inside iterate with it…
Browse files Browse the repository at this point in the history
…emPath (#3886)

Move out the warning from PR #3877 in regards of this issue #3882

---------

Co-authored-by: Anders <anderslangseth@gmail.com>
  • Loading branch information
tujoworker and langz authored Sep 2, 2024
1 parent f768613 commit 116820d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1071,4 +1071,44 @@ describe('Iterate.Array', () => {
expect(container.querySelector('.dnb-flex-container')).toBeNull()
})
})

describe('value and defaultValue', () => {
it('should warn when "value" prop is used', () => {
const log = jest.spyOn(console, 'log').mockImplementation()

render(
<Form.Handler data={['foo']}>
<Iterate.Array path="/">
<Field.String itemPath="/" value="bar" />
</Iterate.Array>
</Form.Handler>
)

expect(log).toHaveBeenCalledWith(
expect.any(String),
'Using value="bar" prop inside Iterate is not supported yet'
)

log.mockRestore()
})

it('should warn when "defaultValue" prop is used', () => {
const log = jest.spyOn(console, 'log').mockImplementation()

render(
<Form.Handler data={['foo']}>
<Iterate.Array path="/">
<Field.String itemPath="/" defaultValue="bar" />
</Iterate.Array>
</Form.Handler>
)

expect(log).toHaveBeenCalledWith(
expect.any(String),
'Using defaultValue="bar" prop inside Iterate is not supported yet'
)

log.mockRestore()
})
})
})
15 changes: 14 additions & 1 deletion packages/dnb-eufemia/src/extensions/forms/hooks/useFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { Context as DataContext, ContextState } from '../DataContext'
import { clearedData } from '../DataContext/Provider/Provider'
import FieldPropsContext from '../Form/FieldProps/FieldPropsContext'
import { combineDescribedBy } from '../../../shared/component-helper'
import { combineDescribedBy, warn } from '../../../shared/component-helper'
import useId from '../../../shared/helpers/useId'
import useUpdateEffect from '../../../shared/helpers/useUpdateEffect'
import useMountEffect from '../../../shared/helpers/useMountEffect'
Expand Down Expand Up @@ -1145,6 +1145,19 @@ export default function useFieldProps<Value, EmptyValue, Props>(
validateInitially,
])

useEffect(() => {
if (itemPath && valueProp !== undefined) {
warn(
`Using value="${valueProp}" prop inside Iterate is not supported yet`
)
}
if (itemPath && defaultValue !== undefined) {
warn(
`Using defaultValue="${defaultValue}" prop inside Iterate is not supported yet`
)
}
}, [defaultValue, itemPath, valueProp])

useEffect(() => {
if (hasPath) {
let value = valueProp
Expand Down

0 comments on commit 116820d

Please sign in to comment.