2023-06-21 09:28:00 +00:00
|
|
|
// @ts-strict-ignore
|
2023-01-16 09:45:12 +00:00
|
|
|
import { FormChange } from "@dashboard/hooks/useForm";
|
|
|
|
import ArrowDropdown from "@dashboard/icons/ArrowDropdown";
|
2021-05-14 08:15:15 +00:00
|
|
|
import { ClickAwayListener, MenuItem, Paper, Popper } from "@material-ui/core";
|
2022-12-02 10:45:19 +00:00
|
|
|
import clsx from "clsx";
|
2020-02-07 16:12:01 +00:00
|
|
|
import { codes } from "keycode";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
2020-02-07 16:12:01 +00:00
|
|
|
|
|
|
|
import Link from "../Link";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { SingleAutocompleteChoiceType } from "../SingleAutocompleteSelectField";
|
2022-07-19 14:10:10 +00:00
|
|
|
import { useStyles } from "./styles";
|
2020-02-07 16:12:01 +00:00
|
|
|
|
|
|
|
export interface LinkChoiceProps {
|
2020-02-07 16:44:42 +00:00
|
|
|
className?: string;
|
2020-02-07 16:12:01 +00:00
|
|
|
choices: SingleAutocompleteChoiceType[];
|
2020-02-07 16:44:42 +00:00
|
|
|
name?: string;
|
2020-02-07 16:12:01 +00:00
|
|
|
value: string;
|
|
|
|
onChange: FormChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LinkChoice: React.FC<LinkChoiceProps> = ({
|
2020-02-07 16:44:42 +00:00
|
|
|
className,
|
2020-02-07 16:12:01 +00:00
|
|
|
choices,
|
|
|
|
name,
|
|
|
|
value,
|
2022-06-21 09:36:55 +00:00
|
|
|
onChange,
|
2020-02-07 16:12:01 +00:00
|
|
|
}) => {
|
2022-07-19 14:10:10 +00:00
|
|
|
const classes = useStyles();
|
2020-02-07 16:12:01 +00:00
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
const anchor = React.useRef<HTMLInputElement>(null);
|
|
|
|
const current = choices.find(c => c.value === value);
|
|
|
|
const [highlightedIndex, setHighlightedIndex] = React.useState(0);
|
|
|
|
|
|
|
|
const handleChange = (value: string) => {
|
|
|
|
setOpen(false);
|
|
|
|
onChange({
|
|
|
|
target: {
|
|
|
|
name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value,
|
|
|
|
},
|
2020-02-07 16:12:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyPress = (event: React.KeyboardEvent<HTMLSpanElement>) => {
|
|
|
|
switch (event.keyCode) {
|
|
|
|
case codes.down:
|
|
|
|
setHighlightedIndex(
|
2022-06-21 09:36:55 +00:00
|
|
|
highlightedIndex => (highlightedIndex + 1) % choices.length,
|
2020-02-07 16:12:01 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case codes.up:
|
|
|
|
setHighlightedIndex(highlightedIndex =>
|
|
|
|
highlightedIndex === 0
|
|
|
|
? choices.length - 1
|
2022-06-21 09:36:55 +00:00
|
|
|
: (highlightedIndex - 1) % choices.length,
|
2020-02-07 16:12:01 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case codes.enter:
|
|
|
|
if (open) {
|
|
|
|
handleChange(choices[highlightedIndex].value);
|
|
|
|
} else {
|
|
|
|
setOpen(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
2022-12-02 10:45:19 +00:00
|
|
|
className={clsx(classes.root, className)}
|
2020-02-07 16:12:01 +00:00
|
|
|
ref={anchor}
|
|
|
|
onKeyDown={handleKeyPress}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
2020-04-29 14:14:20 +00:00
|
|
|
<Link onClick={() => setOpen(open => !open)}>
|
|
|
|
{current.label}
|
|
|
|
<ArrowDropdown
|
2022-12-02 10:45:19 +00:00
|
|
|
className={clsx(classes.arrow, {
|
2022-06-21 09:36:55 +00:00
|
|
|
[classes.rotate]: open,
|
2020-04-29 14:14:20 +00:00
|
|
|
})}
|
|
|
|
color="primary"
|
|
|
|
/>
|
|
|
|
</Link>
|
2020-02-07 16:12:01 +00:00
|
|
|
|
|
|
|
<Popper
|
|
|
|
className={classes.popper}
|
|
|
|
open={open}
|
|
|
|
anchorEl={anchor.current}
|
|
|
|
transition
|
|
|
|
disablePortal
|
|
|
|
placement="bottom-start"
|
|
|
|
>
|
|
|
|
<ClickAwayListener
|
|
|
|
onClickAway={() => setOpen(false)}
|
|
|
|
mouseEvent="onClick"
|
|
|
|
>
|
|
|
|
<Paper className={classes.paper}>
|
|
|
|
{choices.map((choice, choiceIndex) => (
|
|
|
|
<MenuItem
|
2022-12-02 10:45:19 +00:00
|
|
|
className={clsx(classes.menuItem, {
|
2022-06-21 09:36:55 +00:00
|
|
|
[classes.highlighted]: highlightedIndex === choiceIndex,
|
2020-02-07 16:12:01 +00:00
|
|
|
})}
|
|
|
|
selected={choice.value === value}
|
|
|
|
key={choice.value}
|
|
|
|
onClick={() => handleChange(choice.value)}
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="select-option"
|
2020-02-07 16:12:01 +00:00
|
|
|
>
|
|
|
|
{choice.label}
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
</Paper>
|
|
|
|
</ClickAwayListener>
|
|
|
|
</Popper>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
LinkChoice.displayName = "LinkChoice";
|
|
|
|
export default LinkChoice;
|