-
Notifications
You must be signed in to change notification settings - Fork 472
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
Add initialization callback to form helper in Vue adapters #1516
Conversation
init()
methodexport default function useForm<TForm>(...args): InertiaForm<TForm> { | ||
const rememberKey = typeof args[0] === 'string' ? args[0] : null | ||
const data = (typeof args[0] === 'string' ? args[1] : args[0]) || {} | ||
const restored = rememberKey ? (router.restore(rememberKey) as { data: any; errors: any }) : null | ||
let defaults = cloneDeep(data) | ||
let defaults = typeof data === 'object' ? cloneDeep(data) : cloneDeep(data()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useForm()
without any argument now throws an error because it tries to call data which is undefined, but it was a valid call before (in fact it's used widely in laravel)
It should be like this
typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
let cancelToken = null | ||
let recentlySuccessfulTimeoutId = null | ||
let transform = (data) => data | ||
|
||
let form = reactive({ | ||
...(restored ? restored.data : data), | ||
...(restored ? restored.data : cloneDeep(defaults)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is a breaking change: previously, it was possible to pass in a reactive object like this:
const product = reactive({ name: '' });
const form = useForm({ product });
This way, product.name
and form.product.name
would always be in sync.
However, this no longer works, as data
is now deep-cloned twice before spreading into the form object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reinink I recently ran into this breaking change while trying to keep a reactive object in sync with the form object. Has there been a proper graceful fix or we may have to find alternative approaches?
Right now there is no easy way to reinitialize a form using the form helper without providing all the data a second time. This creates unnecessary boilerplate in situations where a simple
form.reset()
isn't enough because you want to update the values based on props that may have changed. For example:Before (option 1):
Before (option 2):
This PR solves this problem by adding an new optional initialization callback to the form helper. This function is automatically called when resetting the form, ensuring that the latest component state (props) is used.