Skip to content

Commit 9dff5e8

Browse files
committed
fix(material/chips): chips form control updating value immediately
Currently, when we have chips with form control, the value is updated only when its focused out. This fix will update the value of form control immediately Fixes #28065
1 parent 87f0621 commit 9dff5e8

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

goldens/material/chips/index.api.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export class MatChipGrid extends MatChipSet implements AfterContentInit, AfterVi
165165
protected _allowFocusEscape(): void;
166166
_blur(): void;
167167
readonly change: EventEmitter<MatChipGridChange>;
168+
_change(): void;
168169
get chipBlurChanges(): Observable<MatChipEvent>;
169170
protected _chipInput: MatChipTextControl;
170171
// (undocumented)

src/material/chips/chip-grid.spec.ts

+72
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,44 @@ describe('MatChipGrid', () => {
10151015
}));
10161016
});
10171017

1018+
describe('chip with form control', () => {
1019+
let fixture: ComponentFixture<ChipsFormControlUpdate>;
1020+
let component: ChipsFormControlUpdate;
1021+
let nativeInput: HTMLInputElement;
1022+
let nativeButton: HTMLButtonElement;
1023+
1024+
beforeEach(() => {
1025+
fixture = createComponent(ChipsFormControlUpdate);
1026+
component = fixture.componentInstance;
1027+
nativeInput = fixture.nativeElement.querySelector('input');
1028+
nativeButton = fixture.nativeElement.querySelector('button[id="save"]');
1029+
});
1030+
1031+
it('should update the form control value when pressed enter', fakeAsync(() => {
1032+
nativeInput.value = 'hello';
1033+
nativeInput.focus();
1034+
fixture.detectChanges();
1035+
1036+
dispatchKeyboardEvent(document.activeElement!, 'keydown', ENTER);
1037+
fixture.detectChanges();
1038+
flush();
1039+
1040+
expect(component.keywordChipControl.value).not.toBeNull();
1041+
expect(component.keywordChipControl.value.length).toBe(1);
1042+
expect(nativeButton.disabled).toBeFalsy();
1043+
1044+
nativeInput.value = 'how are you ?';
1045+
nativeInput.focus();
1046+
fixture.detectChanges();
1047+
1048+
dispatchKeyboardEvent(document.activeElement!, 'keydown', ENTER);
1049+
fixture.detectChanges();
1050+
flush();
1051+
1052+
expect(component.keywordChipControl.value.length).toBe(2);
1053+
}));
1054+
});
1055+
10181056
function createComponent<T>(
10191057
component: Type<T>,
10201058
direction: Direction = 'ltr',
@@ -1220,3 +1258,37 @@ class ChipGridWithRemove {
12201258
this.chips.splice(event.chip.value, 1);
12211259
}
12221260
}
1261+
1262+
@Component({
1263+
template: `
1264+
<mat-form-field>
1265+
<mat-label>Keywords</mat-label>
1266+
<mat-chip-grid #chipGrid [formControl]="keywordChipControl">
1267+
@for (keyword of keywords; track keyword) {
1268+
<mat-chip-row>{{keyword}}</mat-chip-row>
1269+
}
1270+
</mat-chip-grid>
1271+
<input placeholder="New keyword..." [matChipInputFor]="chipGrid" (matChipInputTokenEnd)="add($event)">
1272+
</mat-form-field>
1273+
<button id="save" [disabled]="!keywordChipControl.valid">Save</button>
1274+
<button >Cancel</button>`,
1275+
standalone: false,
1276+
})
1277+
class ChipsFormControlUpdate {
1278+
keywords = new Array<string>();
1279+
keywordChipControl = new FormControl();
1280+
1281+
constructor() {
1282+
this.keywordChipControl.setValidators(Validators.required);
1283+
}
1284+
1285+
add(event: MatChipInputEvent): void {
1286+
const value = (event.value || '').trim();
1287+
1288+
if (value) {
1289+
this.keywords.push(value);
1290+
}
1291+
1292+
event.chipInput.clear();
1293+
}
1294+
}

src/material/chips/chip-grid.ts

+10
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ export class MatChipGrid
415415
}
416416
}
417417

418+
/** When called, propagates the changes and update the immediately */
419+
_change() {
420+
if (!this.disabled) {
421+
// Timeout is needed to wait for the focus() event trigger on chip input.
422+
setTimeout(() => {
423+
this._propagateChanges();
424+
});
425+
}
426+
}
427+
418428
/**
419429
* Removes the `tabindex` from the chip grid and resets it back afterwards, allowing the
420430
* user to tab out of it. This prevents the grid from capturing focus and redirecting

src/material/chips/chip-input.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
198198
value: this.inputElement.value,
199199
chipInput: this,
200200
});
201-
201+
this._chipGrid._change();
202+
this._chipGrid.stateChanges.next();
202203
event?.preventDefault();
203204
}
204205
}

0 commit comments

Comments
 (0)