Skip to content

Commit 423d9f7

Browse files
[0.7.x] Added ability to check form's touched state via touched (#130)
* 🌟 feat(packages/vue): Added isTouched state to the reactive form * Unify the API --------- Co-authored-by: Tim MacDonald <[email protected]>
1 parent d2ab49a commit 423d9f7

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

packages/alpine/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ export default function (Alpine: TAlpine) {
8888
}), {}) as Data
8989
},
9090
touched(name) {
91-
return state.touched.includes(name)
91+
if (typeof name === 'string') {
92+
return state.touched.includes(name)
93+
} else {
94+
return state.touched.length > 0
95+
}
9296
},
9397
touch(name) {
9498
validator.touch(name)

packages/alpine/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Config, NamedInputEvent, SimpleValidationErrors, ValidationConfig, Vali
33
export interface Form<Data extends Record<string, unknown>> {
44
processing: boolean,
55
validating: boolean,
6-
touched(name: string): boolean,
6+
touched(name?: string): boolean,
77
touch(name: string | NamedInputEvent | Array<string>): Data & Form<Data>,
88
data(): Data,
99
errors: Record<string, string>,

packages/react/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
136136
return form
137137
},
138138
touched(name) {
139-
return touched.includes(name)
139+
if (typeof name === 'string') {
140+
return touched.includes(name)
141+
} else {
142+
return touched.length > 0
143+
}
140144
},
141145
touch(name) {
142146
validator.current!.touch(name)

packages/react/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Config, NamedInputEvent, ValidationConfig, Validator } from 'laravel-pr
33
export interface Form<Data extends Record<string, unknown>> {
44
processing: boolean,
55
validating: boolean,
6-
touched(name: keyof Data): boolean,
6+
touched(name?: keyof Data): boolean,
77
touch(name: string | NamedInputEvent | Array<string>): Form<Data>,
88
data: Data,
99
setData(key: Data | keyof Data, value?: unknown): Form<Data>,

packages/vue-inertia/tests/index.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,32 @@ it('allows getter as data inputs', function () {
129129
form.reset()
130130

131131
expect(form.email).toBe('[email protected]')
132-
})
132+
})
133+
134+
it('can check that specific fields have been touched', () => {
135+
const form = useForm('post', '/register', {
136+
name: '',
137+
email: '',
138+
})
139+
140+
expect(form.touched('name')).toBe(false)
141+
expect(form.touched('email')).toBe(false)
142+
143+
form.touch('name')
144+
145+
expect(form.touched('name')).toBe(true)
146+
expect(form.touched('email')).toBe(false)
147+
})
148+
149+
it('can check it any fields have been touched', () => {
150+
const form = useForm('post', '/register', {
151+
name: '',
152+
email: '',
153+
})
154+
155+
expect(form.touched()).toBe(false)
156+
157+
form.touch('name')
158+
159+
expect(form.touched()).toBe(true)
160+
})

packages/vue/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
9595
return form
9696
},
9797
touched(name) {
98-
// @ts-expect-error
99-
return touched.value.includes(name)
98+
if (typeof name === 'string') {
99+
// @ts-expect-error
100+
return touched.value.includes(name)
101+
} else {
102+
return touched.value.length > 0
103+
}
100104
},
101105
touch(name) {
102106
validator.touch(name)

packages/vue/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ValidationConfig, Config, NamedInputEvent, Validator } from 'laravel-pr
33
export interface Form<Data extends Record<string, unknown>> {
44
processing: boolean,
55
validating: boolean,
6-
touched(name: keyof Data): boolean,
6+
touched(name?: keyof Data): boolean,
77
touch(name: string | NamedInputEvent | Array<string>): Data & Form<Data>,
88
data(): Data,
99
setData(data: Record<string, unknown>): Data & Form<Data>,

0 commit comments

Comments
 (0)