-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInput.tsx
More file actions
executable file
·96 lines (85 loc) · 2.37 KB
/
Input.tsx
File metadata and controls
executable file
·96 lines (85 loc) · 2.37 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable react/prop-types */
import React, { Ref, JSX } from "react";
import Label from "../Label/Label";
import Hint from "../Hint/Hint";
import ErrorMessage from "../ErrorMessage/ErrorMessage";
import { InputProps } from "./Input.types";
const defaultProps: Partial<InputProps> = {
type: "text",
};
const Input: React.FC<
InputProps & React.InputHTMLAttributes<HTMLInputElement>
> = React.forwardRef((props = defaultProps, ref: Ref<HTMLInputElement>) => {
const {
className,
"aria-describedby": describedBy,
errorMessage,
formGroup,
hint,
label,
name,
id,
prefix,
suffix,
...attributes
} = props;
let describedByValue: string = describedBy || "";
let hintComponent: JSX.Element | null = null;
let errorMessageComponent: JSX.Element | null = null;
if (hint) {
const hintId: string = `${id}-hint`;
describedByValue += ` ${hintId}`;
hintComponent = <Hint {...hint} id={hintId} />;
}
if (errorMessage) {
const errorId: string = id ? `${id}-error` : "";
describedByValue += ` ${errorId}`;
errorMessageComponent = <ErrorMessage {...errorMessage} id={errorId} />;
}
const input: JSX.Element = (
<input
ref={ref}
id={id}
className={`govuk-input ${className || ""} ${errorMessage ? " govuk-input--error" : ""}`}
name={name || id}
aria-describedby={describedByValue || undefined}
{...attributes}
/>
);
return (
<div
className={`govuk-form-group ${formGroup?.className || ""} ${errorMessage ? "govuk-form-group--error" : ""}`}
>
<Label {...label} htmlFor={id} />
{hintComponent}
{errorMessageComponent}
{prefix || suffix ? (
<div className="govuk-input__wrapper">
{prefix ? (
<div
aria-hidden="true"
{...{
...prefix,
className: `govuk-input__prefix ${prefix.className ? prefix.className : ""}`,
}}
/>
) : null}
{input}
{suffix ? (
<div
aria-hidden="true"
{...{
...suffix,
className: `govuk-input__suffix ${suffix.className ? suffix.className : ""}`,
}}
/>
) : null}
</div>
) : (
input
)}
</div>
);
});
Input.displayName = "Input";
export default Input;