import * as _ from 'lodash'; import {action, computed, observable} from 'mobx'; import {observer} from 'mobx-react'; import * as React from 'react'; import {Icon, IconName} from '../Icon'; import {TextInputField} from '../TextInputField'; import styles from './NumericInputField.scss'; import classNames from 'classnames'; interface NumericInputFieldProps { value: number; onChange?: (newValue: number) => void; min?: number; max?: number; label?: string; placeholder?: string; disabled?: boolean; leftIcon?: IconName | JSX.Element; rightIcon?: IconName | JSX.Element; formatted?: boolean; withDecimals?: boolean; withControls?: boolean; error?: string | boolean; selectAllOnFocus?: boolean; onFocus?: () => void; onBlur?: () => void; inputRef?: (ref: HTMLInputElement) => void; className?: string; inputClassName?: string; } @observer export class NumericInputField extends React.Component { @observable private integerPart = ''; @observable private decimalPart = ''; @observable private isFocused = false; constructor(props: NumericInputFieldProps) { super(props); this.updateValueParts(props.value?.toFixed(2)); } componentDidUpdate(prevProps: NumericInputFieldProps) { const {value: newValue} = this.props; if (newValue !== this.fullPartsValue) { this.isFocused ? this.updateValueParts(newValue) : this.updateValueParts(newValue?.toFixed(2)); } } @computed private get fullPartsValue() { return parseFloat(this.integerPart + this.decimalPart); } @computed private get valueToDisplay() { const {value, formatted, withDecimals} = this.props; if (!_.isNumber(value)) { return ''; } else { const integerPartValue = parseInt(this.integerPart); let integerPartToDisplay; if (isNaN(integerPartValue)) { integerPartToDisplay = ''; } else { integerPartToDisplay = formatted ? integerPartValue.toLocaleString('en-US') : integerPartValue.toString(); } return withDecimals ? `${integerPartToDisplay}${this.decimalPart}` : integerPartToDisplay; } } @computed private get leftIcon() { const {disabled, leftIcon, rightIcon, withControls} = this.props; if (leftIcon) { return leftIcon; } if (withControls && !rightIcon) { return (
); } } @computed private get rightIcon() { const {disabled, leftIcon, rightIcon, withControls} = this.props; if (rightIcon) { return rightIcon; } if (withControls && !leftIcon) { return (
); } } @action private updateValueParts = (value: string | number = null) => { this.integerPart = this.getIntegerPart(value); this.decimalPart = this.getDecimalPart(value); } private getIntegerPart = (value: string | number) => { if (value === null) { return ''; } return value.toString().split('.')[0]; } private getDecimalPart = (value: string | number) => { if (value === null) { return ''; } const decimalPart = value.toString().split('.')[1]; return decimalPart !== undefined ? `.${decimalPart.slice(0, 2)}` : ''; } @action private handleChange = (newValue: string) => { const {onChange, withDecimals} = this.props; const {removeRedundantDots, updateValueParts, isValueInAllowedRange} = this; if (!onChange) { return; } if (newValue === '') { onChange(null); return; } const numericString = withDecimals ? removeRedundantDots(newValue).replace(/[^.\d]/g, '') : newValue.replace(/[^\d]/g, ''); updateValueParts(numericString); const numericValue = withDecimals ? _.floor(parseFloat(numericString), 2) : parseInt(numericString); if (!isNaN(numericValue) && isValueInAllowedRange(numericValue)) { onChange(numericValue); } } private removeRedundantDots = (value: string) => { if (value.includes('.')) { const valueParts = value.split('.'); return `${valueParts.shift()}.${valueParts.join('')}`; } else { return value; } } @action private handleFocus = () => { const {onFocus} = this.props; this.isFocused = true; onFocus?.(); } @action private handleBlur = () => { const {value, onBlur} = this.props; this.isFocused = false; this.updateValueParts(value?.toFixed(2)); onBlur?.(); } private isValueInAllowedRange = (value: number) => { const {min, max} = this.props; let valueIsInRange = true; if (_.isNumber(min) && value < min) { valueIsInRange = false; } if (_.isNumber(max) && value > max) { valueIsInRange = false; } return valueIsInRange; } private handleIncrement = () => { const {value, onChange} = this.props; if (!onChange) { return; } const newValue = value + 1; if (this.isValueInAllowedRange(newValue)) { onChange(newValue); } } private handleDecrement = () => { const {value, onChange} = this.props; if (!onChange) { return; } const newValue = value - 1; if (this.isValueInAllowedRange(newValue)) { onChange(newValue); } } public render() { const {label, placeholder, disabled, leftIcon, rightIcon, withControls, error, selectAllOnFocus, inputRef, className, inputClassName} = this.props; return ( ); } }