-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dropdown.svelte
112 lines (101 loc) · 2.36 KB
/
Dropdown.svelte
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<script>
/**
* @type {{ value: string; text?: string; }[]} [options=[]]
*/
export let options = [];
/**
* @type {string} [selected]
*/
export let selected = undefined;
/**
* @type {string} [id=""]
*/
export let id = "uswds-" + Math.random().toString(36);
/**
* @type {string} [label="Dropdown label"]
*/
export let label = "Dropdown label";
/**
* @type {string} [name=""]
*/
export let name = "";
/**
* @type {boolean} [success=false]
*/
export let success = false;
/**
* @type {boolean} [error=false]
*/
export let error = false;
/**
* @type {string} [errorText="Helpful error message"]
*/
export let errorText = "Helpful error message";
/**
* @type {boolean} [readonly=false]
*/
export let readonly = false;
/**
* @type {boolean} [required=false]
*/
export let required = false;
/**
* @type {boolean} [disabled=false]
*/
export let disabled = false;
</script>
{#if error}
<div class="usa-form-group usa-form-group--error">
<label class="usa-label usa-label--error" for="{id}">
{label}
{#if !required}<span class="usa-hint">(optional)</span>{/if}
</label>
<span
class="usa-error-message"
id="error-{id}"
role="alert"
>{errorText}</span>
<!-- svelte-ignore a11y-no-onchange -->
<select
class="usa-select usa-input--error"
name="{name}"
disabled="{disabled}"
id="{id}"
readonly="{readonly}"
on:change
on:change="{({ target }) => {
selected = target.value;
}}"
>
{#each options as option}
<option value="{option.value}" selected="{selected === option.value}">
{option.text || option.value}
</option>
{/each}
</select>
</div>
{:else}
<label class="usa-label" for="{id}">
{label}
{#if !required}<span class="usa-hint">(optional)</span>{/if}
</label>
<!-- svelte-ignore a11y-no-onchange -->
<select
class="usa-select"
class:usa-input--success="{success}"
name="{name}"
disabled="{disabled}"
id="{id}"
readonly="{readonly}"
on:change
on:change="{({ target }) => {
selected = target.value;
}}"
>
{#each options as option}
<option value="{option.value}" selected="{selected === option.value}">
{option.text || option.value}
</option>
{/each}
</select>
{/if}