@@ -85,12 +85,25 @@ const MaterialAutocomplete: React.FC<{
8585 ) ;
8686
8787 useEffect ( ( ) => {
88- if ( materials && materialSelected === null && initialValue ) {
89- const match = materialOptions . find ( ( o ) => o . label === initialValue ) ;
90- if ( match ) setMaterialSelected ( match ) ;
88+ if ( ! materials || ! initialValue ) return ;
89+
90+ // Fetch pre-existing label
91+ let match = materialOptions . find ( ( o ) => o . label === initialValue ) ?? null ;
92+
93+ // Otherwise fetch new material by name
94+ if ( ! match ) {
95+ const materialByName = materials . find ( ( m ) => m . name === initialValue ) ;
96+ if ( materialByName ) match = materialOptions . find ( ( o ) => o . id === materialByName . materialId ) ?? null ;
97+ }
98+
99+ if ( match && match . id !== materialSelected ?. id ) {
100+ setMaterialSelected ( match ) ;
101+ // Update the form value to the formatted label
102+ const fullMaterial = materials . find ( ( m ) => m . materialId === match ! . id ) ?? null ;
103+ onSelect ( fullMaterial ) ;
91104 }
92105 // eslint-disable-next-line react-hooks/exhaustive-deps
93- } , [ materials ] ) ;
106+ } , [ materials , initialValue ] ) ;
94107
95108 if ( isLoading || ! materials ) {
96109 return < LoadingIndicator /> ;
@@ -551,24 +564,41 @@ const ReimbursementProductTable: React.FC<ReimbursementProductTableProps> = ({
551564 >
552565 { hasWbsNum ? (
553566 < FormControl fullWidth margin = "dense" variant = "outlined" size = "small" >
554- < MaterialAutocomplete
555- wbsNum = { product . reason as WbsNumber }
556- initialValue = { watch ( `reimbursementProducts.${ product . index } .name` ) }
557- onSelect = { ( material ) => {
558- if ( material ) {
559- const label = `${ material . name } (${ material . materialTypeName } ): ${ material . manufacturerName ?? 'N/A' } , ${ material . manufacturerPartNumber ?? 'N/A' } ` ;
560- setValue ( `reimbursementProducts.${ product . index } .name` , label , {
561- shouldValidate : true ,
562- shouldDirty : true
563- } ) ;
564- } else {
565- setValue ( `reimbursementProducts.${ product . index } .name` , '' , {
566- shouldValidate : true ,
567- shouldDirty : true
568- } ) ;
567+ < Box sx = { { display : 'flex' , gap : 1 , alignItems : 'center' } } >
568+ < Box sx = { { flex : 1 } } >
569+ < MaterialAutocomplete
570+ wbsNum = { product . reason as WbsNumber }
571+ initialValue = { watch ( `reimbursementProducts.${ product . index } .name` ) }
572+ onSelect = { ( material ) => {
573+ if ( material ) {
574+ const label = `${ material . name } (${ material . materialTypeName } ): ${ material . manufacturerName ?? 'N/A' } , ${ material . manufacturerPartNumber ?? 'N/A' } ` ;
575+ setValue ( `reimbursementProducts.${ product . index } .name` , label , {
576+ shouldValidate : true ,
577+ shouldDirty : true
578+ } ) ;
579+ } else {
580+ setValue ( `reimbursementProducts.${ product . index } .name` , '' , {
581+ shouldValidate : true ,
582+ shouldDirty : true
583+ } ) ;
584+ }
585+ } }
586+ />
587+ </ Box >
588+ < Typography fontWeight = "bold" sx = { { whiteSpace : 'nowrap' } } >
589+ OR
590+ </ Typography >
591+ < Button
592+ variant = "outlined"
593+ size = "small"
594+ onClick = { ( ) =>
595+ handleOpenCreateMaterial ( product . index , product . reason as WbsNumber )
569596 }
570- } }
571- />
597+ sx = { { whiteSpace : 'nowrap' } }
598+ >
599+ Create New Material
600+ </ Button >
601+ </ Box >
572602 < FormHelperText error >
573603 { errors . reimbursementProducts ?. [ product . index ] ?. name ?. message }
574604 </ FormHelperText >
0 commit comments