Skip to content

fix(material/chips): show required asterisk when using required validator #23379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/material-experimental/mdc-chips/chip-grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ describe('MDC-based MatChipGrid', () => {
.withContext(`Expected placeholder not to have an asterisk, as control was not required.`)
.toBeNull();

fixture.componentInstance.isRequired = true;
fixture.componentInstance.chipGrid.required = true;
fixture.detectChanges();

requiredMarker = fixture.debugElement.query(By.css('.mdc-floating-label--required'))!;
Expand All @@ -741,6 +741,15 @@ describe('MDC-based MatChipGrid', () => {
.toBeNull();
});

it('should mark the component as required if the control has a required validator', () => {
fixture.destroy();
fixture = TestBed.createComponent(InputChipGrid);
fixture.componentInstance.control = new FormControl(undefined, [Validators.required]);
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mdc-floating-label--required')).toBeTruthy();
});

it('should blur the form field when the active chip is blurred', fakeAsync(() => {
const formField: HTMLElement = fixture.nativeElement.querySelector('.mat-mdc-form-field');

Expand Down Expand Up @@ -1077,8 +1086,7 @@ class FormFieldChipGrid {
template: `
<mat-form-field>
<mat-label>New food...</mat-label>
<mat-chip-grid #chipGrid
placeholder="Food" [formControl]="control" [required]="isRequired">
<mat-chip-grid #chipGrid placeholder="Food" [formControl]="control">
<mat-chip-row *ngFor="let food of foods" [value]="food.value" (removed)="remove(food)">
{{ food.viewValue }}
</mat-chip-row>
Expand Down Expand Up @@ -1106,7 +1114,6 @@ class InputChipGrid {

separatorKeyCodes = [ENTER, SPACE];
addOnBlur: boolean = true;
isRequired: boolean;

add(event: MatChipInputEvent): void {
const value = (event.value || '').trim();
Expand Down
14 changes: 11 additions & 3 deletions src/material-experimental/mdc-chips/chip-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import {
Self,
ViewEncapsulation
} from '@angular/core';
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {
ControlValueAccessor,
FormGroupDirective,
NgControl,
NgForm,
Validators,
} from '@angular/forms';
import {
CanUpdateErrorState,
CanUpdateErrorStateCtor,
Expand Down Expand Up @@ -186,12 +192,14 @@ export class MatChipGrid extends _MatChipGridMixinBase implements AfterContentIn
* @docs-private
*/
@Input()
get required(): boolean { return this._required; }
get required(): boolean {
return this._required ?? this.ngControl?.control?.hasValidator(Validators.required) ?? false;
}
set required(value: boolean) {
this._required = coerceBooleanProperty(value);
this.stateChanges.next();
}
protected _required: boolean = false;
protected _required: boolean | undefined;

/**
* Implemented as part of MatFormFieldControl.
Expand Down
12 changes: 9 additions & 3 deletions src/material/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ describe('MatChipList', () => {
.withContext(`Expected placeholder not to have an asterisk, as control was not required.`)
.toBeNull();

fixture.componentInstance.isRequired = true;
fixture.componentInstance.chipList.required = true;
fixture.detectChanges();

requiredMarker = fixture.debugElement.query(By.css('.mat-form-field-required-marker'))!;
Expand All @@ -866,6 +866,13 @@ describe('MatChipList', () => {
.not.toBeNull();
});

it('should mark the component as required if the control has a required validator', () => {
fixture.componentInstance.control = new FormControl(undefined, [Validators.required]);
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mat-form-field-required-marker')).toBeTruthy();
});

it('should be able to programmatically select a falsy option', () => {
fixture.destroy();
TestBed.resetTestingModule();
Expand Down Expand Up @@ -1487,7 +1494,7 @@ class FormFieldChipList {
selector: 'basic-chip-list',
template: `
<mat-form-field>
<mat-chip-list placeholder="Food" [formControl]="control" [required]="isRequired"
<mat-chip-list placeholder="Food" [formControl]="control"
[tabIndex]="tabIndexOverride" [selectable]="selectable">
<mat-chip *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
{{ food.viewValue }}
Expand All @@ -1508,7 +1515,6 @@ class BasicChipList {
{value: 'sushi-7', viewValue: 'Sushi'},
];
control = new FormControl();
isRequired: boolean;
tabIndexOverride: number;
selectable: boolean;

Expand Down
14 changes: 11 additions & 3 deletions src/material/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import {
Self,
ViewEncapsulation,
} from '@angular/core';
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {
ControlValueAccessor,
FormGroupDirective,
NgControl,
NgForm,
Validators,
} from '@angular/forms';
import {
CanUpdateErrorState,
ErrorStateMatcher,
Expand Down Expand Up @@ -213,12 +219,14 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
* @docs-private
*/
@Input()
get required(): boolean { return this._required; }
get required(): boolean {
return this._required ?? this.ngControl?.control?.hasValidator(Validators.required) ?? false;
}
set required(value: boolean) {
this._required = coerceBooleanProperty(value);
this.stateChanges.next();
}
protected _required: boolean = false;
protected _required: boolean | undefined;

/**
* Implemented as part of MatFormFieldControl.
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/chips.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
get required(): boolean;
set required(value: boolean);
// (undocumented)
protected _required: boolean;
protected _required: boolean | undefined;
get role(): string | null;
get selectable(): boolean;
set selectable(value: boolean);
Expand Down