Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 52 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import React from "react"; |
||
2 | import { FieldProps } from "formik"; |
||
3 | import Input, { InputProps } from "../Input"; |
||
4 | |||
5 | interface TextInputProps |
||
6 | extends Exclude<InputProps, "name" | "value" | "onChange" | "onBlur"> { |
||
7 | /** Formik field prop of the shape { name, value, onChange, onBlur } */ |
||
8 | field: FieldProps["field"]; |
||
9 | /** Formik form prop of the shape { errors } */ |
||
10 | form: FieldProps["form"]; |
||
11 | } |
||
12 | |||
13 | const TextInput: React.FunctionComponent<TextInputProps> = ({ |
||
14 | id, |
||
15 | label, |
||
16 | required, |
||
17 | placeholder, |
||
18 | grid, |
||
19 | minLength, |
||
20 | maxLength, |
||
21 | field: { name, value, onChange, onBlur }, |
||
22 | form: { errors, touched }, |
||
23 | ...props |
||
24 | }): React.ReactElement => { |
||
25 | const specificError = errors ? errors[name] : null; |
||
26 | const errorText = specificError ? specificError.toString() : undefined; |
||
27 | const invalid = touched[name] && errors[name] ? true : null; |
||
28 | // Workaround for new TS error https://github.com/microsoft/TypeScript/issues/37559 |
||
29 | const { name: passedName, onChange: passedChange, ...otherProps } = props; |
||
30 | |||
31 | return ( |
||
32 | <Input |
||
33 | id={id} |
||
34 | label={label} |
||
35 | placeholder={placeholder} |
||
36 | required={required} |
||
37 | name={name} |
||
38 | value={value} |
||
39 | grid={grid} |
||
40 | minLength={minLength} |
||
41 | maxLength={maxLength} |
||
42 | onChange={onChange} |
||
43 | onBlur={onBlur} |
||
44 | errorText={errorText} |
||
45 | invalid={invalid} |
||
46 | {...otherProps} |
||
47 | /> |
||
48 | ); |
||
49 | }; |
||
50 | |||
51 | export default TextInput; |
||
52 |