Skip to content

Commit ad234ef

Browse files
committed
feat(popover-edit): Helper class to reduce form state preservation boilerplate
1 parent ddb9343 commit ad234ef

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export interface Entry<FormValue> {
10+
value?: FormValue;
11+
}
12+
13+
export class FormValueContainer<Key extends object, FormValue> {
14+
private _formValues = new WeakMap<Key, Entry<FormValue>>();
15+
16+
for(key: Key): Entry<FormValue> {
17+
const _formValues = this._formValues;
18+
19+
let entry = _formValues.get(key);
20+
if (!entry) {
21+
entry = {};
22+
_formValues.set(key, entry);
23+
}
24+
25+
return entry;
26+
}
27+
}

src/cdk-experimental/popover-edit/lens-directives.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
4949

5050
/**
5151
* A two-way binding for storing unsubmitted form state. If not provided
52-
* then form state will be discarded on close. The CdkEditControlPeristBy directive is offered
52+
* then form state will be discarded on close. The PeristBy directive is offered
5353
* as a convenient shortcut for these bindings.
5454
*/
5555
preservedFormValue?: FormValue;
@@ -126,15 +126,6 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
126126
}
127127
}
128128

129-
@Directive({
130-
selector: 'form[cdkEditControl][cdkEditControlPeristBy]',
131-
host: {
132-
preservedFormValue
133-
}
134-
})
135-
export class CdkEditControlPersistBy<Key, FormValue> {
136-
}
137-
138129
/** Reverts the form to its initial or previously submitted state on click. */
139130
@Directive({
140131
selector: 'button[cdkEditRevert]',

src/cdk-experimental/popover-edit/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
export * from './edit-event-dispatcher';
1010
export * from './edit-ref';
1111
export * from './focus-dispatcher';
12+
export * from './form-value-container';
1213
export * from './lens-directives';
1314
export * from './popover-edit-module';
1415
export * from './popover-edit-position-strategy-factory';

src/material-examples/popover-edit-mat-table/popover-edit-mat-table-example.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
<form #f="ngForm"
99
matEditLens
1010
(ngSubmit)="onSubmitWeight(element, f)"
11-
[matEditLensPreservedFormValue]="preservedWeightValues.get(element)"
12-
(matEditLensPreservedFormValueChange)="preservedWeightValues.set(element, $event)">
11+
[(matEditLensPreservedFormValue)]="weightValues.for(element).value">
1312
<div mat-edit-content>
1413
<mat-form-field>
1514
<input matInput type="number" [ngModel]="element.weight" name="weight" required>
@@ -38,8 +37,7 @@
3837
<form #f="ngForm"
3938
matEditLens
4039
(ngSubmit)="onSubmitName(element, f)"
41-
[matEditLensPreservedFormValue]="preservedNameValues.get(element)"
42-
(matEditLensPreservedFormValueChange)="preservedNameValues.set(element, $event)">
40+
[(matEditLensPreservedFormValue)]="nameValues.for(element).value">
4341
<h2 mat-edit-title>Name</h2>
4442
<div mat-edit-content>
4543
<mat-form-field>

src/material-examples/popover-edit-mat-table/popover-edit-mat-table-example.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component} from '@angular/core';
22
import {DataSource} from '@angular/cdk/collections';
33
import {DomSanitizer} from '@angular/platform-browser';
4+
import {FormValueContainer} from '@angular/cdk-experimental/popover-edit';
45
import {NgForm} from '@angular/forms';
56
import {MatIconRegistry} from '@angular/material';
67
import {BehaviorSubject, Observable} from 'rxjs';
@@ -47,8 +48,8 @@ export class PopoverEditMatTableExample {
4748
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
4849
dataSource = new ExampleDataSource();
4950

50-
readonly preservedNameValues = new WeakMap<PeriodicElement, any>();
51-
readonly preservedWeightValues = new WeakMap<PeriodicElement, any>();
51+
readonly nameValues = new FormValueContainer<PeriodicElement, any>();
52+
readonly weightValues = new FormValueContainer<PeriodicElement, any>();
5253

5354
constructor(iconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
5455
iconRegistry.addSvgIcon(

0 commit comments

Comments
 (0)