Create DecimalInput for storing decimal separator info
[arvados-workbench2.git] / src / views / run-process-panel / inputs / float-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 { getInputLabel, FloatCommandInputParameter } from '~/models/workflow';
7 import { Field, WrappedFieldProps } from 'redux-form';
8 import { TextField } from '~/components/text-field/text-field';
9 import { isNumber } from '~/validators/is-number';
10 export interface FloatInputProps {
11     input: FloatCommandInputParameter;
12 }
13 export const FloatInput = ({ input }: FloatInputProps) =>
14     <Field
15         name={input.id}
16         label={getInputLabel(input)}
17         component={DecimalInput}
18         parse={parseFloat}
19         format={value => isNaN(value) ? '' : JSON.stringify(value)}
20         validate={[isNumber]} />;
21
22
23 class DecimalInput extends React.Component<WrappedFieldProps & { label?: string }> {
24     state = {
25         endsWithDecimalSeparator: false,
26     };
27
28     handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
29         const [base, fraction] = event.target.value.split('.');
30         this.setState({ endsWithDecimalSeparator: fraction === '' });
31         this.props.input.onChange(event);
32     }
33
34     render() {
35         const props = {
36             ...this.props,
37             input: {
38                 ...this.props.input,
39                 value: this.props.input.value + (this.state.endsWithDecimalSeparator ? '.' : ''),
40                 onChange: this.handleChange,
41             },
42         };
43         return <TextField {...props} />;
44     }
45 }