Skip to content

Commit

Permalink
feat: introduce formatValueOnBlur flag (#339)
Browse files Browse the repository at this point in the history
Co-authored-by: Selmir Nedzibi <[email protected]>
  • Loading branch information
Contraboi and Contraboi authored Feb 22, 2024
1 parent 6cfc64a commit 5e30154
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/components/CurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
onKeyDown,
onKeyUp,
transformRawValue,
formatValueOnBlur = true,
...props
}: CurrencyInputProps,
ref
Expand Down Expand Up @@ -231,7 +232,7 @@ export const CurrencyInput: FC<CurrencyInputProps> = forwardRef<
value: newValue,
});

if (onValueChange) {
if (onValueChange && formatValueOnBlur) {
onValueChange(newValue, name, {
float: numberValue,
formatted: formattedValue,
Expand Down
7 changes: 7 additions & 0 deletions src/components/CurrencyInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,12 @@ export type CurrencyInputProps = Overwrite<
* Transform the raw value form the input before parsing
*/
transformRawValue?: (rawValue: string) => string;

/**
* When set to false, the formatValueOnBlur flag disables the application of the __onValueChange__ function
* specifically on blur events. If disabled or set to false, the onValueChange will not trigger on blur.
* Default = true
*/
formatValueOnBlur?: boolean;
}
>;
29 changes: 25 additions & 4 deletions src/components/__tests__/CurrencyInput-onBlur.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('<CurrencyInput/> onBlur', () => {
userEvent.type(screen.getByRole('textbox'), '123');
fireEvent.focusOut(screen.getByRole('textbox'));

expect(onBlurSpy).toBeCalled();
expect(onBlurSpy).toHaveBeenCalled();

expect(onValueChangeSpy).toHaveBeenLastCalledWith('123.00', name, {
float: 123,
Expand All @@ -40,13 +40,34 @@ describe('<CurrencyInput/> onBlur', () => {
expect(screen.getByRole('textbox')).toHaveValue('$123.00');
});

it('should call onBlur, but not onValueChange', () => {
render(
<CurrencyInput
name={name}
prefix="$"
onBlur={onBlurSpy}
onValueChange={onValueChangeSpy}
formatValueOnBlur={false}
decimalScale={2}
/>
);

userEvent.type(screen.getByRole('textbox'), '123');
fireEvent.focusOut(screen.getByRole('textbox'));

expect(onBlurSpy).toHaveBeenCalled();

expect(onValueChangeSpy).toHaveBeenCalledTimes(3);
expect(screen.getByRole('textbox')).toHaveValue('$123.00');
});

it('should call onBlur for 0', () => {
render(<CurrencyInput name={name} prefix="$" onBlur={onBlurSpy} />);

userEvent.type(screen.getByRole('textbox'), '0');
fireEvent.focusOut(screen.getByRole('textbox'));

expect(onBlurSpy).toBeCalled();
expect(onBlurSpy).toHaveBeenCalled();

expect(screen.getByRole('textbox')).toHaveValue('$0');
});
Expand All @@ -56,7 +77,7 @@ describe('<CurrencyInput/> onBlur', () => {

fireEvent.focusOut(screen.getByRole('textbox'));

expect(onBlurSpy).toBeCalled();
expect(onBlurSpy).toHaveBeenCalled();

expect(screen.getByRole('textbox')).toHaveValue('');
});
Expand All @@ -67,7 +88,7 @@ describe('<CurrencyInput/> onBlur', () => {
userEvent.type(screen.getByRole('textbox'), '-');
fireEvent.focusOut(screen.getByRole('textbox'));

expect(onBlurSpy).toBeCalled();
expect(onBlurSpy).toHaveBeenCalled();

expect(screen.getByRole('textbox')).toHaveValue('');
});
Expand Down

0 comments on commit 5e30154

Please sign in to comment.