-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathinput.svelte
More file actions
145 lines (132 loc) · 3.37 KB
/
input.svelte
File metadata and controls
145 lines (132 loc) · 3.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<script lang="ts">
import { notEmpty, type Validator, validUrl } from '@slowreader/core'
import { onMount } from 'svelte'
import type { HTMLInputAttributes } from 'svelte/elements'
import { on } from 'svelte/events'
import Error from './error.svelte'
import Label from './label.svelte'
let {
disabled,
error,
errorId,
font = 'normal',
input = $bindable(),
label,
labelless = false,
onchange,
onescape,
oninput,
type = 'text',
validate = () => undefined,
value = $bindable(''),
...props
}: {
disabled?: boolean
error?: string
errorId?: string
font?: 'mono' | 'normal'
input?: HTMLInputElement
label: string
labelless?: boolean
onchange?: (v: string, valid: boolean) => void
onescape?: () => void
oninput?: (v: string, valid: boolean) => void
validate?: Validator | Validator[]
} & Omit<HTMLInputAttributes, 'onchange' | 'oninput'> = $props()
let id = $props.id()
let isValid = $derived(!error || !errorId)
let validators = $derived.by(() => {
let list = Array.isArray(validate) ? validate : [validate]
if (props.required) list.unshift(notEmpty)
if (type === 'url') list.unshift(validUrl)
return list
})
function runValidators(val: string): string | undefined {
for (let validator of validators) {
let result = validator(val)
if (result) return result
}
return undefined
}
onMount(() => {
if (input) {
return on(input, 'validate', () => {
error = runValidators(value)
})
}
})
</script>
<div class="input">
{#if !labelless}
<Label {id}>{label}</Label>
{/if}
<input
bind:this={input}
{id}
class="input_field"
class:is-mono={font === 'mono'}
aria-disabled={disabled}
aria-errormessage={errorId || (error ? `${id}-error` : null)}
aria-invalid={error || errorId ? true : null}
aria-label={labelless ? label : null}
data-invalid={!!runValidators(value)}
onblur={e => {
value = e.currentTarget.value
if (value !== '') error = runValidators(value)
}}
onchange={e => {
value = e.currentTarget.value
error = runValidators(value)
if (onchange) onchange(value, isValid)
}}
oninput={e => {
if (oninput) oninput(e.currentTarget.value, isValid)
}}
onkeyup={e => {
if (e.key === 'Escape') onescape?.()
if (error) error = runValidators(e.currentTarget.value)
}}
readonly={disabled}
{type}
{value}
{...props}
/>
{#if error}
<div class="input_error">
<Error id={`${id}-error`}>{error}</Error>
</div>
{/if}
</div>
<style>
:global {
.input {
flex-shrink: 1;
width: stretch;
}
.input_field {
box-sizing: border-box;
width: stretch;
height: var(--control-height);
padding: 0 var(--control-padding);
background: --tune-background(--field);
border: none;
border-radius: var(--base-radius);
box-shadow: var(--field-shadow);
&[aria-invalid] {
background: --tune-background(--secondary, --dangerous);
box-shadow:
0 0 0 1px var(--dangerous-text-color),
var(--field-shadow);
}
&.is-mono {
font: var(--mono-font);
}
}
.input_field::placeholder {
color: var(--secondary-text-color);
}
.input_error {
margin-top: 0.125rem;
}
}
</style>