Skip to content
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

fix: onValueChange not called for values with 3 or less characters #842

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,14 @@ export default function NumberFormatBase<BaseType = InputAttributes>(

/**
* if the formatted value is not synced to parent, or if the formatted value is different from last synced value sync it
* we also don't need to sync to the parent if no formatting is applied
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour contradicts what is described in the documentation:

This handler provides access to any values changes in the input field and is triggered only when a prop changes or the user input changes

value is a prop, so any change should trigger the onValueChange. If the original intent of the function was to trigger on formatting changes, it should have been better described and/or be a different prop.

* if the formatting props is removed, in which case last formatted value will be different from the numeric string value
* in such case we need to inform the parent.
*/
useEffect(() => {
const { formattedValue: lastFormattedValue, numAsString: lastNumAsString } =
lastUpdatedValue.current;
if (
formattedValue !== lastFormattedValue &&
(formattedValue !== numAsString || lastFormattedValue !== lastNumAsString)
) {

if (formattedValue !== lastFormattedValue || numAsString !== lastNumAsString) {
_onValueChange(getValueObject(formattedValue, numAsString), {
event: undefined,
source: SourceType.props,
Expand Down
88 changes: 66 additions & 22 deletions test/library/input.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,27 +439,6 @@ describe('NumberFormat as input', () => {
});
});

it('should not call onValueChange if no formatting is applied', async () => {
const mockOnValueChange = vi.fn();

const { rerender } = await render(<NumericFormat value="" onValueChange={mockOnValueChange} />);

expect(mockOnValueChange).not.toHaveBeenCalled();

rerender(<NumericFormat value={NaN} onValueChange={mockOnValueChange} />);
expect(mockOnValueChange).not.toHaveBeenCalled();

rerender(<NumericFormat value={1234} onValueChange={mockOnValueChange} />);
expect(mockOnValueChange).not.toHaveBeenCalled();

rerender(<NumericFormat value={1234} onValueChange={mockOnValueChange} thousandSeparator />);
expect(mockOnValueChange.mock.lastCall[0]).toEqual({
formattedValue: '1,234',
value: '1234',
floatValue: 1234,
});
});

Comment on lines -442 to -462
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be a correct test, why should onValueChange not be called when the input goes from a NaN value to a meaningful value? I have tried to capture this, and further scenarios in the test I have added below, please let me know if this should be adjusted or kept instead of being removed.

it('should always call setState when input is not on focus and value formatting is changed from outside', async () => {
const { input, user, rerender } = await render(
<NumericFormat value="1.1" valueIsNumericString />,
Expand Down Expand Up @@ -526,7 +505,7 @@ describe('NumberFormat as input', () => {
expect(source).toEqual('event');
});

it('should call onValueChange when value changes', async () => {
it('should call onValueChange when value changes via user input', async () => {
const mockOnValueChange = vi.fn();

const { input, user, rerender } = await render(
Expand All @@ -545,8 +524,73 @@ describe('NumberFormat as input', () => {
);
expect(mockOnValueChange).toHaveBeenCalled();

mockOnValueChange.mockReset();

await simulateKeyInput(user, input, '5', 0);
expect(input).toHaveValue('51,234');
expect(mockOnValueChange).toHaveBeenCalled();

await simulateKeyInput(user, input, '{Backspace}', 6);
expect(input).toHaveValue('5,123');
expect(mockOnValueChange).toHaveBeenCalled();

mockOnValueChange.mockReset();

await simulateKeyInput(user, input, '{Backspace}', 5);
expect(input).toHaveValue('512');
expect(mockOnValueChange).toHaveBeenCalled();

mockOnValueChange.mockReset();

await simulateKeyInput(user, input, '{Backspace}', 4);
expect(input).toHaveValue('51');
expect(mockOnValueChange).toHaveBeenCalled();
});

it('should call onValueChange when value changes via props', async () => {
const mockOnValueChange = vi.fn();

const { input, rerender } = await render(
<NumericFormat value="1234" valueIsNumericString={true} onValueChange={mockOnValueChange} />,
);

expect(input).toHaveValue('1234');

rerender(
<NumericFormat
onValueChange={mockOnValueChange}
thousandSeparator
value="1234"
valueIsNumericString={true}
/>,
);
expect(mockOnValueChange).toHaveBeenCalled();

mockOnValueChange.mockReset();

rerender(
<NumericFormat
onValueChange={mockOnValueChange}
thousandSeparator
value="123"
valueIsNumericString={true}
/>,
);
expect(mockOnValueChange).toHaveBeenCalled();

mockOnValueChange.mockReset();

rerender(
<NumericFormat
onValueChange={mockOnValueChange}
thousandSeparator
value="12"
valueIsNumericString={true}
/>,
);
expect(mockOnValueChange).toHaveBeenCalled();

console.log({ input });
});

it('should treat Infinity value as empty string', async () => {
Expand Down
Loading