-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelect.tsx
More file actions
74 lines (67 loc) · 1.85 KB
/
Select.tsx
File metadata and controls
74 lines (67 loc) · 1.85 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
import React, { SelectHTMLAttributes } from "react";
import { SelectProps } from "./Select.types";
import { ErrorMessage } from "../ErrorMessage";
import { Hint } from "../Hint";
import { Label } from "../Label";
const Select: React.FC<
SelectProps & SelectHTMLAttributes<HTMLSelectElement>
> = (props) => {
const {
className,
"aria-describedby": describedBy,
errorMessage,
formGroup,
hint,
id,
items,
label,
...attributes
} = props;
let describedByValue: string = describedBy || "";
let hintComponent: React.ReactNode;
let errorMessageComponent: React.ReactNode;
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 options: React.ReactNode = items
? items
.filter((item) => item)
.map((option, index) => {
const { reactListKey, children, ...optionAttributes } = option;
return (
<option {...optionAttributes} key={reactListKey || index}>
{children}
</option>
);
})
: null;
return (
<div
className={`govuk-form-group${
errorMessage ? " govuk-form-group--error" : ""
} ${formGroup?.className || ""}`}
>
<Label {...label} htmlFor={id} />
{hintComponent}
{errorMessageComponent}
<select
className={`govuk-select ${className || ""}${
errorMessage ? " govuk-select--error" : ""
}`}
id={id}
aria-describedby={describedByValue || undefined}
{...attributes}
>
{options}
</select>
</div>
);
};
export default Select;