@@ -172,6 +172,7 @@ const ReimbursementProductTable: React.FC<ReimbursementProductTableProps> = ({
172172 const [ showCreateMaterialModal , setShowCreateMaterialModal ] = useState ( false ) ;
173173 const [ currentProductIndex , setCurrentProductIndex ] = useState < number | null > ( null ) ;
174174 const [ currentProject , setCurrentProject ] = useState < ProjectPreview | null > ( null ) ;
175+ const [ pendingMaterialIndices , setPendingMaterialIndices ] = useState < Set < number > > ( new Set ( ) ) ;
175176
176177 const {
177178 data : assemblies ,
@@ -256,6 +257,7 @@ const ReimbursementProductTable: React.FC<ReimbursementProductTableProps> = ({
256257 const handleMaterialCreated = ( materialName : string ) => {
257258 if ( currentProductIndex !== null ) {
258259 setValue ( `reimbursementProducts.${ currentProductIndex } .name` , materialName ) ;
260+ setPendingMaterialIndices ( ( prev ) => new Set ( prev ) . add ( currentProductIndex ) ) ;
259261 }
260262 handleCloseCreateMaterial ( ) ;
261263 } ;
@@ -564,53 +566,68 @@ const ReimbursementProductTable: React.FC<ReimbursementProductTableProps> = ({
564566 >
565567 { hasWbsNum ? (
566568 < FormControl fullWidth margin = "dense" variant = "outlined" size = "small" >
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- setValue (
580- `reimbursementProducts.${ product . index } .materialId` ,
581- material . materialId ,
582- {
569+ { watch ( `reimbursementProducts.${ product . index } .materialId` ) || ! watch ( `reimbursementProducts.${ product . index } .name` ) || pendingMaterialIndices . has ( product . index ) ? (
570+ < Box sx = { { display : 'flex' , gap : 1 , alignItems : 'center' } } >
571+ < Box sx = { { flex : 1 } } >
572+ < MaterialAutocomplete
573+ wbsNum = { product . reason as WbsNumber }
574+ initialValue = { watch ( `reimbursementProducts.${ product . index } .name` ) }
575+ onSelect = { ( material ) => {
576+ if ( material ) {
577+ const label = `${ material . name } (${ material . materialTypeName } ): ${ material . manufacturerName ?? 'N/A' } , ${ material . manufacturerPartNumber ?? 'N/A' } ` ;
578+ setValue ( `reimbursementProducts.${ product . index } .name` , label , {
583579 shouldValidate : true ,
584580 shouldDirty : true
585- }
586- ) ;
587- } else {
588- setValue ( `reimbursementProducts.${ product . index } .name` , '' , {
589- shouldValidate : true ,
590- shouldDirty : true
591- } ) ;
592- setValue ( `reimbursementProducts.${ product . index } .materialId` , undefined , {
593- shouldValidate : true ,
594- shouldDirty : true
595- } ) ;
596- }
597- } }
598- />
581+ } ) ;
582+ setValue (
583+ `reimbursementProducts.${ product . index } .materialId` ,
584+ material . materialId ,
585+ {
586+ shouldValidate : true ,
587+ shouldDirty : true
588+ }
589+ ) ;
590+ setPendingMaterialIndices ( ( prev ) => {
591+ const next = new Set ( prev ) ;
592+ next . delete ( product . index ) ;
593+ return next ;
594+ } ) ;
595+ } else {
596+ setValue ( `reimbursementProducts.${ product . index } .name` , '' , {
597+ shouldValidate : true ,
598+ shouldDirty : true
599+ } ) ;
600+ setValue ( `reimbursementProducts.${ product . index } .materialId` , undefined , {
601+ shouldValidate : true ,
602+ shouldDirty : true
603+ } ) ;
604+ }
605+ } }
606+ />
607+ </ Box >
608+ < Typography fontWeight = "bold" sx = { { whiteSpace : 'nowrap' } } >
609+ OR
610+ </ Typography >
611+ < Button
612+ variant = "outlined"
613+ size = "small"
614+ onClick = { ( ) =>
615+ handleOpenCreateMaterial ( product . index , product . reason as WbsNumber )
616+ }
617+ sx = { { whiteSpace : 'nowrap' } }
618+ >
619+ Create New Material
620+ </ Button >
599621 </ Box >
600- < Typography fontWeight = "bold" sx = { { whiteSpace : 'nowrap' } } >
601- OR
602- </ Typography >
603- < Button
622+ ) : (
623+ < TextField
604624 variant = "outlined"
625+ value = { watch ( `reimbursementProducts.${ product . index } .name` ) }
626+ fullWidth
605627 size = "small"
606- onClick = { ( ) =>
607- handleOpenCreateMaterial ( product . index , product . reason as WbsNumber )
608- }
609- sx = { { whiteSpace : 'nowrap' } }
610- >
611- Create New Material
612- </ Button >
613- </ Box >
628+ disabled
629+ />
630+ ) }
614631 < FormHelperText error >
615632 { errors . reimbursementProducts ?. [ product . index ] ?. name ?. message }
616633 </ FormHelperText >
0 commit comments