Add helpetText props to TextField
[arvados-workbench2.git] / src / components / text-field / text-field.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 { WrappedFieldProps } from 'redux-form';
7 import { ArvadosTheme } from '~/common/custom-theme';
8 import {
9     TextField as MaterialTextField,
10     StyleRulesCallback,
11     WithStyles,
12     withStyles,
13     PropTypes
14 } from '@material-ui/core';
15 import RichTextEditor from 'react-rte';
16 import Margin = PropTypes.Margin;
17
18 type CssRules = 'textField';
19
20 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21     textField: {
22         marginBottom: theme.spacing.unit * 3
23     },
24 });
25
26 type TextFieldProps = WrappedFieldProps & WithStyles<CssRules>;
27
28 export const TextField = withStyles(styles)((props: TextFieldProps & {
29     label?: string, autoFocus?: boolean, required?: boolean, select?: boolean, disabled?: boolean, children: React.ReactNode, margin?: Margin, placeholder?: string,
30     helperText?: string,
31 }) =>
32     <MaterialTextField
33         helperText={(props.meta.touched && props.meta.error) || props.helperText}
34         className={props.classes.textField}
35         label={props.label}
36         disabled={props.disabled || props.meta.submitting}
37         error={props.meta.touched && !!props.meta.error}
38         autoComplete='off'
39         autoFocus={props.autoFocus}
40         fullWidth={true}
41         required={props.required}
42         select={props.select}
43         children={props.children}
44         margin={props.margin}
45         placeholder={props.placeholder}
46         {...props.input}
47     />);
48
49
50 interface RichEditorTextFieldData {
51     label?: string;
52 }
53
54 type RichEditorTextFieldProps = RichEditorTextFieldData & TextFieldProps;
55
56 export const RichEditorTextField = withStyles(styles)(
57     class RichEditorTextField extends React.Component<RichEditorTextFieldProps> {
58         state = {
59             value: RichTextEditor.createValueFromString(this.props.input.value, 'html')
60         };
61
62         onChange = (value: any) => {
63             this.setState({ value });
64             this.props.input.onChange(value.toString('html'));
65         }
66
67         render() {
68             return <RichTextEditor
69                 value={this.state.value}
70                 onChange={this.onChange}
71                 placeholder={this.props.label} />;
72         }
73     }
74 );
75
76 export const DateTextField = withStyles(styles)
77     ((props: TextFieldProps) =>
78         <MaterialTextField
79             type="date"
80             disabled={props.meta.submitting}
81             helperText={props.meta.error}
82             error={!!props.meta.error}
83             fullWidth={true}
84             InputLabelProps={{
85                 shrink: true
86             }}
87             name={props.input.name}
88             onChange={props.input.onChange}
89             value={props.input.value}
90         />
91     );