Repository navigation
Revert "Allow edit input for SplitListItem when there is negative value" #81717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,23 +102,20 @@ function calculateAmount(numberOfSplits: number, total: number, currency: string | |
|
|
||
| /** | ||
| * Calculate a split amount in backend cents from a percentage of the original amount. | ||
| * - Supports negative percentages (for cases where someone owes money back to the group) | ||
| * - Clamps percentage to [0, 100] | ||
| * - Preserves decimal precision in percentage (supports 0.1 precision) | ||
| * - Handles all sign combinations: positive/negative amounts with positive/negative percentages | ||
| * - Preserves the sign of the original amount (negative amounts stay negative) | ||
| */ | ||
| function calculateSplitAmountFromPercentage(totalInCents: number, percentage: number): number { | ||
| // Calculate directly without clamping to support negative percentages | ||
| // This naturally handles all sign combinations: | ||
| // - Positive total × Positive % = Positive split (normal expense share) | ||
| // - Positive total × Negative % = Negative split (person owes money back) | ||
| // - Negative total × Positive % = Negative split (person gets refund share) | ||
| // - Negative total × Negative % = Positive split (person owes money on a refund) | ||
| const amount = Math.round((totalInCents * percentage) / 100); | ||
| const totalAbs = Math.abs(totalInCents); | ||
| // Clamp percentage to [0, 100] without rounding to preserve decimal precision | ||
| const clamped = Math.min(100, Math.max(0, percentage)); | ||
| const amount = Math.round((totalAbs * clamped) / 100); | ||
|
Comment on lines
109
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The split-by-percentage input still enables Useful? React with 👍 / 👎. |
||
| // Return 0 for zero amounts to avoid -0 | ||
| if (amount === 0) { | ||
| return 0; | ||
| } | ||
| return amount; | ||
| return totalInCents < 0 ? -amount : amount; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -129,7 +126,6 @@ function calculateSplitAmountFromPercentage(totalInCents: number, percentage: nu | |
| * - The remainder needed to reach 100% goes to the last item (which should be the largest) | ||
| * - When the sum of split amounts does not match the original total (over/under splits), percentages still reflect | ||
| * each amount as a percentage of the original total and may sum to something other than 100 | ||
| * - Preserves the sign of amounts (negative amounts result in negative percentages) | ||
| */ | ||
| function calculateSplitPercentagesFromAmounts(amountsInCents: number[], totalInCents: number): number[] { | ||
| const totalAbs = Math.abs(totalInCents); | ||
|
|
@@ -138,45 +134,32 @@ function calculateSplitPercentagesFromAmounts(amountsInCents: number[], totalInC | |
| return amountsInCents.map(() => 0); | ||
| } | ||
|
|
||
| const amountsAbs = amountsInCents.map((amount) => Math.abs(amount ?? 0)); | ||
|
|
||
| // Helper functions for decimal precision | ||
| const roundToOneDecimal = (value: number): number => Math.round(value * 10) / 10; | ||
| const floorToOneDecimal = (value: number): number => Math.floor(value * 10) / 10; | ||
|
|
||
| // Calculate percentages based on the relationship between amount sign and total sign | ||
| // When they match (both positive or both negative), show positive percentage | ||
| // When they differ, show negative percentage | ||
| const percentages = amountsInCents.map((amount) => { | ||
| if (totalAbs === 0) { | ||
| return 0; | ||
| } | ||
| let percentage = (amount / totalAbs) * 100; | ||
| // Flip sign if original total is negative (so user sees intuitive percentages) | ||
| if (totalInCents < 0) { | ||
| percentage = -percentage; | ||
| } | ||
| // For negative percentages, floor towards zero (Math.ceil for negative numbers) | ||
| return percentage < 0 ? Math.ceil(percentage * 10) / 10 : floorToOneDecimal(percentage); | ||
| }); | ||
| // ALWAYS use floored percentages to guarantee equal amounts get equal percentages | ||
| const flooredPercentages = amountsAbs.map((amount) => (totalAbs > 0 ? floorToOneDecimal((amount / totalAbs) * 100) : 0)); | ||
|
|
||
| // For remainder calculation, we need to work with absolute values to ensure equal amounts get equal percentages | ||
| const amountsAbs = amountsInCents.map((amount) => Math.abs(amount ?? 0)); | ||
| const amountsTotal = amountsAbs.reduce((sum, curr) => sum + curr, 0); | ||
|
|
||
| // If the split amounts don't add up to the original total, return percentages as-is | ||
| // If the split amounts don't add up to the original total, return floored percentages as-is | ||
| // (the sum may not be 100, but that's expected when there's a validation error) | ||
| if (amountsTotal !== totalAbs) { | ||
| return percentages; | ||
| return flooredPercentages; | ||
| } | ||
|
|
||
| // Calculate remainder based on absolute sum and add it to the LAST item with MAXIMUM absolute amount | ||
| const sumOfAbsPercentages = roundToOneDecimal(percentages.map(Math.abs).reduce((sum, current) => sum + current, 0)); | ||
| const remainder = roundToOneDecimal(100 - sumOfAbsPercentages); | ||
| // Calculate remainder and add it to the LAST item (which should be the largest in even splits) | ||
| const sumOfFlooredPercentages = roundToOneDecimal(flooredPercentages.reduce((sum, current) => sum + current, 0)); | ||
| const remainder = roundToOneDecimal(100 - sumOfFlooredPercentages); | ||
|
|
||
| if (remainder <= 0) { | ||
| return percentages; | ||
| return flooredPercentages; | ||
| } | ||
|
|
||
| // Add remainder to the last item with the MAXIMUM absolute amount | ||
| // Add remainder to the last item with the MAXIMUM amount (not just the last item since that can be a new split with 0 amount) | ||
| // This ensures 0-amount splits stay at 0% | ||
| const maxAmount = Math.max(...amountsAbs); | ||
| let lastMaxIndex = amountsAbs.length - 1; // fallback to last | ||
|
|
@@ -186,10 +169,8 @@ function calculateSplitPercentagesFromAmounts(amountsInCents: number[], totalInC | |
| } | ||
| } | ||
|
|
||
| const adjustedPercentages = [...percentages]; | ||
| const currentPercentage = adjustedPercentages.at(lastMaxIndex) ?? 0; | ||
| // Add remainder with the same sign as the current percentage | ||
| adjustedPercentages[lastMaxIndex] = roundToOneDecimal(currentPercentage + (currentPercentage < 0 ? -remainder : remainder)); | ||
| const adjustedPercentages = [...flooredPercentages]; | ||
| adjustedPercentages[lastMaxIndex] = roundToOneDecimal((adjustedPercentages.at(lastMaxIndex) ?? 0) + remainder); | ||
|
|
||
| return adjustedPercentages; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding
keyboardTypetoDECIMAL_PADremoves the ability for callers (e.g., split amount inputs) to request a keyboard with a minus key. On iOS,decimal-paddoesn’t include “-”, so if a split amount is negative (refunds/adjustments) and the user clears or replaces the text, they can’t re-enter the negative sign. PreviouslySplitAmountInputpassed a numbers-and-punctuation keyboard to handle this case.Useful? React with 👍 / 👎.