From bc5878487e676491df52c47596574f519d7920b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Sun, 21 Jan 2024 16:58:58 +0100 Subject: [PATCH] chore(Expiry): make Expiry FieldBlock compatible So we can forward labelDescription. --- .../input-masked/MultiInputMask.tsx | 78 +++++++++++-------- .../__tests__/MultiInputMask.test.tsx | 16 ++-- .../extensions/forms/Field/Expiry/Expiry.tsx | 16 ++-- 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/packages/dnb-eufemia/src/components/input-masked/MultiInputMask.tsx b/packages/dnb-eufemia/src/components/input-masked/MultiInputMask.tsx index 7bcf4c725d4..6e7c3bd4277 100644 --- a/packages/dnb-eufemia/src/components/input-masked/MultiInputMask.tsx +++ b/packages/dnb-eufemia/src/components/input-masked/MultiInputMask.tsx @@ -90,6 +90,7 @@ export type MultiInputMaskProps = { SpacingProps function MultiInputMask({ + id = makeUniqueId(), label, labelDirection = 'horizontal', inputs, @@ -155,29 +156,35 @@ function MultiInputMask({ status_state={statusState} suffix={suffix} stretch={stretch} - input_element={inputs.map((input, index) => ( - { - if (onFocus) { - onFocus(values) + input_element={inputs.map(({ id: inputId, ...rest }, index) => { + return ( + { - if (onBlur) { - onBlur(values) - } - }} - disabled={disabled} - inputRef={getInputRef} - /> - ))} + disabled={disabled} + inputMode={inputMode} + onKeyDown={onKeyDown} + onChange={onChange} + onFocus={() => { + if (onFocus) { + onFocus(values) + } + }} + onBlur={() => { + if (onBlur) { + onBlur(values) + } + }} + getInputRef={getInputRef} + {...rest} + value={values[inputId]} + /> + ) + })} /> ) @@ -195,12 +202,16 @@ function MultiInputMask({ } // Utilities - function getInputRef(ref: any) { + function getInputRef(ref?: { + inputRef?: MutableRefObject + }) { const inputRef = ref?.inputRef if (inputRef && !inputRefs.current.includes(inputRef)) { inputRefs.current.push(inputRef) } + + return inputRef } function getKeysToHandle() { @@ -238,6 +249,7 @@ function MultiInputMask({ type MultiInputMaskInputProps = MultiInputMaskInput & { id: MultiInputMaskInput['id'] + inputId: MultiInputMaskInput['id'] label: MultiInputMaskInput['label'] value: string mask: MultiInputMaskInput['mask'] @@ -249,18 +261,19 @@ type MultiInputMaskInputProps = id: string, placeholderCharacter: MultiInputMaskInput['placeholderCharacter'] ) => void - inputRef: any + getInputRef: () => MutableRefObject } function MultiInputMaskInput({ id, + inputId, label, value, mask, placeholderCharacter, delimiter, disabled, - inputRef, + getInputRef, onKeyDown, onChange, onBlur, @@ -268,13 +281,12 @@ function MultiInputMaskInput({ ...attributes }: MultiInputMaskInputProps) { const shouldHighlight = !disabled && /\w+/.test(value) - const markupId = `${id}-${makeUniqueId()}` return ( <> ({ guide={true} showMask={true} keepCharPositions={false} // so we can overwrite next value, if it already exists - aria-labelledby={`${markupId}__label`} - ref={inputRef} + aria-labelledby={`${id}-${inputId}__label`} + ref={getInputRef} onKeyDown={onKeyDown} onBlur={onBlur} onFocus={({ target, ...event }) => { @@ -302,15 +314,15 @@ function MultiInputMaskInput({ }} onChange={(event) => { onChange( - id, + inputId, removePlaceholder(event.target.value, placeholderCharacter) ) }} {...attributes} />