Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.6.x]: Data Getter Support in vue and vue-inertia Forms #117

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vue-inertia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Form, FormDataConvertible } from './types'

export { client }

export const useForm = <Data extends Record<string, FormDataConvertible>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data, config: ValidationConfig = {}): Form<Data> => {
export const useForm = <Data extends Record<string, FormDataConvertible>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data | (() => Data), config: ValidationConfig = {}): Form<Data> => {
/**
* The Inertia form.
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/vue-inertia/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,22 @@ it('can set individual errors', function () {

expect(form.errors.name).toBe('The name is required.')
})

it('allows getter as data inputs', function () {
let dynamicEmail = '[email protected]'

function getData() {
return {
email: dynamicEmail,
}
}

const form = useForm('post', '/register', getData)

expect(form.email).toBe('[email protected]')

dynamicEmail = '[email protected]'
form.reset()

expect(form.email).toBe('[email protected]')
})
6 changes: 3 additions & 3 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { cloneDeep, get, set } from 'lodash-es'

export { client }

export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data, config: ValidationConfig = {}): Data & Form<Data> => {
export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data | (() => Data), config: ValidationConfig = {}): Data & Form<Data> => {
/**
* The original data.
*/
const originalData = cloneDeep(inputs)
const originalData = typeof inputs === 'function' ? cloneDeep(inputs()) : cloneDeep(inputs)

/**
* The original input names.
Expand Down Expand Up @@ -143,7 +143,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
return form
},
reset(...names) {
const original = cloneDeep(originalData)
const original = typeof inputs === 'function' ? cloneDeep(inputs()) : cloneDeep(originalData)

if (names.length === 0) {
// @ts-expect-error
Expand Down
Loading