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

Provided signature and partial type compatibility with InertiaJS #65

Closed
wants to merge 3 commits into from
Closed

Conversation

juanparati
Copy link

I did some rework on the vue-inertia package in order to make it compatible with the original InertiaJS "useForm":

Changes

  • Precognition "useForm" will accept object type like InertiaJS:
import {useForm} from 'laravel-precognition-vue-inertia'
const form = useForm<MyInterface>('post', 'https://example.net', mydata);
// or  useForm('post', 'https://example.net', mydata as MyInterface);
  • Precognition "useForm" will return the original InertiaJS properties and methods patched with the Precognition properties and methods (I added the types).

  • Original interface properties are also exposed.

Advantages of this change

  • Original interface properties and Precognition properties and methods are exposed to the linter.
  • Better Typescript support.
  • Increased signature compatibility with InertiaJS.
  • It doesn't require any change in the Precognition core libraries or precognition-vue package.

Disadvantages of this change

  • It may require to update the return type in case that original InertiaJS "useForm" method change any of their return properties or method.

@juanparati
Copy link
Author

@driesvints: This is a better solution of #64 but also resolves #55.

@juanparati
Copy link
Author

Another alternative it's also to the extend the original InertiaForm:

Example:

type FormDataType = object;

interface PrecognitionFormProps<TForm> {
    validating: boolean,                                
    progress: Progress | null;
    reset(...fields: (keyof TForm)[]): this;
    clearErrors(...fields: (keyof TForm)[]): this;
    touched(name: string): boolean,
    touch(name: string | string[] | NamedInputEvent): this;
    submit(submitMethod: RequestMethod|Config, submitUrl?: string, submitOptions?: Partial<VisitOptions>): void;
    setErrors(errors: SimpleValidationErrors|ValidationErrors) : this;
    forgetError(name: string|NamedInputEvent): this;
    setError(field: keyof TForm, value: string): this;
    validate(name?: string|NamedInputEvent): this;
    setValidationTimeout(duration: number): this;
    validateFiles(): this;
    validator(): Validator;
    valid(name: string | NamedInputEvent | string[]): boolean,
    invalid(name: string): boolean,
}

type PrecognitionForm<TForm extends FormDataType> = TForm & PrecognitionFormProps<TForm> & InertiaForm<TForm>;

The advantage is that in case that InertiaJS implement new props it will be inhered, however the disadvantage is that it's not possible to hide non compatible methods and props.

@driesvints
Copy link
Member

Thanks @juanparati. Will let @timacdonald review this one.

@taylorotwell taylorotwell marked this pull request as draft January 26, 2024 12:43
@juanparati juanparati closed this by deleting the head repository May 27, 2024
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