Skip to content

Commit e8787ce

Browse files
(Lookup): apply isFocusedInComponent() to the scope selector
This commit refers to `v5.9.1`.
1 parent d3e23a7 commit e8787ce

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/scripts/Lookup.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const LookupSelectedState: FC<LookupSelectedStateProps> = ({
187187
*/
188188
type LookupScopeSelectorContainerProps = {
189189
scopeListboxId: string;
190+
dropdownRef: React.MutableRefObject<HTMLDivElement | null>;
190191
children: React.ReactNode;
191192
} & AutoAlignInjectedProps;
192193

@@ -195,6 +196,7 @@ type LookupScopeSelectorContainerProps = {
195196
*/
196197
const LookupScopeSelectorContainer: FC<LookupScopeSelectorContainerProps> = ({
197198
scopeListboxId,
199+
dropdownRef,
198200
children,
199201
alignment,
200202
autoAlignContentRef,
@@ -214,7 +216,7 @@ const LookupScopeSelectorContainer: FC<LookupScopeSelectorContainerProps> = ({
214216
className={dropdownClassNames}
215217
role='listbox'
216218
aria-label='Scope Options'
217-
ref={useMergeRefs([autoAlignContentRef])}
219+
ref={useMergeRefs([dropdownRef, autoAlignContentRef])}
218220
>
219221
{children}
220222
</div>
@@ -248,6 +250,8 @@ const LookupScopeSelector: FC<LookupScopeSelectorProps> = ({
248250
onScopeMenuClick: onScopeMenuClick_,
249251
onScopeSelect: onScopeSelect_,
250252
}) => {
253+
const dropdownRef = useRef<HTMLDivElement | null>(null);
254+
251255
const [scopeOpened, setScopeOpened] = useState(false);
252256
const [scopeFocusedIndex, setScopeFocusedIndex] = useState<number>(-1);
253257

@@ -379,10 +383,18 @@ const LookupScopeSelector: FC<LookupScopeSelectorProps> = ({
379383
}
380384

381385
setTimeout(() => {
382-
setScopeOpened(false);
386+
if (!isFocusedInComponent()) {
387+
setScopeOpened(false);
388+
}
383389
}, 10);
384390
});
385391

392+
const { getActiveElement } = useContext(ComponentSettingsContext);
393+
const isFocusedInComponent = useEventCallback(() => {
394+
const targetEl = getActiveElement();
395+
return isElInChildren(dropdownRef.current, targetEl);
396+
});
397+
386398
return (
387399
<div className='react-slds-lookup-scope-selector slds-combobox_object-switcher slds-combobox-addon_start'>
388400
<div className='slds-form-element'>
@@ -442,6 +454,7 @@ const LookupScopeSelector: FC<LookupScopeSelectorProps> = ({
442454
{(injectedProps) => (
443455
<LookupScopeSelectorContainer
444456
scopeListboxId={scopeListboxId}
457+
dropdownRef={dropdownRef}
445458
{...injectedProps}
446459
>
447460
<ul

0 commit comments

Comments
 (0)