@@ -14,12 +14,26 @@ export function deserialize(result) {
14
14
return parsed ;
15
15
}
16
16
17
+ /**
18
+ * @param {string } old_name
19
+ * @param {string } new_name
20
+ * @param {string } call_location
21
+ * @returns void
22
+ */
23
+ function warn_on_access ( old_name , new_name , call_location ) {
24
+ if ( ! DEV ) return ;
25
+ // TODO 2.0: Remove this code
26
+ console . warn (
27
+ `\`${ old_name } \` has been deprecated in favor of \`${ new_name } \`. \`${ old_name } \` will be removed in a future version. (Called from ${ call_location } )`
28
+ ) ;
29
+ }
30
+
17
31
/** @type {import('$app/forms').enhance } */
18
- export function enhance ( form , submit = ( ) => { } ) {
32
+ export function enhance ( form_element , submit = ( ) => { } ) {
19
33
if (
20
34
DEV &&
21
- /** @type {HTMLFormElement } */ ( HTMLFormElement . prototype . cloneNode . call ( form ) ) . method !==
22
- 'post'
35
+ /** @type {HTMLFormElement } */ ( HTMLFormElement . prototype . cloneNode . call ( form_element ) )
36
+ . method !== 'post'
23
37
) {
24
38
throw new Error ( 'use:enhance can only be used on <form> fields with method="POST"' ) ;
25
39
}
@@ -35,7 +49,7 @@ export function enhance(form, submit = () => {}) {
35
49
if ( result . type === 'success' ) {
36
50
if ( reset !== false ) {
37
51
// We call reset from the prototype to avoid DOM clobbering
38
- HTMLFormElement . prototype . reset . call ( form ) ;
52
+ HTMLFormElement . prototype . reset . call ( form_element ) ;
39
53
}
40
54
await invalidateAll ( ) ;
41
55
}
@@ -60,28 +74,38 @@ export function enhance(form, submit = () => {}) {
60
74
// We do cloneNode for avoid DOM clobbering - https://github.com/sveltejs/kit/issues/7593
61
75
event . submitter ?. hasAttribute ( 'formaction' )
62
76
? /** @type {HTMLButtonElement | HTMLInputElement } */ ( event . submitter ) . formAction
63
- : /** @type {HTMLFormElement } */ ( HTMLFormElement . prototype . cloneNode . call ( form ) ) . action
77
+ : /** @type {HTMLFormElement } */ ( HTMLFormElement . prototype . cloneNode . call ( form_element ) )
78
+ . action
64
79
) ;
65
80
66
- const data = new FormData ( form ) ;
81
+ const form_data = new FormData ( form_element ) ;
67
82
68
83
const submitter_name = event . submitter ?. getAttribute ( 'name' ) ;
69
84
if ( submitter_name ) {
70
- data . append ( submitter_name , event . submitter ?. getAttribute ( 'value' ) ?? '' ) ;
85
+ form_data . append ( submitter_name , event . submitter ?. getAttribute ( 'value' ) ?? '' ) ;
71
86
}
72
87
73
88
const controller = new AbortController ( ) ;
74
89
75
90
let cancelled = false ;
76
91
const cancel = ( ) => ( cancelled = true ) ;
77
92
93
+ // TODO 2.0: Remove `data` and `form`
78
94
const callback =
79
95
( await submit ( {
80
96
action,
81
97
cancel,
82
98
controller,
83
- data,
84
- form,
99
+ get data ( ) {
100
+ warn_on_access ( 'data' , 'formData' , 'use:enhance submit function' ) ;
101
+ return form_data ;
102
+ } ,
103
+ formData : form_data ,
104
+ get form ( ) {
105
+ warn_on_access ( 'form' , 'formElement' , 'use:enhance submit function' ) ;
106
+ return form_element ;
107
+ } ,
108
+ formElement : form_element ,
85
109
submitter : event . submitter
86
110
} ) ) ?? fallback_callback ;
87
111
if ( cancelled ) return ;
@@ -97,7 +121,7 @@ export function enhance(form, submit = () => {}) {
97
121
'x-sveltekit-action' : 'true'
98
122
} ,
99
123
cache : 'no-store' ,
100
- body : data ,
124
+ body : form_data ,
101
125
signal : controller . signal
102
126
} ) ;
103
127
@@ -110,21 +134,29 @@ export function enhance(form, submit = () => {}) {
110
134
111
135
callback ( {
112
136
action,
113
- data,
114
- form,
137
+ get data ( ) {
138
+ warn_on_access ( 'data' , 'formData' , 'callback returned from use:enhance submit function' ) ;
139
+ return form_data ;
140
+ } ,
141
+ formData : form_data ,
142
+ get form ( ) {
143
+ warn_on_access ( 'form' , 'formElement' , 'callback returned from use:enhance submit function' ) ;
144
+ return form_element ;
145
+ } ,
146
+ formElement : form_element ,
115
147
update : ( opts ) => fallback_callback ( { action, result, reset : opts ?. reset } ) ,
116
148
// @ts -expect-error generic constraints stuff we don't care about
117
149
result
118
150
} ) ;
119
151
}
120
152
121
153
// @ts -expect-error
122
- HTMLFormElement . prototype . addEventListener . call ( form , 'submit' , handle_submit ) ;
154
+ HTMLFormElement . prototype . addEventListener . call ( form_element , 'submit' , handle_submit ) ;
123
155
124
156
return {
125
157
destroy ( ) {
126
158
// @ts -expect-error
127
- HTMLFormElement . prototype . removeEventListener . call ( form , 'submit' , handle_submit ) ;
159
+ HTMLFormElement . prototype . removeEventListener . call ( form_element , 'submit' , handle_submit ) ;
128
160
}
129
161
} ;
130
162
}
0 commit comments