Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Select: fix document scrolling on focus, key down.. #2628

Merged
merged 1 commit into from Oct 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/mantine-core/src/Select/Select.tsx
Expand Up @@ -362,6 +362,9 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>

const selectedItemIndex = _value ? filteredData.findIndex((el) => el.value === _value) : 0;

const shouldShowDropdown =
!readOnly && (filteredData.length > 0 ? dropdownOpened : dropdownOpened && !!nothingFound);

const handlePrevious = () => {
setHovered((current) => {
const nextIndex = getNextIndex(
Expand All @@ -371,7 +374,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
);

targetRef.current = itemsRefs.current[filteredData[nextIndex]?.value];
scrollIntoView({ alignment: isColumn ? 'start' : 'end' });
shouldShowDropdown && scrollIntoView({ alignment: isColumn ? 'start' : 'end' });
return nextIndex;
});
};
Expand All @@ -385,7 +388,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
);

targetRef.current = itemsRefs.current[filteredData[nextIndex]?.value];
scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
shouldShowDropdown && scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
return nextIndex;
});
};
Expand All @@ -396,9 +399,12 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
}, 0);

useDidUpdate(() => {
if (shouldShowDropdown) scrollSelectedItemIntoView();
}, [shouldShowDropdown]);

const handleInputKeydown = (event: React.KeyboardEvent<HTMLInputElement>) => {
typeof onKeyDown === 'function' && onKeyDown(event);

switch (event.key) {
case 'ArrowUp': {
event.preventDefault();
Expand Down Expand Up @@ -438,7 +444,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>

const firstItemIndex = filteredData.findIndex((item) => !item.disabled);
setHovered(firstItemIndex);
scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
shouldShowDropdown && scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
}
break;
}
Expand All @@ -453,7 +459,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>

const lastItemIndex = filteredData.map((item) => !!item.disabled).lastIndexOf(false);
setHovered(lastItemIndex);
scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
shouldShowDropdown && scrollIntoView({ alignment: isColumn ? 'end' : 'start' });
}
break;
}
Expand Down Expand Up @@ -507,7 +513,6 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
typeof onFocus === 'function' && onFocus(event);
if (searchable) {
setDropdownOpened(true);
scrollSelectedItemIntoView();
}
};

Expand All @@ -530,14 +535,10 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>

if (_value && !dropdownOpened) {
setHovered(selectedItemIndex);
scrollSelectedItemIntoView();
}
}
};

const shouldShowDropdown =
!readOnly && (filteredData.length > 0 ? dropdownOpened : dropdownOpened && !!nothingFound);

return (
<Input.Wrapper {...wrapperProps} __staticSelector="Select">
<SelectPopover
Expand Down