Skip to content

Commit 2699b8f

Browse files
committed
Make negative numbers positive when allowNegative is disabled
- When allowNegative changes from true to false, any negative value is converted to positive - Added test to verify negative sign is removed when allowNegative is disabled - All 543 tests passing
1 parent 18d4b4b commit 2699b8f

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

src/index.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,22 @@ class CurrencyInput extends React.Component<CurrencyInputProps, CurrencyInputSta
210210
return { ...newState, previousProps: nextProps };
211211
}
212212

213-
// Other props changed (allowNegative) but value and display formatting didn't
214-
// Don't reformat - just update the previousProps reference and preserve current state
215-
// This allows allowNegative to toggle without erasing the user's input
213+
// Check if allowNegative changed
214+
const allowNegativeChanged = nextProps.allowNegative !== previousProps.allowNegative;
215+
216+
if (allowNegativeChanged && !nextProps.allowNegative) {
217+
// allowNegative was disabled
218+
// If current value is negative, make it positive
219+
const currentValue = typeof prevState.value === 'number' ? prevState.value : 0;
220+
if (currentValue < 0) {
221+
const propsWithPositiveValue = { ...nextProps, value: Math.abs(currentValue) };
222+
const newState = CurrencyInput.prepareProps(propsWithPositiveValue);
223+
return { ...newState, previousProps: nextProps };
224+
}
225+
}
226+
227+
// Other props changed but value and display formatting didn't
228+
// Just update the previousProps reference and preserve current state
216229
return { ...prevState, previousProps: nextProps };
217230
}
218231

tests/base.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,36 @@ test.describe('component parameters', () => {
218218
value = await currencyInput.inputValue();
219219
expect(value).toContain('-');
220220
});
221+
222+
test('removes negative sign when allowNegative is disabled', async ({ page }) => {
223+
// First enable allowNegative
224+
const allowNegativeCheckbox = page.locator('[name=allowNegative]');
225+
const applyBtn = page.locator('[name=apply]');
226+
227+
await allowNegativeCheckbox.check();
228+
await applyBtn.click();
229+
await page.waitForTimeout(200);
230+
231+
const currencyInput = page.locator('#currency-input');
232+
await currencyInput.focus();
233+
await currencyInput.selectText();
234+
235+
// Input a negative number
236+
await currencyInput.pressSequentially('50');
237+
await currencyInput.press('Minus');
238+
239+
let value = await currencyInput.inputValue();
240+
expect(value).toContain('-');
241+
242+
// Now disable allowNegative
243+
await allowNegativeCheckbox.uncheck();
244+
await applyBtn.click();
245+
await page.waitForTimeout(200);
246+
247+
// Value should now be positive
248+
value = await currencyInput.inputValue();
249+
expect(value).not.toContain('-');
250+
});
221251
});
222252

223253
test.describe('allowEmpty', () => {

0 commit comments

Comments
 (0)