1- import { Box , FormControl , FormHelperText , FormLabel , MenuItem , Select , SelectChangeEvent } from '@mui/material' ;
1+ import { Box , FormControl , FormLabel } from '@mui/material' ;
22import { isWithinInterval , subDays } from 'date-fns' ;
33import { Control , Controller , FieldErrors } from 'react-hook-form' ;
44import { AuthenticatedUser , ChangeRequest , wbsPipe } from 'shared' ;
55import { useAllChangeRequests } from '../hooks/change-requests.hooks' ;
66import { useCurrentUser } from '../hooks/users.hooks' ;
77import LoadingIndicator from './LoadingIndicator' ;
8+ import NERAutocomplete from './NERAutocomplete' ;
89
910// Filter and sort change requests to display in the dropdown
1011const getFilteredChangeRequests = ( changeRequests : ChangeRequest [ ] , user : AuthenticatedUser ) : ChangeRequest [ ] => {
@@ -44,14 +45,12 @@ const ChangeRequestDropdown = ({ control, name, errors }: ChangeRequestDropdownP
4445
4546 const filteredRequests = getFilteredChangeRequests ( changeRequests , user ) ;
4647
47- const approvedChangeRequestOptions = filteredRequests . map ( ( cr ) => ( {
48+ const crIdToAutocompleteOption = ( cr : ChangeRequest ) : { label : string ; id : string } => ( {
4849 label : `${ cr . crId } - ${ wbsPipe ( cr . wbsNum ) } - ${ cr . submitter . firstName } ${ cr . submitter . lastName } - ${ cr . type } ` ,
49- value : cr . crId
50- } ) ) ;
51-
52- const renderValues = new Map < number , string > ( ) ;
50+ id : cr . crId . toString ( )
51+ } ) ;
5352
54- changeRequests . forEach ( ( cr ) => renderValues . set ( cr . crId , ` ${ cr . crId } - ${ wbsPipe ( cr . wbsNum ) } ` ) ) ;
53+ const approvedChangeRequestOptions = filteredRequests . map ( crIdToAutocompleteOption ) ;
5554
5655 return (
5756 < Box >
@@ -61,36 +60,17 @@ const ChangeRequestDropdown = ({ control, name, errors }: ChangeRequestDropdownP
6160 control = { control }
6261 name = { name }
6362 render = { ( { field : { onChange, value } } ) => (
64- < Select
63+ < NERAutocomplete
6564 id = "cr-autocomplete"
66- displayEmpty
67- renderValue = { ( value ) => renderValues . get ( Number ( value ) ) }
68- value = { value }
69- onChange = { ( event : SelectChangeEvent < number > ) => onChange ( event . target . value ) }
65+ onChange = { ( _event , newValue ) => onChange ( newValue ? newValue . id : '' ) }
66+ options = { approvedChangeRequestOptions }
7067 size = { 'small' }
71- placeholder = { 'Change Request Id' }
72- sx = { { height : 56 , width : '100%' , textAlign : 'left' } }
73- error = { ! ! errors . crId }
74- MenuProps = { {
75- anchorOrigin : {
76- vertical : 'bottom' ,
77- horizontal : 'right'
78- } ,
79- transformOrigin : {
80- vertical : 'top' ,
81- horizontal : 'right'
82- }
83- } }
84- >
85- { approvedChangeRequestOptions . map ( ( option ) => (
86- < MenuItem key = { option . value } value = { option . value } >
87- { option . label }
88- </ MenuItem >
89- ) ) }
90- </ Select >
68+ placeholder = { 'Change Request ID' }
69+ value = { approvedChangeRequestOptions . find ( ( cr ) => cr . id === value ) || { id : '' , label : '' } }
70+ errorMessage = { errors . crId }
71+ />
9172 ) }
9273 />
93- < FormHelperText error > { errors . crId ?. message } </ FormHelperText >
9474 </ FormControl >
9575 </ Box >
9676 ) ;
0 commit comments