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

fix: Autocomplete reset/re-render on keyboard events #3068

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Typography from "@mui/material/Typography";
import capitalize from "lodash/capitalize";
import React, { forwardRef, PropsWithChildren, useMemo, useState } from "react";
import { borderedFocusStyle } from "theme";
import Checkbox from "ui/shared/Checkbox";

import { FileUploadSlot } from "../FileUpload/Public";
import {
Expand All @@ -28,7 +29,6 @@ import {
removeSlots,
UserFile,
} from "./model";
import Checkbox from "ui/shared/Checkbox";

interface SelectMultipleProps {
uploadedFile: FileUploadSlot;
Expand Down Expand Up @@ -123,7 +123,12 @@ const renderGroup: AutocompleteProps<
false,
"div"
>["renderGroup"] = ({ group, key, children }) => (
<List key={`group-${key}`} role="group" sx={{ paddingY: 0 }} aria-labelledby={`${group}-label`}>
<List
key={`group-${key}`}
role="group"
sx={{ paddingY: 0 }}
aria-labelledby={`${group}-label`}
>
<ListSubheader
id={`${group}-label`}
role="presentation"
Expand Down Expand Up @@ -156,7 +161,7 @@ const renderOption: AutocompleteProps<
data-testid="select-checkbox"
checked={selected}
inputProps={{
"aria-label": option.name
"aria-label": option.name,
}}
/>
<ListItemText sx={{ ml: 2 }}>{option.name}</ListItemText>
Expand All @@ -165,11 +170,37 @@ const renderOption: AutocompleteProps<

const PopupIcon = <ArrowIcon sx={{ color: "primary.main" }} fontSize="large" />;

/**
* Custom Listbox component
* Used to wrap options within the autocomplete and append a custom element above the option list
*/
const ListboxComponent = forwardRef<typeof Box, PropsWithChildren>(
({ children, ...props }, ref) => (
<>
<Box>
<ListHeader>
<Typography variant="h4" component="h3" pr={3}>
Select all that apply
</Typography>
<Button variant="contained" color="prompt" aria-label="Close list">
Done
</Button>
</ListHeader>
</Box>
<Box
ref={ref}
{...props}
role="listbox"
sx={{ paddingY: "0px !important" }}
>
{children}
</Box>
</>
),
);

export const SelectMultiple = (props: SelectMultipleProps) => {
const { uploadedFile, fileList, setFileList } = props;
const [open, setOpen] = useState(false);
const closePopper = () => setOpen(false);
const openPopper = () => setOpen(true);

const initialTags = getTagsForSlot(uploadedFile.id, fileList);

Expand Down Expand Up @@ -234,42 +265,6 @@ export const SelectMultiple = (props: SelectMultipleProps) => {
}
};

/**
* Custom Listbox component
* Used to wrap options within the autocomplete and append a custom element above the option list
*/
const ListboxComponent = forwardRef<typeof Box, PropsWithChildren>(
({ children, ...props }, ref) => {
return (
<>
<Box>
<ListHeader>
<Typography variant="h4" component="h3" pr={3}>
Select all that apply
</Typography>
<Button
variant="contained"
color="prompt"
onClick={closePopper}
aria-label="Close list"
>
Done
</Button>
</ListHeader>
</Box>
<Box
ref={ref}
{...props}
role="listbox"
sx={{ paddingY: "0px !important" }}
>
{children}
</Box>
</>
);
},
);

return (
<FormControl
key={`form-${uploadedFile.id}`}
Expand All @@ -288,9 +283,6 @@ export const SelectMultiple = (props: SelectMultipleProps) => {
ListboxComponent={ListboxComponent}
multiple
onChange={handleChange}
onClose={closePopper}
onOpen={openPopper}
open={open}
options={options}
popupIcon={PopupIcon}
renderGroup={renderGroup}
Expand Down
Loading