Skip to content

Commit 9bd05b1

Browse files
committed
fix(ComboBox): input id linking
1 parent d30c2d4 commit 9bd05b1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/components/fields/ComboBox/ComboBox.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
RefObject,
99
useCallback,
1010
useEffect,
11+
useId,
1112
useMemo,
1213
useRef,
1314
useState,
@@ -585,6 +586,7 @@ function useComboBoxKeyboard({
585586
// ============================================================================
586587
interface ComboBoxInputProps {
587588
inputRef: RefObject<HTMLInputElement>;
589+
id?: string;
588590
value: string;
589591
placeholder?: string;
590592
isDisabled?: boolean;
@@ -609,6 +611,7 @@ const ComboBoxInput = forwardRef<HTMLInputElement, ComboBoxInputProps>(
609611
function ComboBoxInput(
610612
{
611613
inputRef,
614+
id,
612615
value,
613616
placeholder,
614617
isDisabled,
@@ -636,6 +639,7 @@ const ComboBoxInput = forwardRef<HTMLInputElement, ComboBoxInputProps>(
636639
<InputElement
637640
ref={combinedRef}
638641
qa="Input"
642+
id={id}
639643
type="text"
640644
value={value}
641645
placeholder={placeholder}
@@ -854,6 +858,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
854858
isRequired,
855859
necessityIndicator,
856860
validationState,
861+
id,
857862
icon,
858863
prefix,
859864
isDisabled,
@@ -902,6 +907,10 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
902907
...otherProps
903908
} = props;
904909

910+
// Generate ID for label-input linking if not provided
911+
const generatedId = useId();
912+
const inputId = id || generatedId;
913+
905914
// Generate a unique ID for this combobox instance
906915
const comboBoxId = useMemo(() => generateRandomId(), []);
907916

@@ -1313,6 +1322,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
13131322
{prefix ? <div data-element="Prefix">{prefix}</div> : null}
13141323
<ComboBoxInput
13151324
inputRef={inputRef}
1325+
id={inputId}
13161326
value={effectiveInputValue}
13171327
placeholder={placeholder}
13181328
isDisabled={isDisabled}
@@ -1411,10 +1421,20 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
14111421
</ComboBoxWrapperElement>
14121422
);
14131423

1424+
const finalProps = { ...props, styles };
1425+
1426+
// Ensure labelProps has the for attribute for label-input linking
1427+
if (!finalProps.labelProps) {
1428+
finalProps.labelProps = {};
1429+
}
1430+
if (!finalProps.labelProps.for) {
1431+
finalProps.labelProps.for = inputId;
1432+
}
1433+
14141434
return wrapWithField<Omit<CubeComboBoxProps<T>, 'children'>>(
14151435
comboBoxField,
14161436
ref,
1417-
mergeProps({ ...props, styles }, {}),
1437+
mergeProps(finalProps, {}),
14181438
);
14191439
}) as unknown as (<T>(
14201440
props: CubeComboBoxProps<T> & { ref?: ForwardedRef<HTMLDivElement> },

0 commit comments

Comments
 (0)