Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ngneat/dirty-check-forms
π¬Detect Unsaved Changes in Angular Forms
https://github.com/ngneat/dirty-check-forms
angular beforeunload dirty-check forms guard reactive reactive-forms
Last synced: about 1 month ago
JSON representation
π¬Detect Unsaved Changes in Angular Forms
- Host: GitHub
- URL: https://github.com/ngneat/dirty-check-forms
- Owner: ngneat
- License: mit
- Created: 2020-03-19T08:58:35.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T16:12:57.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T21:34:40.360Z (about 2 months ago)
- Topics: angular, beforeunload, dirty-check, forms, guard, reactive, reactive-forms
- Language: TypeScript
- Homepage: https://netbasal.com/detect-unsaved-changes-in-angular-forms-75fd8f5f1fa6
- Size: 2.66 MB
- Stars: 199
- Watchers: 9
- Forks: 15
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-opensource-israel - @ngneat/dirty-check-forms - π¬Detect Unsaved Changes in Angular Forms ![GitHub last commit](https://img.shields.io/github/last-commit/ngneat/dirty-check-forms?style=flat-square) ![GitHub top language](https://img.shields.io/github/languages/top/ngneat/dirty-check-forms?style=flat-square) ![GitHub stars](https://img.shields.io/github/stars/ngneat/dirty-check-forms?style=flat-square) (Projects by main language / angular)
README
![Test](https://github.com/ngneat/dirty-check-forms/workflows/Test/badge.svg?branch=master)
[![MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?style=flat-square)]()
[![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]()
[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)]()
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)
[![ngneat](https://img.shields.io/badge/@-ngneat-383636?style=flat-square&labelColor=8f68d4)](https://github.com/ngneat/)
[![spectator](https://img.shields.io/badge/tested%20with-spectator-2196F3.svg?style=flat-square)]()> 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](#installation)
- [Usage](#usage)
- [In-App Navigation](#In-App-Navigation)## 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`.
- `useBeforeunloadEvent` - enable or disable `onbeforeunload` event handling. Defaults to `true`.The function returns an `Observable`, 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:
```ts
import { dirtyCheck } from '@ngneat/dirty-check-forms';@Component({ ... })
export class SettingsComponent {
storeSub: Subscription;settings = new FormGroup({
firstName: new FormControl(''),
lastName: new FormControl('')
});isDirty$: Observable;
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();
}
}
``````html
Submit
```
### In-App Navigation:
Create a guard that extends `DirtyCheckGuard`, and provide the `confirmChanges` method:
```ts
import { Injectable } from "@angular/core";
import { DirtyCheckGuard } from "@ngneat/dirty-check-forms";
import { Observable } from "rxjs";@Injectable()
export class DirtyGuard extends DirtyCheckGuard {
constructor() {
super();
}confirmChanges(): Observable | boolean {
return confirm('Are you sure you want to discard changes?');
}
}
```Note that when using a guard, your component **must** implement the `DirtyComponent` interface:
```ts
import { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms';@Component({ ... })
export class SettingsComponent implements DirtyComponent { ... }
```The last step is to activate the `DirtyCheckGuard`:
```ts
const routes: Routes = [
{
path: 'settings',
component: SettingsComponent,
canDeactivate: [DirtyCheckGuard],
},
];
```You can find a complete example [here](https://github.com/ngneat/dirty-check-forms/tree/master/apps/playground).
## Contributors β¨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Dan Roujinsky
π» π π‘ π€ π
Netanel Basal
π π» π π¨ π π‘ π π§ π β οΈ
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!