Skip to content

Commit 9358771

Browse files
yo-gototimacdonald
andauthored
[0.3.x] Fix react-inertia setError bug (#22)
* Fix react-inertia setError bug * Do not reference "this" * Improve reference --------- Co-authored-by: Tim MacDonald <[email protected]>
1 parent 4595eed commit 9358771

File tree

6 files changed

+54
-50
lines changed

6 files changed

+54
-50
lines changed

packages/core/src/client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export const client: Client = {
3131
patch: (url, data = {}, config = {}) => request(mergeConfig('patch', url, data, config)),
3232
put: (url, data = {}, config = {}) => request(mergeConfig('put', url, data, config)),
3333
delete: (url, data = {}, config = {}) => request(mergeConfig('delete', url, data, config)),
34-
use(client) {
35-
axiosClient = client
34+
use(axios) {
35+
axiosClient = axios
3636

37-
return this
37+
return client
3838
},
3939
axios() {
4040
return axiosClient
@@ -44,12 +44,12 @@ export const client: Client = {
4444
? () => null
4545
: callback
4646

47-
return this
47+
return client
4848
},
4949
determineSuccessUsing(callback) {
5050
successResolver = callback
5151

52-
return this
52+
return client
5353
},
5454
}
5555

packages/core/src/validator.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
111111
validator.cancel()
112112

113113
validator = createValidator()
114-
115-
return this
116114
}
117115

118116
/**
@@ -233,12 +231,12 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
233231
/**
234232
* The form validator instance.
235233
*/
236-
return {
234+
const form: TValidator = {
237235
touched: () => touched,
238236
validate(input, value) {
239237
validate(input, value)
240238

241-
return this
239+
return form
242240
},
243241
validating: () => validating,
244242
valid,
@@ -247,12 +245,12 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
247245
setErrors(value) {
248246
setErrors(value)
249247

250-
return this
248+
return form
251249
},
252250
forgetError(name) {
253251
forgetError(name)
254252

255-
return this
253+
return form
256254
},
257255
reset(...names) {
258256
if (names.length === 0) {
@@ -271,24 +269,26 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
271269
setTouched(newTouched)
272270
}
273271

274-
return this
272+
return form
275273
},
276274
setTimeout(value) {
277275
setDebounceTimeout(value)
278276

279-
return this
277+
return form
280278
},
281279
on(event, callback) {
282280
listeners[event].push(callback)
283281

284-
return this
282+
return form
285283
},
286284
validateFiles() {
287285
validateFiles = true
288286

289-
return this
287+
return form
290288
},
291289
}
290+
291+
return form
292292
}
293293

294294
/**

packages/react-inertia/src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
6262
/**
6363
* Patch the form.
6464
*/
65-
return Object.assign(inertiaForm, {
65+
const form = Object.assign(inertiaForm, {
6666
validating: precognitiveForm.validating,
6767
touched: precognitiveForm.touched,
6868
valid: precognitiveForm.valid,
@@ -72,7 +72,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
7272

7373
precognitiveForm.setData(key, value)
7474

75-
return this
75+
return form
7676
},
7777
clearErrors(...names: string[]) {
7878
inertiaClearErrors(...names)
@@ -83,7 +83,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
8383
names.forEach(precognitiveForm.forgetError)
8484
}
8585

86-
return this
86+
return form
8787
},
8888
reset(...names: string[]) {
8989
inertiaReset(...names)
@@ -94,39 +94,39 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
9494
// @ts-expect-error
9595
precognitiveForm.setErrors(errors)
9696

97-
return this
97+
return form
9898
},
9999
setError(key: any, value?: any) {
100-
this.setErrors({
100+
form.setErrors({
101101
...inertiaForm.errors,
102102
...typeof value === 'undefined'
103103
? key
104104
: { [key]: value },
105105
})
106106

107-
return this
107+
return form
108108
},
109109
forgetError(name: string|NamedInputEvent) {
110110
precognitiveForm.forgetError(name)
111111

112-
return this
112+
return form
113113
},
114114
validate(name: string|NamedInputEvent) {
115115
precognitiveForm.setData(inertiaForm.data)
116116

117117
precognitiveForm.validate(name)
118118

119-
return this
119+
return form
120120
},
121121
setValidationTimeout(duration: number) {
122122
precognitiveForm.setValidationTimeout(duration)
123123

124-
return this
124+
return form
125125
},
126126
validateFiles() {
127127
precognitiveForm.validateFiles()
128128

129-
return this
129+
return form
130130
},
131131
submit(submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void {
132132
const isPatchedCall = typeof submitMethod !== 'string'
@@ -156,4 +156,6 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
156156
},
157157
validator: precognitiveForm.validator,
158158
})
159+
160+
return form
159161
}

packages/react/src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
118118
/**
119119
* The form instance.
120120
*/
121-
return {
121+
const form: Form<Data> = {
122122
data,
123123
setData(key, value) {
124124
if (typeof key === 'object') {
@@ -133,7 +133,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
133133
setData(payload.current)
134134
}
135135

136-
return this
136+
return form
137137
},
138138
touched(name) {
139139
return touched.includes(name)
@@ -144,7 +144,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
144144

145145
validator.current!.validate(name, get(payload.current, name))
146146

147-
return this
147+
return form
148148
},
149149
validating,
150150
valid(name) {
@@ -159,13 +159,13 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
159159
// @ts-expect-error
160160
validator.current!.setErrors(errors)
161161

162-
return this
162+
return form
163163
},
164164
forgetError(name) {
165165
// @ts-expect-error
166166
validator.current!.forgetError(name)
167167

168-
return this
168+
return form
169169
},
170170
reset(...names) {
171171
const original = cloneDeep(originalData.current)!
@@ -183,12 +183,12 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
183183
// @ts-expect-error
184184
validator.current!.reset(...names)
185185

186-
return this
186+
return form
187187
},
188188
setValidationTimeout(duration) {
189189
validator.current!.setTimeout(duration)
190190

191-
return this
191+
return form
192192
},
193193
processing,
194194
async submit(config = {}) {
@@ -197,10 +197,12 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
197197
validateFiles() {
198198
validator.current!.validateFiles()
199199

200-
return this
200+
return form
201201
},
202202
validator() {
203203
return validator.current!
204204
},
205205
}
206+
207+
return form
206208
}

packages/vue-inertia/src/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
5050
/**
5151
* Patch the form.
5252
*/
53-
return Object.assign(inertiaForm, {
53+
const form = Object.assign(inertiaForm, {
5454
validating: precognitiveForm.validating,
5555
touched: precognitiveForm.touched,
5656
valid: precognitiveForm.valid,
@@ -64,7 +64,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
6464
names.forEach(precognitiveForm.forgetError)
6565
}
6666

67-
return this
67+
return form
6868
},
6969
reset(...names: string[]) {
7070
inertiaReset(...names)
@@ -75,39 +75,39 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
7575
// @ts-expect-error
7676
precognitiveForm.setErrors(errors)
7777

78-
return this
78+
return form
7979
},
8080
forgetError(name: string|NamedInputEvent) {
8181
precognitiveForm.forgetError(name)
8282

83-
return this
83+
return form
8484
},
8585
setError(key: any, value?: any) {
86-
this.setErrors({
86+
form.setErrors({
8787
...inertiaForm.errors,
8888
...typeof value === 'undefined'
8989
? key
9090
: { [key]: value },
9191
})
9292

93-
return this
93+
return form
9494
},
9595
validate(name: string|NamedInputEvent) {
9696
precognitiveForm.setData(inertiaForm.data())
9797

9898
precognitiveForm.validate(name)
9999

100-
return this
100+
return form
101101
},
102102
setValidationTimeout(duration: number) {
103103
precognitiveForm.setValidationTimeout(duration)
104104

105-
return this
105+
return form
106106
},
107107
validateFiles() {
108108
precognitiveForm.validateFiles()
109109

110-
return this
110+
return form
111111
},
112112
submit(submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void {
113113
const isPatchedCall = typeof submitMethod !== 'string'
@@ -137,4 +137,6 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
137137
},
138138
validator: precognitiveForm.validator,
139139
})
140+
141+
return form
140142
}

packages/vue/src/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
7979
/**
8080
* Create a new form instance.
8181
*/
82-
const createForm = (): Data&Form<Data> => ({
82+
let form: Data&Form<Data> = {
8383
...cloneDeep(originalData),
8484
data() {
8585
const data = cloneDeep(toRaw(form))
@@ -91,7 +91,8 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
9191
},
9292
setData(data: Record<string, unknown>) {
9393
Object.keys(data).forEach(input => {
94-
this[input] = data[input]
94+
// @ts-expect-error
95+
form[input] = data[input]
9596
})
9697

9798
return form
@@ -104,7 +105,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
104105
// @ts-expect-error
105106
name = resolveName(name)
106107

107-
validator.validate(name, get(this.data(), name))
108+
validator.validate(name, get(form.data(), name))
108109

109110
return form
110111
},
@@ -162,12 +163,9 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
162163
validator() {
163164
return validator
164165
},
165-
})
166+
}
166167

167-
/**
168-
* The form instance.
169-
*/
170-
const form = reactive(createForm()) as Data&Form<Data>
168+
form = reactive(form) as Data&Form<Data>
171169

172170
return form
173171
}

0 commit comments

Comments
 (0)