1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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;
13 export const FloatInput = ({ input }: FloatInputProps) =>
16 label={getInputLabel(input)}
17 component={DecimalInput}
19 format={value => isNaN(value) ? '' : JSON.stringify(value)}
20 validate={[isNumber]} />;
23 class DecimalInput extends React.Component<WrappedFieldProps & { label?: string }> {
25 endsWithDecimalSeparator: false,
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);
39 value: this.props.input.value + (this.state.endsWithDecimalSeparator ? '.' : ''),
40 onChange: this.handleChange,
43 return <TextField {...props} />;