noph-ui 0.13.1 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,24 +17,29 @@
|
|
|
17
17
|
variant = 'filled',
|
|
18
18
|
placeholder = ' ',
|
|
19
19
|
element = $bindable(),
|
|
20
|
+
inputElement = $bindable(),
|
|
20
21
|
...attributes
|
|
21
22
|
}: TextFieldProps = $props()
|
|
22
23
|
|
|
23
24
|
let errorRaw: boolean = $state(error)
|
|
24
25
|
let errorTextRaw: string = $state(errorText)
|
|
26
|
+
let focusOnInvalid = $state(true)
|
|
27
|
+
let checkValidity = $state(false)
|
|
25
28
|
$effect(() => {
|
|
26
29
|
errorRaw = error
|
|
27
30
|
errorTextRaw = errorText
|
|
28
31
|
})
|
|
29
|
-
let textElement: HTMLInputElement | HTMLTextAreaElement | undefined = $state()
|
|
30
32
|
|
|
31
33
|
$effect(() => {
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
+
if (inputElement) {
|
|
35
|
+
inputElement.form?.addEventListener('reset', () => {
|
|
34
36
|
errorRaw = error
|
|
35
37
|
value = ''
|
|
36
38
|
})
|
|
37
|
-
|
|
39
|
+
inputElement.addEventListener('input', () => {
|
|
40
|
+
checkValidity = true
|
|
41
|
+
})
|
|
42
|
+
inputElement.addEventListener('invalid', (event) => {
|
|
38
43
|
event.preventDefault()
|
|
39
44
|
const { currentTarget } = event as Event & {
|
|
40
45
|
currentTarget: HTMLInputElement | HTMLTextAreaElement
|
|
@@ -43,18 +48,22 @@
|
|
|
43
48
|
if (errorText === '') {
|
|
44
49
|
errorTextRaw = currentTarget.validationMessage
|
|
45
50
|
}
|
|
46
|
-
if (isFirstInvalidControlInForm(currentTarget.form, currentTarget)) {
|
|
51
|
+
if (focusOnInvalid && isFirstInvalidControlInForm(currentTarget.form, currentTarget)) {
|
|
47
52
|
currentTarget.focus()
|
|
48
53
|
}
|
|
49
54
|
})
|
|
50
55
|
|
|
51
|
-
|
|
56
|
+
inputElement.addEventListener('blur', (event) => {
|
|
52
57
|
const { currentTarget } = event as Event & {
|
|
53
58
|
currentTarget: HTMLInputElement | HTMLTextAreaElement
|
|
54
59
|
}
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
if (checkValidity) {
|
|
61
|
+
focusOnInvalid = false
|
|
62
|
+
if (currentTarget.checkValidity()) {
|
|
63
|
+
errorRaw = error
|
|
64
|
+
errorTextRaw = errorText
|
|
65
|
+
}
|
|
66
|
+
focusOnInvalid = true
|
|
58
67
|
}
|
|
59
68
|
})
|
|
60
69
|
}
|
|
@@ -122,7 +131,7 @@
|
|
|
122
131
|
aria-label={label}
|
|
123
132
|
{...attributes}
|
|
124
133
|
bind:value
|
|
125
|
-
bind:this={
|
|
134
|
+
bind:this={inputElement}
|
|
126
135
|
class="input"
|
|
127
136
|
{placeholder}
|
|
128
137
|
rows={attributes.rows || 2}
|
|
@@ -138,7 +147,7 @@
|
|
|
138
147
|
aria-label={label}
|
|
139
148
|
{...attributes}
|
|
140
149
|
bind:value
|
|
141
|
-
bind:this={
|
|
150
|
+
bind:this={inputElement}
|
|
142
151
|
class="input"
|
|
143
152
|
{placeholder}
|
|
144
153
|
aria-invalid={errorRaw}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { TextFieldProps } from './types.ts';
|
|
2
|
-
declare const TextField: import("svelte").Component<TextFieldProps, {}, "element" | "value">;
|
|
2
|
+
declare const TextField: import("svelte").Component<TextFieldProps, {}, "element" | "value" | "inputElement">;
|
|
3
3
|
type TextField = ReturnType<typeof TextField>;
|
|
4
4
|
export default TextField;
|
|
@@ -12,6 +12,7 @@ interface FieldProps {
|
|
|
12
12
|
end?: Snippet;
|
|
13
13
|
noAsterisk?: boolean;
|
|
14
14
|
element?: HTMLSpanElement;
|
|
15
|
+
inputElement?: HTMLInputElement | HTMLTextAreaElement;
|
|
15
16
|
}
|
|
16
17
|
export interface InputFieldProps extends HTMLInputAttributes, FieldProps {
|
|
17
18
|
type?: 'text' | 'password' | 'email' | 'number' | 'search' | 'tel' | 'url' | 'datetime-local';
|