@@ -14,9 +14,60 @@ type State = {
14
14
15
15
const states = new WeakMap < AutoCheckElement , State > ( )
16
16
17
- export class AutoCheckCompleteEvent extends Event {
17
+ class AutoCheckEvent extends Event {
18
+ constructor ( public readonly phase : string ) {
19
+ super ( `auto-check-${ phase } ` , { bubbles : true } )
20
+ }
21
+
22
+ // Backwards compatibiltiy with `CustomEvent`
23
+ get detail ( ) {
24
+ return this
25
+ }
26
+ }
27
+
28
+ class AutoCheckValidationEvent extends AutoCheckEvent {
29
+ constructor ( public readonly phase : string , public message = '' ) {
30
+ super ( phase )
31
+ }
32
+
33
+ setValidity ( message : string ) {
34
+ this . message = message
35
+ }
36
+ }
37
+
38
+ // eslint-disable-next-line custom-elements/no-exports-with-element
39
+ export class AutoCheckCompleteEvent extends AutoCheckEvent {
40
+ constructor ( ) {
41
+ super ( 'complete' )
42
+ }
43
+ }
44
+
45
+ // eslint-disable-next-line custom-elements/no-exports-with-element
46
+ export class AutoCheckSuccessEvent extends AutoCheckEvent {
47
+ constructor ( public readonly response : Response ) {
48
+ super ( 'success' )
49
+ }
50
+ }
51
+
52
+ // eslint-disable-next-line custom-elements/no-exports-with-element
53
+ export class AutoCheckStartEvent extends AutoCheckValidationEvent {
18
54
constructor ( ) {
19
- super ( 'auto-check-complete' , { bubbles : true } )
55
+ super ( 'start' , 'Verifying…' )
56
+ }
57
+ }
58
+
59
+ // eslint-disable-next-line custom-elements/no-exports-with-element
60
+ export class AutoCheckErrorEvent extends AutoCheckValidationEvent {
61
+ constructor ( public readonly response : Response ) {
62
+ // eslint-disable-next-line i18n-text/no-en
63
+ super ( 'error' , 'Validation failed' )
64
+ }
65
+ }
66
+
67
+ // eslint-disable-next-line custom-elements/no-exports-with-element
68
+ export class AutoCheckSendEvent extends AutoCheckEvent {
69
+ constructor ( public readonly body : FormData ) {
70
+ super ( 'send' )
20
71
}
21
72
}
22
73
@@ -128,17 +179,10 @@ function setLoadingState(event: Event) {
128
179
return
129
180
}
130
181
131
- let message = 'Verifying…'
132
- const setValidity = ( text : string ) => ( message = text )
133
- input . dispatchEvent (
134
- new CustomEvent ( 'auto-check-start' , {
135
- bubbles : true ,
136
- detail : { setValidity} ,
137
- } ) ,
138
- )
139
-
182
+ const startEvent = new AutoCheckStartEvent ( )
183
+ input . dispatchEvent ( startEvent )
140
184
if ( autoCheckElement . required ) {
141
- input . setCustomValidity ( message )
185
+ input . setCustomValidity ( startEvent . message )
142
186
}
143
187
}
144
188
@@ -199,12 +243,7 @@ async function check(autoCheckElement: AutoCheckElement) {
199
243
body . append ( csrfField , csrf )
200
244
body . append ( 'value' , input . value )
201
245
202
- input . dispatchEvent (
203
- new CustomEvent ( 'auto-check-send' , {
204
- bubbles : true ,
205
- detail : { body} ,
206
- } ) ,
207
- )
246
+ input . dispatchEvent ( new AutoCheckSendEvent ( body ) )
208
247
209
248
if ( state . controller ) {
210
249
state . controller . abort ( )
@@ -222,9 +261,16 @@ async function check(autoCheckElement: AutoCheckElement) {
222
261
body,
223
262
} )
224
263
if ( response . ok ) {
225
- processSuccess ( response , input , autoCheckElement . required )
264
+ if ( autoCheckElement . required ) {
265
+ input . setCustomValidity ( '' )
266
+ }
267
+ input . dispatchEvent ( new AutoCheckSuccessEvent ( response . clone ( ) ) )
226
268
} else {
227
- processFailure ( response , input , autoCheckElement . required )
269
+ const event = new AutoCheckErrorEvent ( response . clone ( ) )
270
+ input . dispatchEvent ( event )
271
+ if ( autoCheckElement . required ) {
272
+ input . setCustomValidity ( event . message )
273
+ }
228
274
}
229
275
state . controller = null
230
276
input . dispatchEvent ( new AutoCheckCompleteEvent ( ) )
@@ -236,37 +282,4 @@ async function check(autoCheckElement: AutoCheckElement) {
236
282
}
237
283
}
238
284
239
- function processSuccess ( response : Response , input : HTMLInputElement , required : boolean ) {
240
- if ( required ) {
241
- input . setCustomValidity ( '' )
242
- }
243
- input . dispatchEvent (
244
- new CustomEvent ( 'auto-check-success' , {
245
- bubbles : true ,
246
- detail : {
247
- response : response . clone ( ) ,
248
- } ,
249
- } ) ,
250
- )
251
- }
252
-
253
- function processFailure ( response : Response , input : HTMLInputElement , required : boolean ) {
254
- // eslint-disable-next-line i18n-text/no-en
255
- let message = 'Validation failed'
256
- const setValidity = ( text : string ) => ( message = text )
257
- input . dispatchEvent (
258
- new CustomEvent ( 'auto-check-error' , {
259
- bubbles : true ,
260
- detail : {
261
- response : response . clone ( ) ,
262
- setValidity,
263
- } ,
264
- } ) ,
265
- )
266
-
267
- if ( required ) {
268
- input . setCustomValidity ( message )
269
- }
270
- }
271
-
272
285
export default AutoCheckElement
0 commit comments