-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: Autocomplete focus scroll #3067
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,15 +9,22 @@ import Button from "@mui/material/Button"; | |
import FormControl from "@mui/material/FormControl"; | ||
import { inputLabelClasses } from "@mui/material/InputLabel"; | ||
import List from "@mui/material/List"; | ||
import ListItem from "@mui/material/ListItem"; | ||
import ListItem, { listItemClasses } from "@mui/material/ListItem"; | ||
import ListItemText from "@mui/material/ListItemText"; | ||
import ListSubheader from "@mui/material/ListSubheader"; | ||
import { outlinedInputClasses } from "@mui/material/OutlinedInput"; | ||
import { styled } from "@mui/material/styles"; | ||
import TextField from "@mui/material/TextField"; | ||
import Typography from "@mui/material/Typography"; | ||
import capitalize from "lodash/capitalize"; | ||
import React, { forwardRef, PropsWithChildren, useMemo, useState } from "react"; | ||
import React, { | ||
forwardRef, | ||
PropsWithChildren, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { borderedFocusStyle } from "theme"; | ||
import Checkbox from "ui/shared/Checkbox"; | ||
|
||
|
@@ -148,6 +155,8 @@ const renderGroup: AutocompleteProps< | |
|
||
/** | ||
* Function which returns the options (li elements) used by Autocomplete | ||
* This is not a React.FC so cannot contain hooks | ||
* Therefore this has to be handled in OptionComponent directly | ||
*/ | ||
const renderOption: AutocompleteProps< | ||
Option, | ||
|
@@ -156,18 +165,62 @@ const renderOption: AutocompleteProps< | |
false, | ||
"div" | ||
>["renderOption"] = (props, option, { selected }) => ( | ||
<ListItem {...props}> | ||
<Checkbox | ||
data-testid="select-checkbox" | ||
checked={selected} | ||
inputProps={{ | ||
"aria-label": option.name, | ||
}} | ||
/> | ||
<ListItemText sx={{ ml: 2 }}>{option.name}</ListItemText> | ||
</ListItem> | ||
<OptionComponent option={option} selected={selected} {...props} /> | ||
); | ||
|
||
interface OptionComponentProps extends React.HTMLAttributes<HTMLLIElement> { | ||
option: Option; | ||
selected: boolean; | ||
} | ||
|
||
const OptionComponent: React.FC<OptionComponentProps> = ({ | ||
option, | ||
selected, | ||
...props | ||
}) => { | ||
const ref = useRef<HTMLLIElement>(null); | ||
|
||
// Using a custom listbox has a breaking effect on the native scroll used by MUI's Autocomplete | ||
// Using a MutationObserver we can listen for MUI applying the focusVisible class | ||
// We then use this to indicate that we need to scroll the focused element into view | ||
useEffect(() => { | ||
const targetElement = ref.current; | ||
if (!targetElement) return; | ||
|
||
const observer = new MutationObserver((mutationsList) => { | ||
mutationsList.forEach(({ type, attributeName }) => { | ||
const isClassMutation = | ||
type === "attributes" && attributeName === "class"; | ||
if (!isClassMutation) return; | ||
|
||
const isFocused = targetElement.classList.contains( | ||
listItemClasses.focusVisible, | ||
); | ||
if (!isFocused) return; | ||
|
||
targetElement.scrollIntoView(false); | ||
}); | ||
}); | ||
|
||
observer.observe(targetElement, { attributes: true }); | ||
|
||
return () => observer.disconnect(); | ||
}, []); | ||
|
||
return ( | ||
<ListItem {...props} ref={ref}> | ||
<Checkbox | ||
data-testid="select-checkbox" | ||
checked={selected} | ||
inputProps={{ | ||
"aria-label": option.name, | ||
}} | ||
/> | ||
<ListItemText sx={{ ml: 2 }}>{option.name}</ListItemText> | ||
</ListItem> | ||
); | ||
}; | ||
|
||
const PopupIcon = <ArrowIcon sx={{ color: "primary.main" }} fontSize="large" />; | ||
|
||
/** | ||
|
@@ -276,6 +329,7 @@ export const SelectMultiple = (props: SelectMultipleProps) => { | |
aria-live="polite" | ||
disableClearable | ||
disableCloseOnSelect | ||
disableListWrap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was also flagged by @ianjon3s as a usability issue - you can now no longer wrap around from final → first option when navigating by keyboard. |
||
getOptionLabel={(option) => option.name} | ||
groupBy={(option) => option.category} | ||
id={`select-multiple-file-tags-${uploadedFile.id}`} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a pretty complex solution to a small UI problem. Tried a few alternatives first, but if there's anything obvious I'm missing here please let me know!