Skip to content

Commit 2aff969

Browse files
committed
refactor: store formatted value in a constant instead of repeating replace() calls
1 parent 2fe9c86 commit 2aff969

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

web-components/src/components/inputs/new/EditableInput/EditableInput.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ export const EditableInput = ({
4343
const [currentValue, setCurrentValue] = useState(value);
4444
const [decimalSeparator, setDecimalSeparator] = useState('.');
4545
const wrapperRef = useRef(null);
46+
const formattedValue = +currentValue.replace('.', ',');
4647

47-
const amountValid =
48-
+currentValue.replace(/,/g, '.') <= maxValue &&
49-
+currentValue.replace(/,/g, '.') > 0;
48+
const amountValid = formattedValue <= maxValue && formattedValue > 0;
5049

5150
const isUpdateDisabled =
52-
!amountValid ||
53-
error?.hasError ||
54-
+initialValue === +currentValue.replace(/,/g, '.');
51+
!amountValid || error?.hasError || +initialValue === formattedValue;
5552

5653
useEffect(() => {
5754
setInitialValue(value);
@@ -112,7 +109,7 @@ export const EditableInput = ({
112109

113110
const handleOnUpdate = () => {
114111
if (isUpdateDisabled) return;
115-
onChange(+currentValue.replace(/,/g, '.'));
112+
onChange(formattedValue);
116113
toggleEditable();
117114
};
118115

0 commit comments

Comments
 (0)