From 6e5ce051b98843054b1ffbe6a493a8e070f88136 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Sat, 11 Jan 2025 00:14:04 -0800 Subject: [PATCH] chore: migrate Lit adapter to new generics --- .../lit-form/src/tanstack-form-controller.ts | 286 +++++++++++++++++- packages/lit-form/tests/simple.ts | 10 +- 2 files changed, 275 insertions(+), 21 deletions(-) diff --git a/packages/lit-form/src/tanstack-form-controller.ts b/packages/lit-form/src/tanstack-form-controller.ts index f248007b2..ac05907a2 100644 --- a/packages/lit-form/src/tanstack-form-controller.ts +++ b/packages/lit-form/src/tanstack-form-controller.ts @@ -22,13 +22,43 @@ type renderCallback< | Validator | undefined = undefined, TData extends DeepValue = DeepValue, + TOnMountReturn = undefined, + TOnChangeReturn = undefined, + TOnChangeAsyncReturn = undefined, + TOnBlurReturn = undefined, + TOnBlurAsyncReturn = undefined, + TOnSubmitReturn = undefined, + TOnSubmitAsyncReturn = undefined, + TFormOnMountReturn = undefined, + TFormOnChangeReturn = undefined, + TFormOnChangeAsyncReturn = undefined, + TFormOnBlurReturn = undefined, + TFormOnBlurAsyncReturn = undefined, + TFormOnSubmitReturn = undefined, + TFormOnSubmitAsyncReturn = undefined, + TFormOnServerReturn = undefined, > = ( fieldOptions: FieldApi< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn >, ) => unknown @@ -42,21 +72,69 @@ type fieldDirectiveType< | Validator | undefined = undefined, TData extends DeepValue = DeepValue, + TOnMountReturn = undefined, + TOnChangeReturn = undefined, + TOnChangeAsyncReturn = undefined, + TOnBlurReturn = undefined, + TOnBlurAsyncReturn = undefined, + TOnSubmitReturn = undefined, + TOnSubmitAsyncReturn = undefined, + TFormOnMountReturn = undefined, + TFormOnChangeReturn = undefined, + TFormOnChangeAsyncReturn = undefined, + TFormOnBlurReturn = undefined, + TFormOnBlurAsyncReturn = undefined, + TFormOnSubmitReturn = undefined, + TFormOnSubmitAsyncReturn = undefined, + TFormOnServerReturn = undefined, > = ( - form: FormApi, + form: FormApi< + TParentData, + TFormValidator, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + >, options: FieldOptions< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn >, render: renderCallback< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn >, ) => { values: { @@ -66,14 +144,36 @@ type fieldDirectiveType< TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn > render: renderCallback< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn > } } @@ -83,20 +183,61 @@ export class TanStackFormController< TFormValidator extends | Validator | undefined = undefined, + TFormOnMountReturn = undefined, + TFormOnChangeReturn = undefined, + TFormOnChangeAsyncReturn = undefined, + TFormOnBlurReturn = undefined, + TFormOnBlurAsyncReturn = undefined, + TFormOnSubmitReturn = undefined, + TFormOnSubmitAsyncReturn = undefined, + TFormOnServerReturn = undefined, > implements ReactiveController { #host: ReactiveControllerHost #subscription?: () => void - api: FormApi + api: FormApi< + TParentData, + TFormValidator, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + > constructor( host: ReactiveControllerHost, - config?: FormOptions, + config?: FormOptions< + TParentData, + TFormValidator, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + >, ) { ;(this.#host = host).addController(this) - this.api = new FormApi(config) + this.api = new FormApi< + TParentData, + TFormValidator, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + >(config) } hostConnected() { @@ -115,20 +256,49 @@ export class TanStackFormController< | Validator, unknown> | undefined = undefined, TData extends DeepValue = DeepValue, + TOnMountReturn = undefined, + TOnChangeReturn = undefined, + TOnChangeAsyncReturn = undefined, + TOnBlurReturn = undefined, + TOnBlurAsyncReturn = undefined, + TOnSubmitReturn = undefined, + TOnSubmitAsyncReturn = undefined, >( fieldConfig: FieldOptions< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn >, render: renderCallback< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn >, ) { return ( @@ -137,7 +307,22 @@ export class TanStackFormController< TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn > )(this.api, fieldConfig, render) } @@ -153,9 +338,45 @@ class FieldDirective< | Validator | undefined = undefined, TData extends DeepValue = DeepValue, + TOnMountReturn = undefined, + TOnChangeReturn = undefined, + TOnChangeAsyncReturn = undefined, + TOnBlurReturn = undefined, + TOnBlurAsyncReturn = undefined, + TOnSubmitReturn = undefined, + TOnSubmitAsyncReturn = undefined, + TFormOnMountReturn = undefined, + TFormOnChangeReturn = undefined, + TFormOnChangeAsyncReturn = undefined, + TFormOnBlurReturn = undefined, + TFormOnBlurAsyncReturn = undefined, + TFormOnSubmitReturn = undefined, + TFormOnSubmitAsyncReturn = undefined, + TFormOnServerReturn = undefined, > extends AsyncDirective { #registered = false - #field?: FieldApi + #field?: FieldApi< + TParentData, + TName, + TFieldValidator, + TFormValidator, + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + > #unmount?: () => void constructor(partInfo: PartInfo) { @@ -198,20 +419,53 @@ class FieldDirective< } render( - _form: FormApi, + _form: FormApi< + TParentData, + TFormValidator, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn + >, _fieldConfig: FieldOptions< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn >, _renderCallback: renderCallback< TParentData, TName, TFieldValidator, TFormValidator, - TData + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn, + TFormOnMountReturn, + TFormOnChangeReturn, + TFormOnChangeAsyncReturn, + TFormOnBlurReturn, + TFormOnBlurAsyncReturn, + TFormOnSubmitReturn, + TFormOnSubmitAsyncReturn, + TFormOnServerReturn >, ) { if (this.#field) { diff --git a/packages/lit-form/tests/simple.ts b/packages/lit-form/tests/simple.ts index 25a36404f..ebd28a0cc 100644 --- a/packages/lit-form/tests/simple.ts +++ b/packages/lit-form/tests/simple.ts @@ -42,7 +42,7 @@ export class TestForm extends LitElement { value.length < 3 ? 'Not long enough' : undefined, }, }, - (field: FieldApi) => { + (field) => { return html`
) => { + (field) => { return html`
) => { + (field) => { return html`