{"id":13627279,"url":"https://github.com/ngneat/dirty-check-forms","last_synced_at":"2025-05-07T18:15:34.226Z","repository":{"id":40288581,"uuid":"248455140","full_name":"ngneat/dirty-check-forms","owner":"ngneat","description":"🐬Detect Unsaved Changes in Angular Forms","archived":false,"fork":false,"pushed_at":"2023-03-07T16:12:57.000Z","size":2788,"stargazers_count":198,"open_issues_count":16,"forks_count":14,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-07T18:15:30.784Z","etag":null,"topics":["angular","beforeunload","dirty-check","forms","guard","reactive","reactive-forms"],"latest_commit_sha":null,"homepage":"https://netbasal.com/detect-unsaved-changes-in-angular-forms-75fd8f5f1fa6","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ngneat.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":"ngneat"}},"created_at":"2020-03-19T08:58:35.000Z","updated_at":"2025-03-11T18:42:40.000Z","dependencies_parsed_at":"2023-02-18T15:01:50.566Z","dependency_job_id":null,"html_url":"https://github.com/ngneat/dirty-check-forms","commit_stats":{"total_commits":68,"total_committers":6,"mean_commits":"11.333333333333334","dds":0.7205882352941176,"last_synced_commit":"e014b03abdbfb66a109a0f5f5b12a6acdc921018"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fdirty-check-forms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fdirty-check-forms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fdirty-check-forms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fdirty-check-forms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngneat","download_url":"https://codeload.github.com/ngneat/dirty-check-forms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252931507,"owners_count":21827112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","beforeunload","dirty-check","forms","guard","reactive","reactive-forms"],"created_at":"2024-08-01T22:00:32.335Z","updated_at":"2025-05-07T18:15:34.199Z","avatar_url":"https://github.com/ngneat.png","language":"TypeScript","funding_links":["https://github.com/sponsors/ngneat"],"categories":["Projects by main language"],"sub_categories":["angular"],"readme":"\u003cp align=\"center\"\u003e\n \u003cimg width=\"20%\" height=\"20%\" src=\"./logo.svg\"\u003e\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\n![Test](https://github.com/ngneat/dirty-check-forms/workflows/Test/badge.svg?branch=master)\n[![MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?style=flat-square)]()\n[![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]()\n[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)]()\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)\n[![ngneat](https://img.shields.io/badge/@-ngneat-383636?style=flat-square\u0026labelColor=8f68d4)](https://github.com/ngneat/)\n[![spectator](https://img.shields.io/badge/tested%20with-spectator-2196F3.svg?style=flat-square)]()\n\n\u003e The cleanest way to do the dirty job\n\nDetect Unsaved Changes in Angular Forms\n\n\u003cimg src=\"https://miro.medium.com/max/1400/1*OEA-Gdmy4GFmkNPCtwHXKg.gif\"\u003e\n\n## Features\n\n- ✅ Dirty Check Forms!\n- ✅ Support In-App Navigation\n- ✅ Support Form Departure\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [In-App Navigation](#In-App-Navigation)\n\n## Installation\n\n`npm install @ngneat/dirty-check-forms`\n\n## Usage\n\nCall the `dirtyCheck` function, which accepts three arguments:\n\n1. AbstractControl (`FormControl`, `FormGroup`, `FormArray`)\n2. A stream with the original value to compare\n3. Config:  \n  - `debounce` - debounce time of `valueChanges`. Defaults to 300  \n  - `withDisabled` - whether to include disable fields (by using control's `getRawValue`) or not. Defaults to `true`. \n  - `useBeforeunloadEvent` - enable or disable `onbeforeunload` event handling. Defaults to `true`.\n\nThe function returns an `Observable\u003cboolean\u003e`, 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.\n\nFor example:\n\n```ts\nimport { dirtyCheck } from '@ngneat/dirty-check-forms';\n\n@Component({ ... })\nexport class SettingsComponent {\n  storeSub: Subscription;\n\n  settings = new FormGroup({\n    firstName: new FormControl(''),\n    lastName: new FormControl('')\n  });\n\n  isDirty$: Observable\u003cboolean\u003e;\n\n  constructor(private store: Store) {}\n\n  ngOnInit() {\n    // Update the form with the current store value\n    this.storeSub = this.store.selectSettings()\n      .subscribe(state =\u003e this.settings.patchValue(state, { emitEvent: false }));\n\n    // Initialize dirtyCheck\n    this.isDirty$ = dirtyCheck(this.settings, this.store.selectSettings());\n  }\n\n  ngOnDestroy() {\n    this.storeSub \u0026\u0026 this.storeSub.unsubscribe();\n  }\n}\n```\n\n```html\n\u003cform [formGroup]=\"settings\"\u003e\n  \u003cinput type=\"text\" formControlName=\"firstName\" placeholder=\"First name\" /\u003e\n  \u003cinput type=\"text\" formControlName=\"lastName\" placeholder=\"Last name\" /\u003e\n\n  \u003cbutton (click)=\"submit()\" [disabled]=\"isDirty$ | async\"\u003eSubmit\u003c/button\u003e\n\u003c/form\u003e\n```\n\n### In-App Navigation:\n\nCreate a guard that extends `DirtyCheckGuard`, and provide the `confirmChanges` method:\n\n```ts\nimport { Injectable } from \"@angular/core\";\nimport { DirtyCheckGuard } from \"@ngneat/dirty-check-forms\";\nimport { Observable } from \"rxjs\";\n\n@Injectable()\nexport class DirtyGuard extends DirtyCheckGuard {\n  constructor() {\n    super();\n  }\n\n  confirmChanges(): Observable\u003cboolean\u003e | boolean {\n    return confirm('Are you sure you want to discard changes?');\n  }\n}\n```\n\nNote that when using a guard, your component **must** implement the `DirtyComponent` interface:\n\n```ts\nimport { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms';\n\n@Component({ ... })\nexport class SettingsComponent implements DirtyComponent { ... }\n```\n\nThe last step is to activate the `DirtyCheckGuard`:\n\n```ts\nconst routes: Routes = [\n  {\n    path: 'settings',\n    component: SettingsComponent,\n    canDeactivate: [DirtyCheckGuard],\n  },\n];\n```\n\nYou can find a complete example [here](https://github.com/ngneat/dirty-check-forms/tree/master/apps/playground).\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/danzrou\"\u003e\u003cimg src=\"https://avatars3.githubusercontent.com/u/6433766?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDan Roujinsky\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/@ngneat/dirty-check-forms/commits?author=danzrou\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/@ngneat/dirty-check-forms/commits?author=danzrou\" title=\"Documentation\"\u003e📖\u003c/a\u003e \u003ca href=\"#example-danzrou\" title=\"Examples\"\u003e💡\u003c/a\u003e \u003ca href=\"#ideas-danzrou\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"#projectManagement-danzrou\" title=\"Project Management\"\u003e📆\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://www.netbasal.com\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/6745730?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eNetanel Basal\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#blog-NetanelBasal\" title=\"Blogposts\"\u003e📝\u003c/a\u003e \u003ca href=\"https://github.com/@ngneat/dirty-check-forms/commits?author=NetanelBasal\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#content-NetanelBasal\" title=\"Content\"\u003e🖋\u003c/a\u003e \u003ca href=\"#design-NetanelBasal\" title=\"Design\"\u003e🎨\u003c/a\u003e \u003ca href=\"https://github.com/@ngneat/dirty-check-forms/commits?author=NetanelBasal\" title=\"Documentation\"\u003e📖\u003c/a\u003e \u003ca href=\"#example-NetanelBasal\" title=\"Examples\"\u003e💡\u003c/a\u003e \u003ca href=\"#infra-NetanelBasal\" title=\"Infrastructure (Hosting, Build-Tools, etc)\"\u003e🚇\u003c/a\u003e \u003ca href=\"#maintenance-NetanelBasal\" title=\"Maintenance\"\u003e🚧\u003c/a\u003e \u003ca href=\"#projectManagement-NetanelBasal\" title=\"Project Management\"\u003e📆\u003c/a\u003e \u003ca href=\"https://github.com/@ngneat/dirty-check-forms/commits?author=NetanelBasal\" title=\"Tests\"\u003e⚠️\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngneat%2Fdirty-check-forms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngneat%2Fdirty-check-forms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngneat%2Fdirty-check-forms/lists"}