Skip to content

Commit 7dc1321

Browse files
Handled intial value formatting
1 parent d09178a commit 7dc1321

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/components/Input/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const Input = forwardRef(
5050
const errorId = `error_${id}`;
5151
const helpTextId = `helpText_${id}`;
5252

53-
const value = otherProps.value ?? valueInternal ?? "";
53+
const value =
54+
formatWithPrecision(otherProps.value, precision) ?? valueInternal ?? "";
5455

5556
const valueLength = value?.toString().length || 0;
5657
const isCharacterLimitVisible = valueLength >= maxLength * 0.85;

src/components/Input/utils.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isNotPresent } from "neetocist";
21
import { replace } from "ramda";
32

43
const toFixed = (numStr, precision) => {
@@ -15,7 +14,7 @@ const isValidNumberString = numStr => {
1514
};
1615

1716
export const formatWithPrecision = (value, precision) => {
18-
if (precision < 0) return value;
17+
if (precision < 0 || !value) return value;
1918

2019
const str = value.toString();
2120

@@ -25,10 +24,9 @@ export const formatWithPrecision = (value, precision) => {
2524
};
2625

2726
export const enforceDecimalPrecision = (value, precision) => {
28-
if (precision < 0) return value;
27+
if (precision < 0 || !value) return value;
2928

3029
const valueStr = value.toString();
31-
if (isNotPresent(valueStr)) return "";
3230

3331
if (precision === 0) return valueStr.split(".")[0];
3432

stories/Components/Input.stories.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ RejectCharsInputStory.parameters = {
199199
};
200200

201201
const PrecisionInputStory = args => (
202-
<Input {...args} label="Input (up to 2 decimal places)" precision={2} />
202+
<Input
203+
label={`Input (up to ${args.precision} decimal places)`}
204+
precision={args.precision}
205+
{...args}
206+
/>
203207
);
204208

205209
PrecisionInputStory.storyName = "Precision";

tests/Input.test.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,19 @@ describe("Input", () => {
182182
await userEvent.clear(input);
183183
await userEvent.type(input, "45.67");
184184
expect(input).toHaveValue("4567");
185+
186+
rerender(<Input label="label" />);
187+
await userEvent.clear(input);
188+
await userEvent.type(input, "45.677");
189+
expect(input).toHaveValue("45.677");
190+
191+
rerender(<Input label="label" precision={2} value={45.677} />);
192+
expect(input).toHaveValue("45.68");
193+
194+
rerender(<Input label="label" precision={3} value={45.6} />);
195+
expect(input).toHaveValue("45.600");
196+
197+
rerender(<Input label="label" value={45.677} />);
198+
expect(input).toHaveValue("45.677");
185199
});
186200
});

0 commit comments

Comments
 (0)