Coder Social home page Coder Social logo

ffkl / dirty-check-forms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ngneat/dirty-check-forms

0.0 0.0 0.0 2.03 MB

🐬Detect Unsaved Changes in Angular Forms

Home Page: https://netbasal.com/detect-unsaved-changes-in-angular-forms-75fd8f5f1fa6

License: MIT License

TypeScript 80.40% JavaScript 12.24% HTML 6.58% SCSS 0.78%

dirty-check-forms's Introduction


Test MIT commitizen PRs styled with prettier All Contributors ngneat spectator

The cleanest way to do the dirty job

Detect Unsaved Changes in Angular Forms

Features

  • βœ… Dirty Check Forms!
  • βœ… Support In-App Navigation
  • βœ… Support Form Departure

Table of Contents

Installation

npm install @ngneat/dirty-check-forms

Usage

Call the dirtyCheck function, which accepts three arguments:

  1. AbstractControl (FormControl, FormGroup, FormArray)
  2. A stream with the original value to compare
  3. Config:
  • debounce - debounce time of valueChanges. Defaults to 300
  • withDisabled - whether to include disable fields (by using control's getRawValue) or not. Defaults to true.

The function returns an Observable<boolean>, which notifies whether the form is dirty. Furthermore, it also hooks on the browser's beforeunload event to confirm upon refreshing/closing the tab when needed.

For example:

import { dirtyCheck } from '@ngneat/dirty-check-forms';

@Component({ ... })
export class SettingsComponent {
  storeSub: Subscription;

  settings = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl('')
  });

  isDirty$: Observable<boolean>;

  constructor(private store: Store) {}

  ngOnInit() {
    // Update the form with the current store value
    this.storeSub = this.store.selectSettings()
      .subscribe(state => this.settings.patchValue(state, { emitEvent: false }));

    // Initialize dirtyCheck
    this.isDirty$ = dirtyCheck(this.settings, this.store.selectSettings());
  }

  ngOnDestroy() {
    this.storeSub && this.storeSub.unsubscribe();
  }
}
<form [formGroup]="settings">
  <input type="text" formControlName="firstName" placeholder="First name" />
  <input type="text" formControlName="lastName" placeholder="Last name" />

  <button (click)="submit()" [disabled]="isDirty$ | async">Submit</button>
</form>

In-App Navigation:

Create a guard that extends DirtyCheckGuard, and provide the confirmChanges method:

import { DirtyCheckGuard, DirtyComponent } from '@ngneat/dirty-check-forms';

@Injectable()
export class DirtyGuard extends DirtyCheckGuard<DirtyComponent> {
  constructor() {
    super();
  }

  confirmChanges(): Observable<boolean> | boolean {
    return confirm('Are you sure you want to discard changes?');
  }
}

Note that when using a guard, your component must implement the DirtyComponent interface:

import { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms';

@Component({ ... })
export class SettingsComponent implements DirtyComponent { ... }

The last step is to activate the DirtyCheckGuard:

const routes: Routes = [
  {
    path: 'settings',
    component: SettingsComponent,
    canDeactivate: [DirtyCheckGuard],
  },
];

You can find a complete example here.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dan Roujinsky

πŸ’» πŸ“– πŸ’‘ πŸ€” πŸ“†

Netanel Basal

πŸ“ πŸ’» πŸ–‹ 🎨 πŸ“– πŸ’‘ πŸš‡ 🚧 πŸ“† ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.