@@ -7,7 +7,7 @@ import Navigation from '@libs/Navigation/Navigation';
77import { getHeaderMessageForNonUserList } from '@libs/OptionsListUtils' ;
88import { getTaxRatesSection } from '@libs/TaxOptionsListUtils' ;
99import type { TaxRatesOption } from '@libs/TaxOptionsListUtils' ;
10- import { getEnabledTaxRateCount } from '@libs/TransactionUtils' ;
10+ import { getDefaultTaxCode , getEnabledTaxRateCount , transformedTaxRates } from '@libs/TransactionUtils' ;
1111import CONST from '@src/CONST' ;
1212import type { IOUAction } from '@src/CONST' ;
1313import ONYXKEYS from '@src/ONYXKEYS' ;
@@ -26,7 +26,7 @@ type TaxPickerProps = {
2626 transactionID ?: string ;
2727
2828 /** Callback to fire when a tax is pressed */
29- onSubmit : ( tax : TaxRatesOption ) => void ;
29+ onSubmit : ( tax : TaxRatesOption , shouldClearTax ?: boolean ) => void ;
3030
3131 /** The action to take */
3232 action ?: IOUAction ;
@@ -68,10 +68,29 @@ function TaxPicker({selectedTaxRate = '', policyID, transactionID, onSubmit, act
6868
6969 const shouldShowTextInput = ! isTaxRatesCountBelowThreshold ;
7070
71- const selectedOptions = selectedTaxRate
71+ const { taxCode, taxValue} = currentTransaction ?? { } ;
72+ const defaultTaxCode = getDefaultTaxCode ( policy , currentTransaction ) ?? '' ;
73+ const effectiveTaxCode = taxCode && taxCode . length > 0 ? taxCode : defaultTaxCode ;
74+ const effectiveSelectedTaxRate = selectedTaxRate || ( effectiveTaxCode ? ( transformedTaxRates ( policy , currentTransaction ) [ effectiveTaxCode ] ?. modifiedName ?? '' ) : '' ) ;
75+ const hasTaxBeenDeleted = ! ! taxCode && taxValue !== undefined && ! taxRates ?. taxes ?. [ taxCode ] ;
76+ const hasTaxValueChanged = ! ! taxCode && taxValue !== undefined && taxRates ?. taxes ?. [ taxCode ] ?. value !== taxValue ;
77+
78+ const deletedTaxOption = ! hasTaxBeenDeleted
79+ ? null
80+ : {
81+ code : undefined ,
82+ text : taxValue ?? '' ,
83+ keyForList : taxCode ?? '' ,
84+ searchText : taxValue ?? '' ,
85+ tooltipText : taxValue ?? '' ,
86+ isDisabled : true ,
87+ isSelected : true ,
88+ } ;
89+
90+ const selectedOptions = effectiveSelectedTaxRate
7291 ? [
7392 {
74- modifiedName : selectedTaxRate ,
93+ modifiedName : effectiveSelectedTaxRate ,
7594 isDisabled : false ,
7695 accountID : null ,
7796 } ,
@@ -86,14 +105,26 @@ function TaxPicker({selectedTaxRate = '', policyID, transactionID, onSubmit, act
86105 transaction : currentTransaction ,
87106 } ) ;
88107
89- const selectedOptionKey = sections ?. at ( 0 ) ?. data ?. find ( ( taxRate ) => taxRate . searchText === selectedTaxRate ) ?. keyForList ;
108+ const flattenedOptions = sections . flatMap ( ( section ) => section . data ) ;
109+ const selectedOptionKey =
110+ flattenedOptions . find ( ( taxRate ) => taxRate . code === effectiveTaxCode ) ?. keyForList ?? flattenedOptions . find ( ( taxRate ) => taxRate . searchText === effectiveSelectedTaxRate ) ?. keyForList ;
90111
91112 const handleSelectRow = ( newSelectedOption : TaxRatesOption ) => {
92- if ( selectedOptionKey === newSelectedOption . keyForList ) {
113+ if ( hasTaxValueChanged ) {
114+ onSubmit ( newSelectedOption , ! newSelectedOption . code ) ;
115+ return ;
116+ }
117+
118+ const isSameTaxCode = taxCode === newSelectedOption . code ;
119+ const currentTaxRateValue = taxCode ? taxRates ?. taxes ?. [ taxCode ] ?. value : undefined ;
120+ const hasMatchingTaxValue = taxValue === undefined || currentTaxRateValue === taxValue ;
121+
122+ if ( isSameTaxCode && hasMatchingTaxValue ) {
93123 onDismiss ( ) ;
94124 return ;
95125 }
96- onSubmit ( newSelectedOption ) ;
126+
127+ onSubmit ( newSelectedOption , hasTaxBeenDeleted ) ;
97128 } ;
98129
99130 const textInputOptions = {
@@ -103,9 +134,16 @@ function TaxPicker({selectedTaxRate = '', policyID, transactionID, onSubmit, act
103134 headerMessage : getHeaderMessageForNonUserList ( ( sections . at ( 0 ) ?. data ?. length ?? 0 ) > 0 , searchValue ) ,
104135 } ;
105136
137+ const updatedSections = deletedTaxOption
138+ ? sections . map ( ( section ) => ( {
139+ ...section ,
140+ data : [ ...section . data . filter ( ( item ) => item . code !== deletedTaxOption . code ) , deletedTaxOption ] ,
141+ } ) )
142+ : sections ;
143+
106144 return (
107145 < SelectionListWithSections
108- sections = { sections }
146+ sections = { updatedSections }
109147 shouldShowTextInput = { shouldShowTextInput }
110148 textInputOptions = { textInputOptions }
111149 onSelectRow = { handleSelectRow }
0 commit comments