-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormInput.tsx
More file actions
75 lines (67 loc) · 1.62 KB
/
Copy pathFormInput.tsx
File metadata and controls
75 lines (67 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React from 'react';
import { v4 as uuid } from 'uuid';
import { FormGroup, TextField, FormHelperText } from '@material-ui/core';
// Form input UI. Use with hooks/useInput.tsx.
interface IFormInput {
type: string;
name: string;
label: string;
value: string;
helperText?: string;
placeholder?: string;
className?: string;
autoFocus?: boolean;
error: string;
onChange: (e: any) => void;
noWhiteSpace?: boolean;
autoComplete?: string;
};
const FormInput: React.FC<IFormInput> = props => {
const {
type,
name,
label,
value,
helperText,
placeholder,
autoFocus,
error,
onChange,
autoComplete,
} = props;
const hasError = error.length > 0; // hasError if errors array is not empty.
return (
<FormGroup>
<TextField
type={type}
name={name}
label={label}
value={value}
autoFocus={autoFocus}
error={hasError}
placeholder={placeholder}
variant='outlined'
fullWidth
onChange={e => onChange(e)}
autoComplete={autoComplete}
// Fix. Prevent white space in email input.
// https://github.com/facebook/react/issues/6368
onKeyDown={type === 'email' ? e => onChange(e) : () => { }}
/>
{/* Initial helper text. If exist and has no errors. */}
{
helperText && !hasError &&
<FormHelperText>
{helperText}
</FormHelperText>
}
{/* Error if exist */}
{hasError &&
<FormHelperText error key={uuid()}>
{error}
</FormHelperText>
}
</FormGroup>
);
};
export default FormInput;