blob: 0847f3efcf812e7398040e73f1086a589bbb48b1 (
plain) (
blame)
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
113
114
115
116
117
118
119
120
121
|
<script lang="ts">
import { random_string } from "$lib/helpers";
import { ExclamationCircle } from "./icons";
export let label: string | undefined = undefined;
export let type: string = "text";
export let id: string | undefined = "input__" + random_string(4);
export let name: string | undefined = undefined;
export let placeholder: string | undefined = undefined;
export let helpText: string | undefined = undefined;
export let errorText: string | undefined = undefined;
export let disabled = false;
export let hideLabel = false;
export let cornerHint: string | undefined = undefined;
export let icon: any = undefined;
export let addon: string | undefined = undefined;
export let value: string | undefined;
$: ariaErrorDescribedBy = id + "__" + "error";
$: attributes = {
"aria-describedby": errorText ? ariaErrorDescribedBy : null,
"aria-invalid": errorText ? "true" : null,
disabled: disabled || null,
} as any;
$: hasBling = icon || addon || errorText;
const defaultColorClass =
"border-gray-300 focus:border-teal-500 focus:ring-teal-500";
let colorClass = defaultColorClass;
$: if (errorText) {
colorClass =
"placeholder-red-300 focus:border-red-500 focus:outline-none focus:ring-red-500 text-red-900 pr-10 border-red-300";
} else {
colorClass = defaultColorClass;
}
function typeAction(node: HTMLInputElement) {
node.type = type;
}
</script>
<div>
{#if label && !cornerHint && !hideLabel}
<label
for={id}
class={hideLabel
? "sr-only"
: "block text-sm font-medium text-gray-700"}
>
{label}
</label>
{:else if cornerHint && !hideLabel}
<div class="flex justify-between">
{#if label}
<label
for={id}
class={hideLabel
? "sr-only"
: "block text-sm font-medium text-gray-700"}
>
{label}
</label>
{/if}
<span class="text-sm text-gray-500">
{cornerHint}
</span>
</div>
{/if}
<div
class="mt-1 {hasBling ? 'relative rounded-md' : ''} {addon
? 'flex'
: ''}"
>
{#if icon}
<div
class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3"
>
<svelte:component
this={icon}
class={errorText ? "text-red-500" : "text-gray-400"}
/>
</div>
{:else if addon}
<div
class="inline-flex items-center rounded-l-md border border-r-0 border-gray-300 bg-gray-50 px-3 text-gray-500 sm:text-sm"
>
<span class="text-gray-500 sm:text-sm">{addon}</span>
</div>
{/if}
<input
use:typeAction
{name}
{id}
{...attributes}
bind:value
class="block w-full rounded-md shadow-sm sm:text-sm {colorClass} {disabled
? 'disabled:cursor-not-allowed disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500'
: ''}
{addon ? 'min-w-0 flex-1 rounded-none rounded-r-md' : ''} {icon
? 'pl-10'
: ''}"
{placeholder}
/>
{#if errorText}
<div
class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3"
>
<ExclamationCircle class="text-red-500" />
</div>
{/if}
</div>
{#if helpText && !errorText}
<p class="mt-2 text-sm text-gray-500">
{helpText}
</p>
{/if}
{#if errorText}
<p class="mt-2 text-sm text-red-600" id={ariaErrorDescribedBy}>
{errorText}
</p>
{/if}
</div>
|