Skip to content

Commit a4f0c44

Browse files
(Picklist): refactor to use optionIdPrefix
1 parent 5718342 commit a4f0c44

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/scripts/Picklist.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ const PicklistContext = createContext<{
4040
multiSelect?: boolean;
4141
onSelect: (value: PicklistValue) => void;
4242
focusedValue?: PicklistValue;
43+
optionIdPrefix: string;
4344
}>({
4445
values: [],
4546
onSelect: () => {
4647
// noop
4748
},
49+
optionIdPrefix: '',
4850
});
4951

5052
/**
@@ -126,6 +128,8 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
126128
const id = id_ ?? fallbackId;
127129
const listboxId = `${id}-listbox`;
128130

131+
const optionIdPrefix = `${useId()}-option`;
132+
129133
const values_: PicklistValue[] | undefined =
130134
typeof value_ === 'undefined'
131135
? undefined
@@ -219,7 +223,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
219223

220224
const dropdownContainer = dropdownElRef.current;
221225
const targetElement = dropdownContainer.querySelector(
222-
`#option-${nextFocusedValue}`
226+
`#${CSS.escape(optionIdPrefix)}-${nextFocusedValue}`
223227
);
224228

225229
if (!(targetElement instanceof HTMLElement)) {
@@ -458,6 +462,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
458462
multiSelect,
459463
onSelect: onPicklistItemSelect,
460464
focusedValue,
465+
optionIdPrefix,
461466
};
462467

463468
return (
@@ -479,7 +484,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
479484
aria-haspopup='listbox'
480485
aria-disabled={disabled}
481486
aria-activedescendant={
482-
focusedValue ? `option-${focusedValue}` : undefined
487+
focusedValue ? `${optionIdPrefix}-${focusedValue}` : undefined
483488
}
484489
onClick={onClick}
485490
onKeyDown={onKeyDown}
@@ -545,7 +550,7 @@ export const PicklistItem: FC<PicklistItemProps> = ({
545550
disabled,
546551
children,
547552
}) => {
548-
const { values, multiSelect, onSelect, focusedValue } =
553+
const { values, multiSelect, onSelect, focusedValue, optionIdPrefix } =
549554
useContext(PicklistContext);
550555
const selected =
551556
selected_ ?? (value != null ? values.indexOf(value) >= 0 : false);
@@ -571,7 +576,7 @@ export const PicklistItem: FC<PicklistItemProps> = ({
571576
return (
572577
<li role='presentation' className='slds-listbox__item'>
573578
<div
574-
id={value ? `option-${value}` : undefined}
579+
id={value ? `${optionIdPrefix}-${value}` : undefined}
575580
className={itemClassNames}
576581
role='option'
577582
aria-selected={selected}

0 commit comments

Comments
 (0)