import * as ArrowIcon from 'material-design-icons/hardware/svg/production/ic_keyboard_arrow_down_24px.svg'; // Libs import * as React from 'react'; // Misc import InputGroup from '../inputGroup/InputGroup'; import { BaseInputProps, BaseInputState, BaseInput, } from '../base/input/BaseInput'; import { withFormContext } from '../form/withFormContext'; import { withThemeContext } from '../themeProvider/withThemeContext'; import classNames from 'classnames'; export interface NumberInputProps extends BaseInputProps { value?: never; onChange?: never; onNumberChange: (num: number) => void; number: number; min?: number; max?: number; unit?: string; } export interface NumberInputState extends BaseInputState { currentText: string; } export class NumberInputRaw extends BaseInput< NumberInputProps, NumberInputState, HTMLInputElement > { public static defaultProps = (Object.assign({}, BaseInput.defaultProps, { type: 'time', placeholder: '', }) as unknown) as Partial; constructor(props: NumberInputProps) { super(props); this.removeUnitClick = this.removeUnitClick.bind(this); this.addUnitClick = this.addUnitClick.bind(this); this.onUnitChanged = this.onUnitChanged.bind(this); } public render() { const { unit } = this.props; return (
{unit}
{this.renderDefaultValidation()} {this.props.label && ( )}
); } private commitState() { if (this.state.currentText) { var num = Number(this.state.currentText); if (!isNaN(num)) { this.handleLimits(num); } this.setState({ currentText: undefined }); } } private onBlur = () => { this.commitState(); this.handleBlur(); }; private removeUnitClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleLimits((this.props.number || 0) - 1); }; private onUnitChanged = (e: React.ChangeEvent) => { this.setState({ currentText: e.target.value }); }; private onKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { this.commitState(); } }; private addUnitClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleLimits((this.props.number || 0) + 1); }; private handleLimits(num: number) { if (this.props.min !== undefined) { if (num - this.props.min <= 0) { this.props.onNumberChange(this.props.min); return; } } if (this.props.max !== undefined) { if (this.props.max - num <= 0) { this.props.onNumberChange(this.props.max); return; } } this.props.onNumberChange(num); } } export const NumberInput = withThemeContext< NumberInputProps, InstanceType >(withFormContext(NumberInputRaw), 'numberInput'); export default NumberInput;