@@ -10,7 +10,7 @@ import {
1010import { Autocomplete , Box , Button , Chip , InputAdornment , TextField , useMediaQuery , useTheme } from '@mui/material' ;
1111import { styled } from '@mui/system' ;
1212import Fuse from 'fuse.js' ;
13- import React , { useContext , useEffect , useRef , useState } from 'react' ;
13+ import React , { useContext , useEffect , useMemo , useRef , useState } from 'react' ;
1414import { ListChildComponentProps , VariableSizeList } from 'react-window' ;
1515
1616import { ThemeType } from '../../constants/theme' ;
@@ -327,6 +327,14 @@ const CourseSelect: React.FC<CourseSelectProps> = ({ assignedColors, handleSelec
327327 setSelectedFaculty ( '' ) ;
328328 } ;
329329
330+ const keyOf = ( x : { code : string ; career : string } ) => `${ x . code } |${ x . career } ` ;
331+ const mergedOptions = useMemo ( ( ) => {
332+ const map = new Map < string , CourseOverview > ( ) ;
333+ options . forEach ( ( x ) => map . set ( keyOf ( x ) , x ) ) ;
334+ selectedValue . forEach ( ( x ) => map . set ( keyOf ( x ) , x ) ) ;
335+ return Array . from ( map . values ( ) ) ;
336+ } , [ options , selectedValue ] ) ;
337+
330338 const shrinkLabel = inputValue . length > 0 || selectedValue . length > 0 ;
331339
332340 const OuterElementContext = React . createContext ( { } ) ;
@@ -413,7 +421,7 @@ const CourseSelect: React.FC<CourseSelectProps> = ({ assignedColors, handleSelec
413421 disableListWrap
414422 noOptionsText = "No Results"
415423 selectOnFocus = { false }
416- options = { options }
424+ options = { mergedOptions }
417425 value = { selectedValue }
418426 onChange = { onChange }
419427 inputValue = { inputValue }
@@ -424,34 +432,38 @@ const CourseSelect: React.FC<CourseSelectProps> = ({ assignedColors, handleSelec
424432 filterOptions = { ( o ) => o }
425433 ListboxComponent = { ListboxComponent }
426434 isOptionEqualToValue = { ( option , value ) => option . code === value . code && option . career === value . career }
427- renderOption = { ( props , option , { selected } ) => (
428- < li { ...props } >
429- < StyledOption >
430- < StyledIcon >
431- { selectedValue . find ( ( course : CourseOverview ) => course . code === option . code ) ? (
432- < CheckRounded />
433- ) : (
434- < AddRounded />
435- ) }
436- </ StyledIcon >
437- < span > { option . code } </ span >
438- < Weak > { ! ( isMedium || isTiny ) && option . name } </ Weak >
439- < Career > { getCourseCareer ( option . career ) } </ Career >
440- < RightContainer >
441- { option . online && (
442- < StyledIconRight >
443- < VideocamOutlined />
444- </ StyledIconRight >
445- ) }
446- { option . inPerson && (
447- < StyledIconRight >
448- < PersonOutline />
449- </ StyledIconRight >
450- ) }
451- </ RightContainer >
452- </ StyledOption >
453- </ li >
454- ) }
435+ renderOption = { ( props , option , { selected } ) => {
436+ const { key, ...rest } = props ;
437+
438+ return (
439+ < li key = { key } { ...rest } >
440+ < StyledOption >
441+ < StyledIcon >
442+ { selectedValue . find ( ( course : CourseOverview ) => course . code === option . code ) ? (
443+ < CheckRounded />
444+ ) : (
445+ < AddRounded />
446+ ) }
447+ </ StyledIcon >
448+ < span > { option . code } </ span >
449+ < Weak > { ! ( isMedium || isTiny ) && option . name } </ Weak >
450+ < Career > { getCourseCareer ( option . career ) } </ Career >
451+ < RightContainer >
452+ { option . online && (
453+ < StyledIconRight >
454+ < VideocamOutlined />
455+ </ StyledIconRight >
456+ ) }
457+ { option . inPerson && (
458+ < StyledIconRight >
459+ < PersonOutline />
460+ </ StyledIconRight >
461+ ) }
462+ </ RightContainer >
463+ </ StyledOption >
464+ </ li >
465+ ) ;
466+ } }
455467 renderInput = { ( params ) => (
456468 < StyledTextField
457469 { ...params }
@@ -491,19 +503,24 @@ const CourseSelect: React.FC<CourseSelectProps> = ({ assignedColors, handleSelec
491503 />
492504 ) }
493505 renderTags = { ( value : CoursesList , getTagProps ) =>
494- value . map ( ( option : CourseOverview , index : number ) => (
495- < StyledChip
496- label = { option . code }
497- color = "primary"
498- backgroundColor = { assignedColors [ option . code ] }
499- deleteIcon = { < CloseRounded /> }
500- { ...getTagProps ( { index } ) }
501- onDelete = { ( ) => {
502- setSelectedValue ( selectedValue . filter ( ( course ) => course . code !== option . code ) ) ;
503- handleRemove ( option . code ) ;
504- } }
505- />
506- ) )
506+ value . map ( ( option : CourseOverview , index : number ) => {
507+ const { key, ...rest } = getTagProps ( { index } ) ;
508+
509+ return (
510+ < StyledChip
511+ key = { key }
512+ { ...rest }
513+ label = { option . code }
514+ color = "primary"
515+ backgroundColor = { assignedColors [ option . code ] }
516+ deleteIcon = { < CloseRounded /> }
517+ onDelete = { ( ) => {
518+ setSelectedValue ( selectedValue . filter ( ( course ) => course . code !== option . code ) ) ;
519+ handleRemove ( option . code ) ;
520+ } }
521+ />
522+ ) ;
523+ } )
507524 }
508525 />
509526 </ StyledSelect >
0 commit comments