Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const handleKeyDown = (e) => {
const { type, key } = e;
const stringValue = ref.current.value;
const numberValue = stringValue ? Number(numbro.unformat(stringValue)) : 0;
// default to 0 if defaultValue is NaN
if (is.number(numberValue) && !is.empty(stringValue)) {
let newValue = numberValue;
if (key === 'ArrowDown') {
newValue = Number(numbro(numberValue).subtract(props.step).format({ mantissa: countDecimals(props.step) }));
if (greaterThanMin(newValue) && lessThanMax(newValue)) {
const updateError = displayErrorMessage(!is.empty(stringValue) ? newValue : '');
context.updateState({ value: toCurrency(newValue, countDecimals(props.step)), ...updateError }, () => {
if (is.fn(props.onChange)) {
props.onChange(newValue, props.id, type, key);
}
});
}
} else if (key === 'ArrowUp') {
newValue = Number(numbro(numberValue).add(props.step).format({ mantissa: countDecimals(props.step) }));
if (greaterThanMin(newValue) && lessThanMax(newValue)) {
function handleOnBlur(e: React.FocusEvent) {
const input = e.currentTarget as HTMLInputElement;
let value = ValueLineController.autoFixString(input.value, false);
if (numbro.languageData().delimiters.decimal == ',' && !value.contains(",") && value.trim().length > 0) //Numbro transforms 1.000 to 1,0 in spanish or german
value = value + ",00";
if (p.format && p.format.endsWith("%")) {
if (value && !value.endsWith("%"))
value += "%";
}
const result = value == undefined || value.length == 0 ? null : numbro.unformat(value, p.format);
setText(undefined);
if (result != p.value)
p.onChange(result);
if (p.htmlAttributes && p.htmlAttributes.onBlur)
p.htmlAttributes.onBlur(e);
}
private convertStringToNumber = (val: string) => {
if (!val) {
return undefined;
}
if (this.isSeparatorLastChar(val) || ((val.includes(',') || val.includes('.')) && val.slice(-1) === '0')) {
return val;
}
return numbro.unformat(val);
};
private restoreNumberWithLocale = (val?: string | number): number | undefined => {
if (!val) {
return undefined;
}
if (!isNaN(Number(val))) {
return numbro(val).value();
}
return numbro.unformat(String(val));
};