Skip to content

Commit fdc9a98

Browse files
authored
Issue #442: Add SelectField form component (#449)
1 parent 214c13a commit fdc9a98

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
export type SelectOption = {
3+
/** Option value */
4+
value: string;
5+
/** Visible label */
6+
label: string;
7+
/** Disabled option */
8+
disabled?: boolean;
9+
};
10+
11+
export interface Props {
12+
/** Group label */
13+
legend: string;
14+
/** Select id */
15+
id: string;
16+
/** Name attribute */
17+
name: string;
18+
/** Select options */
19+
options: SelectOption[];
20+
/** Selected value */
21+
selected?: string;
22+
/** Optional placeholder option */
23+
placeholder?: string;
24+
/** Optional help text */
25+
helpText?: string;
26+
/** Optional error text */
27+
errorText?: string;
28+
/** Required field */
29+
required?: boolean;
30+
/** Disabled select */
31+
disabled?: boolean;
32+
/** Optional class */
33+
className?: string;
34+
}
35+
36+
const {
37+
legend,
38+
id,
39+
name,
40+
options,
41+
selected,
42+
placeholder,
43+
helpText,
44+
errorText,
45+
required,
46+
disabled,
47+
className,
48+
} = Astro.props;
49+
50+
const helpId = helpText ? `${id}-help` : undefined;
51+
const errorId = errorText ? `${id}-error` : undefined;
52+
const describedBy = [errorId, helpId].filter(Boolean).join(" ") || undefined;
53+
---
54+
55+
<fieldset class={className} aria-describedby={describedBy}>
56+
<legend>{legend}</legend>
57+
58+
{errorText && (
59+
<p id={errorId} role="alert">
60+
{errorText}
61+
</p>
62+
)}
63+
64+
{helpText && <p id={helpId}>{helpText}</p>}
65+
66+
<label for={id}>
67+
{required && <span aria-hidden="true">*</span>}
68+
</label>
69+
70+
<select
71+
id={id}
72+
name={name}
73+
required={required}
74+
disabled={disabled}
75+
aria-describedby={describedBy}
76+
>
77+
{placeholder && (
78+
<option value="" disabled selected={!selected}>
79+
{placeholder}
80+
</option>
81+
)}
82+
83+
{options.map((opt) => (
84+
<option
85+
value={opt.value}
86+
selected={opt.value === selected}
87+
disabled={opt.disabled}
88+
>
89+
{

0 commit comments

Comments
 (0)