Skip to content

Commit

Permalink
fix: Update input to empty string when userValue is undefined (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
aconrad authored Dec 28, 2023
1 parent 75da360 commit 30c5fcc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/components/CurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
transformRawValue,
};

const formattedStateValue =
defaultValue !== undefined && defaultValue !== null
const [stateValue, setStateValue] = useState(() =>
defaultValue != null
? formatValue({ ...formatValueOptions, decimalScale, value: String(defaultValue) })
: userValue !== undefined && userValue !== null
: userValue != null
? formatValue({ ...formatValueOptions, decimalScale, value: String(userValue) })
: '';

const [stateValue, setStateValue] = useState(formattedStateValue);
: ''
);
const [dirty, setDirty] = useState(false);
const [cursor, setCursor] = useState(0);
const [changeCount, setChangeCount] = useState(0);
Expand Down Expand Up @@ -157,7 +156,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
...formatValueOptions,
});

if (cursorPosition !== undefined && cursorPosition !== null) {
if (cursorPosition != null) {
// Prevent cursor jumping
let newCursor = cursorPosition + (formattedValue.length - value.length);
newCursor = newCursor <= 0 ? (prefix ? prefix.length : 0) : newCursor;
Expand Down Expand Up @@ -261,7 +260,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<

const currentValue =
parseFloat(
userValue !== undefined && userValue !== null
userValue != null
? String(userValue).replace(decimalSeparator, '.')
: cleanValue({ value: stateValue, ...cleanValueOptions })
) || 0;
Expand Down Expand Up @@ -315,6 +314,13 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
onKeyUp && onKeyUp(event);
};

// Update state if userValue changes to undefined
useEffect(() => {
if (userValue == null && defaultValue == null) {
setStateValue('');
}
}, [defaultValue, userValue]);

useEffect(() => {
// prevent cursor jumping if editing value
if (
Expand All @@ -333,8 +339,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
*/
const getRenderValue = () => {
if (
userValue !== undefined &&
userValue !== null &&
userValue != null &&
stateValue !== '-' &&
(!decimalSeparator || stateValue !== decimalSeparator)
) {
Expand Down
29 changes: 29 additions & 0 deletions src/components/__tests__/CurrencyInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '@testing-library/jest-dom';
import { render, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import CurrencyInput from '../CurrencyInput';
import { act } from 'react-dom/test-utils';

describe('<CurrencyInput/>', () => {
const onValueChangeSpy = jest.fn();
Expand Down Expand Up @@ -260,4 +261,32 @@ describe('<CurrencyInput/>', () => {

expect(onKeyUpSpy).toBeCalledTimes(1);
});

it('should update the input when prop value changes to another number', () => {
const { rerender } = render(
<CurrencyInput value="1" placeholder="Please enter a number" prefix="£" />
);

const field = screen.getByRole('textbox');
expect(field).toHaveValue('£1');

act(() => {
rerender(<CurrencyInput value="2" placeholder="Please enter a number" prefix="£" />);
});

expect(field).toHaveValue('£2');
});

it('should update the input when prop value changes to undefined', () => {
const { rerender } = render(<CurrencyInput value="1" prefix="£" />);

const field = screen.getByRole('textbox');
expect(field).toHaveValue('£1');

act(() => {
rerender(<CurrencyInput value={undefined} prefix="£" />);
});

expect(field).toHaveValue('');
});
});

0 comments on commit 30c5fcc

Please sign in to comment.