Skip to content

Conversation

@SantosVilanculos
Copy link

@SantosVilanculos SantosVilanculos commented Oct 24, 2025

This PR implements a wrapper component for controlled inputs for use in conjunction with the useForm hook.

As a result, this component will provide APIs that make any application form code less verbose by isolating form complexity (tracking dirty, touched, error and value states) away from the input component, making your form components reusable, simpler, and easy to integrate with third-party controlled component libraries.

Usage example:

import { Controller, useForm } from 'laravel-precognition-react-inertia';

export default function Form() {
    const form = useForm('post', '/users', {
        name: '',
        email: ''
    });

    return (
        <form
            onSubmit={event => {
                event.preventDefault();
                event.stopPropagation();

                form.submit();
            }}
            inert={form.processing}
        >
            <Controller form={form} name="name">
                {field => (
                    <div>
                        <label htmlFor={field.name}>Name</label>
                        <input
                            id={field.name}
                            name={field.name}
                            value={field.state.value}
                            onChange={event =>
                                field.setValue(event.target.value)
                            }
                            onBlur={() => field.validate()}
                        />
                        {field.state.invalid && <div>{field.state.error}</div>}
                    </div>
                )}
            </Controller>

            <Controller form={form} name="email">
                {field => (
                    <div>
                        <label htmlFor={field.name}>Email</label>
                        <input
                            id={field.name}
                            name={field.name}
                            value={field.state.value}
                            onChange={event =>
                                field.setValue(event.target.value)
                            }
                            onBlur={() => field.validate()}
                        />
                        {field.state.invalid && <div>{field.state.error}</div>}
                    </div>
                )}
            </Controller>

            <button disabled={form.processing}>Create User</button>
        </form>
    );
}

@github-actions
Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@SantosVilanculos SantosVilanculos marked this pull request as ready for review October 24, 2025 14:58
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants