0aeaeb85f663ccb3394965b4ff5dff21912e13ee
[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, children: React.ReactNode, margin?: Margin
30 }) =>
31     <MaterialTextField
32         helperText={props.meta.touched && props.meta.error}
33         className={props.classes.textField}
34         label={props.label}
35         disabled={props.meta.submitting}
36         error={props.meta.touched && !!props.meta.error}
37         autoComplete='off'
38         autoFocus={props.autoFocus}
39         fullWidth={true}
40         required={props.required}
41         select={props.select}
42         children={props.children}
43         margin={props.margin}
44         {...props.input}
45     />);
46
47
48 interface RichEditorTextFieldData {
49     label?: string;
50 }
51
52 type RichEditorTextFieldProps = RichEditorTextFieldData & TextFieldProps;
53
54 export const RichEditorTextField = withStyles(styles)(
55     class RichEditorTextField extends React.Component<RichEditorTextFieldProps> {
56         state = {
57             value: RichTextEditor.createValueFromString(this.props.input.value, 'html')
58         };
59
60         onChange = (value: any) => {
61             this.setState({ value });
62             this.props.input.onChange(value.toString('html'));
63         }
64
65         render() {
66             return <RichTextEditor
67                 value={this.state.value}
68                 onChange={this.onChange}
69                 placeholder={this.props.label} />;
70         }
71     }
72 );
73
74 export const DateTextField = withStyles(styles)
75     ((props: TextFieldProps) =>
76         <MaterialTextField
77             type="date"
78             disabled={props.meta.submitting}
79             helperText={props.meta.error}
80             error={!!props.meta.error}
81             fullWidth={true}
82             InputLabelProps={{
83                 shrink: true
84             }}
85             name={props.input.name}
86             onChange={props.input.onChange}
87             value={props.input.value}
88         />
89     );