diff --git a/src/components/input/inputNumber/inputNumber.test.tsx b/src/components/input/inputNumber/inputNumber.test.tsx index 5b22f36b2..f4c0ab022 100644 --- a/src/components/input/inputNumber/inputNumber.test.tsx +++ b/src/components/input/inputNumber/inputNumber.test.tsx @@ -28,6 +28,19 @@ describe(' component', () => { expect(screen.queryAllByRole('button').length).toEqual(0); }); + it('should show suffix & prefix only when there is a value', () => { + let value = ''; + const prefix = '~'; + const suffix = '%'; + + const { rerender } = render(createTestComponent({ prefix, suffix })); + expect(screen.queryByDisplayValue(prefix + value + suffix)).not.toBeInTheDocument(); + + value = 'test'; + rerender(createTestComponent({ value, prefix, suffix })); + expect(screen.getByDisplayValue(prefix + value + suffix)).toBeInTheDocument(); + }); + it('should default step to 1 when given value less than zero', () => { const step = -15; render(createTestComponent({ step })); diff --git a/src/components/input/inputNumber/inputNumber.tsx b/src/components/input/inputNumber/inputNumber.tsx index 0e9eb7b73..e96b2e057 100644 --- a/src/components/input/inputNumber/inputNumber.tsx +++ b/src/components/input/inputNumber/inputNumber.tsx @@ -16,17 +16,21 @@ export interface IInputNumberProps extends Omit = (props) => { const { - suffix, - onChange, max = Number.MAX_SAFE_INTEGER, min = Number.MIN_SAFE_INTEGER, step: inputStep = 1, value, + prefix = '', + suffix = '', + onChange, ...otherProps } = props; @@ -52,7 +57,7 @@ export const InputNumber: React.FC = (props) => { const [isFocused, setIsFocused] = useState(false); const { - ref: numberMaskRef, + ref, value: maskedValue, unmaskedValue, setValue, @@ -63,7 +68,7 @@ export const InputNumber: React.FC = (props) => { onChange, }); - const suffixedValue = maskedValue && suffix ? maskedValue + suffix : maskedValue; + const augmentedValue = maskedValue ? `${prefix}${maskedValue}${suffix}` : maskedValue; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'ArrowUp') { @@ -129,17 +134,17 @@ export const InputNumber: React.FC = (props) => { /> )} {!isDisabled && (