From aeeba936181592eb200827c2c8a3672dc17f312f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Tue, 5 Nov 2024 09:15:12 +0100 Subject: [PATCH] fix(Forms): add Iterate support for Field.PostalCodeAndCity when using `country` with a path --- .../__tests__/PostalCodeAndCity.test.tsx | 28 +++++++++++++++++++ .../extensions/forms/hooks/useDataValue.ts | 10 +++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/dnb-eufemia/src/extensions/forms/Field/PostalCodeAndCity/__tests__/PostalCodeAndCity.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Field/PostalCodeAndCity/__tests__/PostalCodeAndCity.test.tsx index b8df3d1f6cd..3c67196fe72 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Field/PostalCodeAndCity/__tests__/PostalCodeAndCity.test.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Field/PostalCodeAndCity/__tests__/PostalCodeAndCity.test.tsx @@ -240,6 +240,34 @@ describe('Field.PostalCodeAndCity', () => { expect(postalCodeNo).toHaveAttribute('aria-placeholder', '0000') }) + it('should use value from itemPath inside iterate', async () => { + render( + + + + + + ) + + const [norway, germany] = Array.from( + document.querySelectorAll('.dnb-forms-field-postal-code-and-city') + ) + + await userEvent.type( + norway.querySelector('input'), + '{Backspace>4}987654' + ) + expect(norway.querySelector('input').value).toBe('9876') + + await userEvent.type( + germany.querySelector('input'), + '{Backspace>4}987654' + ) + expect(germany.querySelector('input').value).toBe('987654') + }) + describe('ARIA', () => { const props = { postalCode: { required: true, validateInitially: true }, diff --git a/packages/dnb-eufemia/src/extensions/forms/hooks/useDataValue.ts b/packages/dnb-eufemia/src/extensions/forms/hooks/useDataValue.ts index 2371afe13b2..bf4adac49f8 100644 --- a/packages/dnb-eufemia/src/extensions/forms/hooks/useDataValue.ts +++ b/packages/dnb-eufemia/src/extensions/forms/hooks/useDataValue.ts @@ -2,6 +2,7 @@ import { useCallback, useContext, useRef } from 'react' import pointer from '../utils/json-pointer' import { Path } from '../types' import DataContext, { ContextState } from '../DataContext/Context' +import { IterateItemContext } from '../Iterate' import usePath from './usePath' export type Props = { @@ -16,7 +17,8 @@ export default function useDataValue({ value, }: Props = {}) { const dataContextRef = useRef() - dataContextRef.current = useContext(DataContext) + dataContextRef.current = useContext(DataContext) + const iterateItemContext = useContext(IterateItemContext) const { makePath, makeIteratePath } = usePath() @@ -76,12 +78,16 @@ export default function useDataValue({ const getSourceValue = useCallback( (source: Path | Value) => { if (typeof source === 'string' && isPath(source)) { + if (iterateItemContext) { + return getValueByIteratePath(source) + } + return getValueByPath(source) } return source }, - [getValueByPath] + [getValueByIteratePath, getValueByPath, iterateItemContext] ) if (pathProp) {