Skip to content

Commit

Permalink
fix: support dynamic formatter (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Oct 13, 2023
1 parent ebd65ca commit 4453c15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ const InternalInputNumber = React.forwardRef(
};

// ========================== Controlled ==========================
// Input by precision
// Input by precision & formatter
useLayoutUpdateEffect(() => {
if (!decimalValue.isInvalidate()) {
setInputValue(decimalValue, false);
}
}, [precision]);
}, [precision, formatter]);

// Input by value
useLayoutUpdateEffect(() => {
Expand Down
32 changes: 32 additions & 0 deletions tests/formatter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,36 @@ describe('InputNumber.Formatter', () => {
expect(formatter).toHaveBeenCalledTimes(1);
expect(formatter).toHaveBeenCalledWith('1', { userTyping: true, input: '1' });
});

describe('dynamic formatter', () => {
it('uncontrolled', () => {
const { container, rerender } = render(
<InputNumber defaultValue={93} formatter={(val) => `$${val}`} />,
);

expect(container.querySelector<HTMLInputElement>('.rc-input-number-input').value).toEqual(
'$93',
);

rerender(<InputNumber defaultValue={93} formatter={(val) => `*${val}`} />);
expect(container.querySelector<HTMLInputElement>('.rc-input-number-input').value).toEqual(
'*93',
);
});

it('controlled', () => {
const { container, rerender } = render(
<InputNumber value={510} formatter={(val) => `$${val}`} />,
);

expect(container.querySelector<HTMLInputElement>('.rc-input-number-input').value).toEqual(
'$510',
);

rerender(<InputNumber value={510} formatter={(val) => `*${val}`} />);
expect(container.querySelector<HTMLInputElement>('.rc-input-number-input').value).toEqual(
'*510',
);
});
});
});

0 comments on commit 4453c15

Please sign in to comment.