laravel-precognition-react
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -1,3 +0,4 @@ | ||
import { RequestMethod, ValidationConfig } from 'laravel-precognition'; | ||
import { client, RequestMethod, ValidationConfig } from 'laravel-precognition'; | ||
import { Form } from './types'; | ||
export declare const useForm: <Data extends Record<string, unknown>>(method: RequestMethod, url: string, input: Data, config?: ValidationConfig) => Form<Data>; | ||
export { client }; | ||
export declare const useForm: <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), input: Data, config?: ValidationConfig) => Form<Data>; |
@@ -1,2 +0,2 @@ | ||
import { resolveName, client, createValidator, toSimpleValidationErrors } from 'laravel-precognition'; | ||
import { resolveName, client, createValidator, toSimpleValidationErrors, resolveUrl, resolveMethod } from 'laravel-precognition'; | ||
import cloneDeep from 'lodash.clonedeep'; | ||
@@ -6,5 +6,4 @@ import get from 'lodash.get'; | ||
import { useRef, useState } from 'react'; | ||
export { client }; | ||
export const useForm = (method, url, input, config = {}) => { | ||
// @ts-expect-error | ||
method = method.toLowerCase(); | ||
/** | ||
@@ -61,13 +60,14 @@ * The original data. | ||
if (validator.current === null) { | ||
validator.current = createValidator(client => client[method](url, payload.current, config), input) | ||
validator.current = createValidator(client => client[resolveMethod(method)](resolveUrl(url), payload.current, config), input) | ||
.on('validatingChanged', () => { | ||
setValidating(validator.current.validating()); | ||
}) | ||
.on('validatedChanged', () => { | ||
setValid(validator.current.valid()); | ||
}) | ||
.on('touchedChanged', () => { | ||
setTouched(validator.current.touched()); | ||
setValid(validator.current.valid()); | ||
}) | ||
.on('errorsChanged', () => { | ||
setHasErrors(validator.current.hasErrors()); | ||
setValid(validator.current.valid()); | ||
// @ts-expect-error | ||
@@ -101,3 +101,3 @@ setErrors(toSimpleValidationErrors(validator.current.errors())); | ||
*/ | ||
return { | ||
const form = { | ||
data, | ||
@@ -114,3 +114,3 @@ setData(key, value) { | ||
} | ||
return this; | ||
return form; | ||
}, | ||
@@ -120,7 +120,16 @@ touched(name) { | ||
}, | ||
touch(name) { | ||
validator.current.touch(name); | ||
return form; | ||
}, | ||
validate(name) { | ||
// @ts-expect-error | ||
name = resolveName(name); | ||
validator.current.validate(name, get(payload.current, name)); | ||
return this; | ||
if (typeof name === 'undefined') { | ||
validator.current.validate(); | ||
} | ||
else { | ||
// @ts-expect-error | ||
name = resolveName(name); | ||
validator.current.validate(name, get(payload.current, name)); | ||
} | ||
return form; | ||
}, | ||
@@ -139,3 +148,3 @@ validating, | ||
validator.current.setErrors(errors); | ||
return this; | ||
return form; | ||
}, | ||
@@ -145,3 +154,3 @@ forgetError(name) { | ||
validator.current.forgetError(name); | ||
return this; | ||
return form; | ||
}, | ||
@@ -160,15 +169,15 @@ reset(...names) { | ||
validator.current.reset(...names); | ||
return this; | ||
return form; | ||
}, | ||
setValidationTimeout(duration) { | ||
validator.current.setTimeout(duration); | ||
return this; | ||
return form; | ||
}, | ||
processing, | ||
async submit(config = {}) { | ||
return client[method](url, payload.current, resolveSubmitConfig(config)); | ||
return client[resolveMethod(method)](resolveUrl(url), payload.current, resolveSubmitConfig(config)); | ||
}, | ||
validateFiles() { | ||
validator.current.validateFiles(); | ||
return this; | ||
return form; | ||
}, | ||
@@ -179,2 +188,3 @@ validator() { | ||
}; | ||
return form; | ||
}; |
@@ -6,2 +6,3 @@ import { Config, NamedInputEvent, Validator } from 'laravel-precognition'; | ||
touched(name: keyof Data): boolean; | ||
touch(name: string | NamedInputEvent | Array<string>): Form<Data>; | ||
data: Data; | ||
@@ -13,3 +14,3 @@ setData(key: Data | keyof Data, value?: unknown): Form<Data>; | ||
invalid(name: keyof Data): boolean; | ||
validate(name: keyof Data | NamedInputEvent): Form<Data>; | ||
validate(name?: keyof Data | NamedInputEvent): Form<Data>; | ||
setErrors(errors: Partial<Record<keyof Data, string | string[]>>): Form<Data>; | ||
@@ -16,0 +17,0 @@ forgetError(string: keyof Data | NamedInputEvent): Form<Data>; |
{ | ||
"name": "laravel-precognition-react", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Laravel Precognition (React).", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
"dependencies": { | ||
"laravel-precognition": "0.3.2", | ||
"laravel-precognition": "0.4.0", | ||
"lodash.clonedeep": "^4.5.0", | ||
@@ -37,0 +37,0 @@ "lodash.get": "^4.4.2", |
10957
207
+ Addedlaravel-precognition@0.4.0(transitive)
- Removedlaravel-precognition@0.3.2(transitive)
Updatedlaravel-precognition@0.4.0