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

Angular adapter #627

Merged
merged 36 commits into from
Mar 23, 2024
Merged

Angular adapter #627

merged 36 commits into from
Mar 23, 2024

Conversation

crutchcorn
Copy link
Member

@crutchcorn crutchcorn commented Mar 17, 2024

Hi Enea

This PR starts an initial Angular adapter that has an API like so:

import { Component } from '@angular/core'
import {
  FieldValidateFn,
  injectForm,
  TanStackField,
} from '@tanstack/angular-form'

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [TanStackField],
  template: `
    <p>Testing</p>
    <form (submit)="handleSubmit($event)">
      <ng-container
        [tanstackField]="form"
        name="firstName"
        [validators]="{ onChange: required }"
        #f="field"
      >
        <label>
          <div>First name:</div>
          <input
            [value]="f.api.state.value"
            (blur)="f.api.handleBlur()"
            (input)="f.api.handleChange($any($event).target.value)"
          />
        </label>
        @for (error of f.api.state.meta.errors; track $index) {
          <div style="color: red">
            {{ error }}
          </div>
        }
      </ng-container>
      <button>Submit</button>
    </form>
  `,
})
export class AppComponent {
  required: FieldValidateFn<any, string> = ({ value }) => {
    return !value ? 'Required' : undefined
  }
  form = injectForm({
    defaultValues: {
      firstName: 'Ryan',
      age: 25,
    },
    onSubmit({ value }: any) {
      console.log({ value })
    },
  })

  handleSubmit(event: SubmitEvent) {
    event.preventDefault()
    event.stopPropagation()
    void this.form.handleSubmit()
  }
}

This should work with any of the adapters (Zod) OOTB day 1.

This is still WIP despite working as we're waiting for:

  • TypeScript type validation
  • Examples
  • Tests
  • Docs
  • Fixing configuration to publish the package on merge

This API was directly inspired by conversations I had with @nartc who brainstormed the initial API. Thank you!

Copy link

nx-cloud bot commented Mar 17, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit d005782. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@crutchcorn crutchcorn marked this pull request as draft March 17, 2024 17:40
@codecov-commenter
Copy link

codecov-commenter commented Mar 18, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 88.86%. Comparing base (ac3fdb8) to head (d005782).
Report is 1 commits behind head on main.

Files Patch % Lines
packages/angular-form/src/inject-store.ts 0.00% 2 Missing ⚠️
...kages/angular-form/src/tanstack-field.directive.ts 95.23% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #627      +/-   ##
==========================================
+ Coverage   88.82%   88.86%   +0.04%     
==========================================
  Files          28       32       +4     
  Lines         814      844      +30     
  Branches      188      189       +1     
==========================================
+ Hits          723      750      +27     
- Misses         84       87       +3     
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@crutchcorn
Copy link
Member Author

Per a conversation with @alxhub at ngConf, the API has shifted from:

<ng-template
  [tanstackField]="form"
  name="firstName"
  [validators]="{ onChange: required }"
  let-field
>
  <label>
    <div>First name:</div>
    <input
      [value]="field.state.value"
      (blur)="field.handleBlur()"
      (input)="field.handleChange($any($event).target.value)"
    />
  </label>
  <div *ngFor="let error of field.state.meta.errors" style="color: red">
    {{ error }}
  </div>
</ng-template>

To:

<ng-container
  [tanstackField]="form"
  name="firstName"
  [validators]="{ onChange: required }"
  #f="field"
>
  <label>
    <div>First name:</div>
    <input
      [value]="f.api.state.value"
      (blur)="f.api.handleBlur()"
      (input)="f.api.handleChange($any($event).target.value)"
    />
  </label>
  <div *ngFor="let error of f.api.state.meta.errors" style="color: red">
    {{ error }}
  </div>
</ng-container>

This should have better performance heuristics as well as a nicer DX

Base automatically changed from fix-nested-types to main March 22, 2024 23:39
@crutchcorn crutchcorn changed the title [WIP] Angular adapter Angular adapter Mar 23, 2024
@crutchcorn crutchcorn marked this pull request as ready for review March 23, 2024 04:58
@crutchcorn crutchcorn merged commit d88bde9 into main Mar 23, 2024
2 checks passed
@crutchcorn crutchcorn deleted the angular-adapter branch March 23, 2024 05:28
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