0527efa35547993d19fcdb45e9b6523360fb1288
[arvados-workbench2.git] / src / components / int-input / int-input.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Input } from '@material-ui/core';
7 import { InputProps } from '@material-ui/core/Input';
8
9 export class IntInput extends React.Component<InputProps> {
10     handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
11         const { onChange = () => { return; } } = this.props;
12         const parsedValue = parseInt(event.target.value, 10);
13         event.target.value = parsedValue.toString();
14         onChange(event);
15     }
16
17     render() {
18         const parsedValue = parseInt(typeof this.props.value === 'string' ? this.props.value : '', 10);
19         const value = isNaN(parsedValue) ? '' : parsedValue.toString();
20         const props = {
21             ...this.props,
22             value,
23             onChange: this.handleChange,
24         };
25         return <Input {...props} />;
26     }
27 }