Skip to content

Commit 6311b96

Browse files
committed
wip
1 parent d2f7d14 commit 6311b96

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

packages/react-inertia/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist
2+
/node_modules

packages/react-inertia/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

packages/react-inertia/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Laravel Precognition
2+
3+
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/tests/badge.svg" alt="Test Status"></a>
4+
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/build/badge.svg" alt="Build Status"></a>
5+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/dt/laravel-precognition" alt="Total Downloads"></a>
6+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/v/laravel-precognition" alt="Latest Stable Version"></a>
7+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/l/laravel-precognition" alt="License"></a>
8+
9+
## Introduction
10+
11+
Laravel Precognition allows you to anticipate the outcome of a future HTTP request. One of the primary use cases of Precognition is the ability to provide "live" validation in your frontend application.
12+
13+
## Official Documentation
14+
15+
Documentation for Laravel Precognition can be found on the [Laravel website](https://laravel.com/docs/precognition).
16+
17+
## Contributing
18+
19+
Thank you for considering contributing to Laravel Precognition! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
20+
21+
## Code of Conduct
22+
23+
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
24+
25+
## Security Vulnerabilities
26+
27+
Please review [our security policy](https://github.com/laravel/precognition/security/policy) on how to report security vulnerabilities.
28+
29+
## License
30+
31+
Laravel Precognition is open-sourced software licensed under the [MIT license](LICENSE.md).
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "laravel-precognition-react-inertia",
3+
"version": "0.1.0",
4+
"description": "Laravel Precognition (React w/ Inertia).",
5+
"keywords": [
6+
"laravel",
7+
"precognition",
8+
"react",
9+
"inertia"
10+
],
11+
"homepage": "https://github.com/laravel/precognition",
12+
"type": "module",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/laravel/precognition"
16+
},
17+
"license": "MIT",
18+
"author": "Laravel",
19+
"main": "dist/index.js",
20+
"files": [
21+
"/dist"
22+
],
23+
"scripts": {
24+
"build": "rm -rf dist && tsc",
25+
"prepublishOnly": "npm run build"
26+
},
27+
"engines": {
28+
"node": ">=14"
29+
},
30+
"peerDependencies": {
31+
"@inertiajs/react": "^1.0.0"
32+
},
33+
"dependencies": {
34+
"laravel-precognition-react": "0.1.0"
35+
},
36+
"devDependencies": {
37+
"typescript": "^5.0.0"
38+
}
39+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { Config, NamedInputEvent, RequestMethod, SimpleValidationErrors, toSimpleValidationErrors, ValidationConfig, ValidationErrors } from 'laravel-precognition'
2+
import { useForm as useVueForm } from 'laravel-precognition-vue'
3+
import { useForm as useInertiaForm } from '@inertiajs/vue3'
4+
5+
export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod, url: string, inputs: Data, config: ValidationConfig = {}): any => {
6+
/**
7+
* The Inertia form.
8+
*/
9+
const inertiaForm = useInertiaForm(inputs)
10+
11+
/**
12+
* The Vue form.
13+
*/
14+
const vueForm = useVueForm(method, url, inputs, config)
15+
16+
/**
17+
* Setup event listeners.
18+
*/
19+
vueForm.validator().on('errorsChanged', () => {
20+
inertiaClearErrors()
21+
22+
inertiaSetError(
23+
// @ts-expect-error
24+
toSimpleValidationErrors(vueForm.validator().errors())
25+
)
26+
})
27+
28+
/**
29+
* The Inertia submit function.
30+
*/
31+
const inertiaSubmit = inertiaForm.submit.bind(inertiaForm)
32+
33+
/**
34+
* The Inertia reset function.
35+
*/
36+
const inertiaReset = inertiaForm.reset.bind(inertiaForm)
37+
38+
/**
39+
* The Inertia clear errors function.
40+
*/
41+
const inertiaClearErrors = inertiaForm.clearErrors.bind(inertiaForm)
42+
43+
/**
44+
* The Inertia set error function.
45+
*/
46+
const inertiaSetError = inertiaForm.setError.bind(inertiaForm)
47+
48+
/**
49+
* Patch the form.
50+
*/
51+
return Object.assign(inertiaForm, {
52+
processing: vueForm.processing,
53+
validating: vueForm.validating,
54+
touched: vueForm.touched,
55+
valid: vueForm.valid,
56+
invalid: vueForm.invalid,
57+
clearErrors() {
58+
inertiaClearErrors()
59+
60+
vueForm.setErrors({})
61+
62+
return this
63+
},
64+
reset(...names: string[]) {
65+
inertiaReset(...names)
66+
67+
vueForm.reset(...names)
68+
},
69+
setErrors(errors: SimpleValidationErrors|ValidationErrors) {
70+
// @ts-expect-error
71+
vueForm.setErrors(errors)
72+
73+
return this
74+
},
75+
setError(key: any, value?: any) {
76+
this.setErrors({
77+
...inertiaForm.errors,
78+
...typeof value === 'undefined'
79+
? key
80+
: { [key]: value }
81+
})
82+
83+
return this
84+
},
85+
validate(name: string|NamedInputEvent) {
86+
vueForm.setData(inertiaForm.data())
87+
88+
vueForm.validate(name)
89+
90+
return this
91+
},
92+
setValidationTimeout(duration: number) {
93+
vueForm.setValidationTimeout(duration)
94+
95+
return this
96+
},
97+
submit(submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void {
98+
const isPatchedCall = typeof submitMethod !== 'string'
99+
100+
const userOptions = isPatchedCall
101+
? submitMethod
102+
: submitOptions
103+
104+
const options = {
105+
...userOptions,
106+
onError: (errors: SimpleValidationErrors): any => {
107+
vueForm.validator().setErrors(errors)
108+
109+
if (userOptions.onError) {
110+
return userOptions.onError(errors)
111+
}
112+
},
113+
}
114+
115+
inertiaSubmit(
116+
isPatchedCall ? method : submitMethod,
117+
// @ts-expect-error
118+
(isPatchedCall ? url : submitUrl),
119+
options
120+
)
121+
},
122+
})
123+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist",
4+
"target": "ES2020",
5+
"module": "ES2020",
6+
"moduleResolution": "node",
7+
"resolveJsonModule": true,
8+
"strict": true,
9+
"declaration": true,
10+
"esModuleInterop": true,
11+
"types": [
12+
"vue"
13+
]
14+
},
15+
"include": [
16+
"./src/index.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)