Restric order and filters arguments of favorite list
[arvados-workbench2.git] / src / utils / dialog-validator.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 { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
7
8 type ValidatorProps = {
9   value: string,
10   onChange: (isValid: boolean | string) => void;
11   render: (hasError: boolean) => React.ReactElement<any>;
12   isRequired: boolean;
13 };
14
15 interface ValidatorState {
16   isPatternValid: boolean;
17   isLengthValid: boolean;
18 }
19
20 const nameRegEx = /^[a-zA-Z0-9-_ ]+$/;
21 const maxInputLength = 60;
22
23 class Validator extends React.Component<ValidatorProps & WithStyles<CssRules>> {
24   state: ValidatorState = {
25     isPatternValid: true,
26     isLengthValid: true
27   };
28
29   componentWillReceiveProps(nextProps: ValidatorProps) {
30     const { value } = nextProps;
31
32     if (this.props.value !== value) {
33       this.setState({
34         isPatternValid: value.match(nameRegEx),
35         isLengthValid: value.length < maxInputLength
36       }, () => this.onChange());
37     }
38   }
39
40   onChange() {
41     const { value, onChange, isRequired } = this.props;
42     const { isPatternValid, isLengthValid } = this.state;
43     const isValid = value && isPatternValid && isLengthValid && (isRequired || (!isRequired && value.length > 0));
44
45     onChange(isValid);
46   }
47
48   render() {
49     const { classes, isRequired, value } = this.props;
50     const { isPatternValid, isLengthValid } = this.state;
51
52     return (
53       <span>
54         {this.props.render(!(isPatternValid && isLengthValid) && (isRequired || (!isRequired && value.length > 0)))}
55         {!isPatternValid && (isRequired || (!isRequired && value.length > 0)) ? <span className={classes.formInputError}>This field allow only alphanumeric characters, dashes, spaces and underscores.<br /></span> : null}
56         {!isLengthValid ? <span className={classes.formInputError}>This field should have max 60 characters.</span> : null}
57       </span>
58     );
59   }
60 }
61
62 type CssRules = "formInputError";
63
64 const styles: StyleRulesCallback<CssRules> = theme => ({
65   formInputError: {
66     color: "#ff0000",
67     marginLeft: "5px",
68     fontSize: "11px",
69   }
70 });
71
72 export default withStyles(styles)(Validator);