Merge branch 'master' into 14254-triggering-vie-details-from-more-options-does-not...
[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 { TextField as MaterialTextField, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
9 import RichTextEditor from 'react-rte';
10
11 type CssRules = 'textField';
12
13 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
14     textField: {
15         marginBottom: theme.spacing.unit * 3
16     },
17 });
18
19 type TextFieldProps = WrappedFieldProps & WithStyles<CssRules>;
20
21 export const TextField = withStyles(styles)((props: TextFieldProps & { label?: string, autoFocus?: boolean, required?: boolean }) =>
22     <MaterialTextField
23         helperText={props.meta.touched && props.meta.error}
24         className={props.classes.textField}
25         label={props.label}
26         disabled={props.meta.submitting}
27         error={props.meta.touched && !!props.meta.error}
28         autoComplete='off'
29         autoFocus={props.autoFocus}
30         fullWidth={true}
31         required={props.required}
32         {...props.input}
33     />);
34
35
36 interface RichEditorTextFieldData {
37     label?: string;
38 }
39
40 type RichEditorTextFieldProps = RichEditorTextFieldData & TextFieldProps;
41
42 export const RichEditorTextField = withStyles(styles)(
43     class RichEditorTextField extends React.Component<RichEditorTextFieldProps> {
44         state = {
45             value: RichTextEditor.createValueFromString(this.props.input.value, 'html')
46         };
47
48         onChange = (value: any) => {
49             this.setState({ value });
50             this.props.input.onChange(value.toString('html'));
51         }
52
53         render() {
54             return <RichTextEditor 
55                 value={this.state.value}
56                 onChange={this.onChange}
57                 placeholder={this.props.label} />;
58         }
59     }
60 );
61
62 export const DateTextField = withStyles(styles)
63     ((props: TextFieldProps) => 
64         <MaterialTextField
65             type="date"
66             disabled={props.meta.submitting}
67             helperText={props.meta.error}
68             error={!!props.meta.error}
69             fullWidth={true}
70             InputLabelProps={{
71                 shrink: true
72             }}
73             name={props.input.name}
74             onChange={props.input.onChange}
75             value={props.input.value}
76         />    
77     );