Add login in/out handling, fix async validation
[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, placeholder?: string
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         placeholder={props.placeholder}
45         {...props.input}
46     />);
47
48
49 interface RichEditorTextFieldData {
50     label?: string;
51 }
52
53 type RichEditorTextFieldProps = RichEditorTextFieldData & TextFieldProps;
54
55 export const RichEditorTextField = withStyles(styles)(
56     class RichEditorTextField extends React.Component<RichEditorTextFieldProps> {
57         state = {
58             value: RichTextEditor.createValueFromString(this.props.input.value, 'html')
59         };
60
61         onChange = (value: any) => {
62             this.setState({ value });
63             this.props.input.onChange(value.toString('html'));
64         }
65
66         render() {
67             return <RichTextEditor
68                 value={this.state.value}
69                 onChange={this.onChange}
70                 placeholder={this.props.label} />;
71         }
72     }
73 );
74
75 export const DateTextField = withStyles(styles)
76     ((props: TextFieldProps) =>
77         <MaterialTextField
78             type="date"
79             disabled={props.meta.submitting}
80             helperText={props.meta.error}
81             error={!!props.meta.error}
82             fullWidth={true}
83             InputLabelProps={{
84                 shrink: true
85             }}
86             name={props.input.name}
87             onChange={props.input.onChange}
88             value={props.input.value}
89         />
90     );