Skip to content

Commit 9b11ad4

Browse files
(Admin) Jonathan Ruda(Admin) Jonathan Ruda
authored andcommitted
Implement onBlur, comma support, spaces sanitization and leading zeros
1 parent 556e2a5 commit 9b11ad4

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

client/components/mma/accountoverview/updateAmount/ContributionUpdateAmountForm.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,37 @@ export const ContributionUpdateAmountForm = (
160160
};
161161

162162
const onChangeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
163-
const next = event.target.value;
163+
const next = event.target.value.trim();
164164

165165
if (next === '') {
166166
setOtherAmountInput('');
167167
setOtherAmount(null);
168168
return;
169169
}
170170

171-
// Whole-string match: 1+ digits, optional '.', and 0–2 fractional digits (allows '2', '2.', '2.5', '2.50').
172-
if (/^\d+(?:\.\d{0,2})?$/.test(next)) {
173-
setOtherAmountInput(next);
174-
setOtherAmount(Number(next));
171+
// Whole-string match: 1+ digits, optional '.' or ',', and 0–2 fractional digits (allows '2', '2.', '2,', '2.5', '2,50').
172+
if (/^\d+(?:[.,]\d{0,2})?$/.test(next)) {
173+
const normalizedValue = next.replace(',', '.');
174+
175+
setOtherAmountInput(normalizedValue);
176+
setOtherAmount(Number(normalizedValue));
177+
}
178+
};
179+
180+
const onBlurHandler = () => {
181+
let processed = otherAmountInput;
182+
183+
if (processed.endsWith('.')) {
184+
processed = processed.slice(0, -1);
185+
}
186+
187+
// Remove leading zeros
188+
if (processed !== '' && processed !== '0') {
189+
processed = processed.replace(/^0+(?=\d)/, '');
175190
}
191+
192+
setOtherAmountInput(processed);
193+
setOtherAmount(processed === '' ? null : Number(processed));
176194
};
177195

178196
useEffect(() => {
@@ -388,6 +406,7 @@ export const ContributionUpdateAmountForm = (
388406
inputMode="decimal"
389407
value={otherAmountInput}
390408
onChange={onChangeHandler}
409+
onBlur={onBlurHandler}
391410
/>
392411
</div>
393412
)}

cypress/tests/mocked/parallel-2/cancelContribution.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('Cancel contribution', () => {
284284

285285
cy.findByRole('button', { name: 'Reduce amount' }).click();
286286

287-
cy.get('input[type="text"][inputmode="decimal"]').type('80');
287+
cy.findByRole('textbox', { name: /amount/i }).type('80');
288288
cy.findByRole('button', { name: 'Change amount' }).click();
289289

290290
cy.findByText(

0 commit comments

Comments
 (0)