1- import ToggleButton from "@mui/material/ToggleButton" ;
2- import ToggleButtonGroup from "@mui/material/ToggleButtonGroup" ;
3- import SortIcon from "@mui/icons-material/Sort" ;
41import Algorithm , {
52 ALGORITHM_KEYS ,
63 IAlgorithmKey ,
74 SortingState ,
85} from "../algorithms/Algorithm" ;
9- import Stack from "@mui/material/Stack" ;
10- import { Typography } from "@mui/material" ;
6+ import {
7+ FormControl ,
8+ InputLabel ,
9+ MenuItem ,
10+ OutlinedInput ,
11+ Select ,
12+ SelectChangeEvent ,
13+ } from "@mui/material" ;
1114import { AlgorithmContext } from "./Dashboard" ;
12- import { useContext } from "react" ;
15+ import { useContext , useState } from "react" ;
16+ import { Theme , useTheme } from "@mui/material/styles" ;
1317
1418export type AlgorithmSelectionProps = { } ;
1519
20+ const ITEM_HEIGHT = 48 ;
21+ const ITEM_PADDING_TOP = 8 ;
22+ const MenuProps = {
23+ PaperProps : {
24+ style : {
25+ maxHeight : ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP ,
26+ width : 450 ,
27+ } ,
28+ } ,
29+ } ;
30+
31+ function getStyles ( algorithmKey : string , textValue : string [ ] , theme : Theme ) {
32+ return {
33+ fontWeight :
34+ textValue . indexOf ( algorithmKey ) === - 1
35+ ? theme . typography . fontWeightRegular
36+ : theme . typography . fontWeightMedium ,
37+ } ;
38+ }
39+
1640export default function AlgorithmSelection ( props : AlgorithmSelectionProps ) {
1741 const state = useContext ( AlgorithmContext ) ;
42+ const theme = useTheme ( ) ;
43+ const [ textValue , setTextValue ] = useState < string [ ] > ( state . algorithms ) ;
44+
45+ const handleChange = ( event : SelectChangeEvent < typeof ALGORITHM_KEYS > ) => {
46+ const {
47+ target : { value } ,
48+ } = event ;
49+
50+ const algorithms = (
51+ Array . isArray ( value ) ? value : ! value ? [ ] : [ value ]
52+ ) as IAlgorithmKey [ ] ;
1853
19- const handleAlgorithmSelection = (
20- event : React . MouseEvent < HTMLElement > ,
21- algorithms : IAlgorithmKey [ ]
22- ) => {
2354 if ( algorithms . length > 0 ) {
55+ setTextValue ( typeof value === "string" ? value . split ( "," ) : value ) ;
2456 const sortingStateEval : SortingState = algorithms . reduce (
2557 ( prev , algorithmKey ) => ( { ...prev , [ algorithmKey ] : false } ) ,
2658 { }
@@ -32,19 +64,28 @@ export default function AlgorithmSelection(props: AlgorithmSelectionProps) {
3264 } ;
3365
3466 return (
35- < Stack spacing = { 1 } direction = "row" alignItems = "center" >
36- < SortIcon fontSize = "large" />
37- < Typography gutterBottom > Algorithms</ Typography >
38- < ToggleButtonGroup
39- value = { state . algorithms }
40- onChange = { handleAlgorithmSelection }
41- >
42- { ALGORITHM_KEYS . map ( ( key ) => (
43- < ToggleButton key = { key } value = { key } >
44- { Algorithm [ key ] . name }
45- </ ToggleButton >
46- ) ) }
47- </ ToggleButtonGroup >
48- </ Stack >
67+ < div >
68+ < FormControl sx = { { width : "80vw" } } >
69+ < InputLabel id = "algorithm-selection" > Algorithms</ InputLabel >
70+ < Select
71+ labelId = "algorithm-selection"
72+ multiple
73+ value = { textValue as IAlgorithmKey [ ] }
74+ onChange = { handleChange }
75+ input = { < OutlinedInput label = "Algorithms" /> }
76+ MenuProps = { MenuProps }
77+ >
78+ { ALGORITHM_KEYS . map ( ( algorithmKey ) => (
79+ < MenuItem
80+ key = { algorithmKey }
81+ value = { algorithmKey }
82+ style = { getStyles ( algorithmKey , textValue , theme ) }
83+ >
84+ { Algorithm [ algorithmKey ] . name }
85+ </ MenuItem >
86+ ) ) }
87+ </ Select >
88+ </ FormControl >
89+ </ div >
4990 ) ;
5091}
0 commit comments