All Projects β†’ ngneat β†’ Dirty Check Forms

ngneat / Dirty Check Forms

Licence: mit
🐬Detect Unsaved Changes in Angular Forms

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Dirty Check Forms

forms
A library to build declarative, composable, reactive user interfaces with WebSharper.
Stars: ✭ 12 (-88.57%)
Mutual labels:  reactive, forms
Reactive forms
This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular's Reactive Forms
Stars: ✭ 135 (+28.57%)
Mutual labels:  reactive, forms
Form
Form is an iOS Swift library for building and styling UIs
Stars: ✭ 99 (-5.71%)
Mutual labels:  reactive, forms
Reactive Forms
(Angular Reactive) Forms with Benefits πŸ˜‰
Stars: ✭ 276 (+162.86%)
Mutual labels:  reactive, forms
Morpheus
Reactive type-safe Scala driver for SQL databases
Stars: ✭ 101 (-3.81%)
Mutual labels:  reactive
Formatic
✏️ A configurable, pluggable forms library for React used on Zapier.com.
Stars: ✭ 93 (-11.43%)
Mutual labels:  forms
Viewi
Powerful tool for building full-stack and completely reactive user interfaces using PHP
Stars: ✭ 93 (-11.43%)
Mutual labels:  reactive
Alpakka Kafka
Alpakka Kafka connector - Alpakka is a Reactive Enterprise Integration library for Java and Scala, based on Reactive Streams and Akka.
Stars: ✭ 1,295 (+1133.33%)
Mutual labels:  reactive
Nostyle
Design System
Stars: ✭ 101 (-3.81%)
Mutual labels:  forms
Sodium Typescript
Typescript/Javascript implementation of Sodium FRP (Functional Reactive Programming) library
Stars: ✭ 102 (-2.86%)
Mutual labels:  reactive
Uniforms
A React library for building forms from any schema.
Stars: ✭ 1,368 (+1202.86%)
Mutual labels:  forms
Swiftforms
A small and lightweight library written in Swift that allows you to easily create forms.
Stars: ✭ 1,329 (+1165.71%)
Mutual labels:  forms
Play2 Html5tags
HTML5 form tags module for Play Framework
Stars: ✭ 101 (-3.81%)
Mutual labels:  forms
Workflow Swift
A Swift and Kotlin library for making composable state machines, and UIs driven by those state machines.
Stars: ✭ 92 (-12.38%)
Mutual labels:  reactive
Input Value
React hook for creating input values
Stars: ✭ 104 (-0.95%)
Mutual labels:  forms
Lda Topic Modeling
A PureScript, browser-based implementation of LDA topic modeling.
Stars: ✭ 91 (-13.33%)
Mutual labels:  reactive
Rsfloatinputview
A Float Input View with smooth animation and supporting icon and seperator written with Swift
Stars: ✭ 103 (-1.9%)
Mutual labels:  forms
Squbs
Akka Streams & Akka HTTP for Large-Scale Production Deployments
Stars: ✭ 1,365 (+1200%)
Mutual labels:  reactive
Connective
agent-based reactive programming library for typescript
Stars: ✭ 98 (-6.67%)
Mutual labels:  reactive
Derivatives
🌱 Your companion to create derived values from a single source (atom)
Stars: ✭ 101 (-3.81%)
Mutual labels:  reactive


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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].