Skip to content

Commit 2789d8e

Browse files
authored
feat(material-experimental/mdc-card): add DI token for configuring appearance (#23302)
Adds a new DI token to the MDC-based card that allows consumers to set the default appearance. Also sets up unit tests for the card. Fixes #23298.
1 parent e791236 commit 2789d8e

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

src/material-experimental/mdc-card/BUILD.bazel

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
2-
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "sass_binary", "sass_library")
2+
load(
3+
"//tools:defaults.bzl",
4+
"ng_e2e_test_library",
5+
"ng_module",
6+
"ng_test_library",
7+
"ng_web_test_suite",
8+
"sass_binary",
9+
"sass_library",
10+
)
311

412
package(default_visibility = ["//visibility:public"])
513

@@ -52,3 +60,19 @@ e2e_test_suite(
5260
"//src/cdk/testing/private/e2e",
5361
],
5462
)
63+
64+
ng_test_library(
65+
name = "unit_test_sources",
66+
srcs = glob(
67+
["**/*.spec.ts"],
68+
exclude = ["**/*.e2e.spec.ts"],
69+
),
70+
deps = [
71+
":mdc-card",
72+
],
73+
)
74+
75+
ng_web_test_suite(
76+
name = "unit_tests",
77+
deps = [":unit_test_sources"],
78+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {Component, Provider, Type, ViewChild} from '@angular/core';
3+
import {MatCardModule} from './module';
4+
import {MatCard, MAT_CARD_CONFIG} from './card';
5+
6+
7+
describe('MDC-based MatCard', () => {
8+
function createComponent<T>(component: Type<T>, providers: Provider[] = []): ComponentFixture<T> {
9+
TestBed.configureTestingModule({
10+
imports: [MatCardModule],
11+
declarations: [component],
12+
providers
13+
}).compileComponents();
14+
15+
return TestBed.createComponent<T>(component);
16+
}
17+
18+
it('should default the card to the `raised` appearance', () => {
19+
const fixture = createComponent(BasicCard);
20+
fixture.detectChanges();
21+
const card = fixture.nativeElement.querySelector('.mat-mdc-card');
22+
expect(card.classList).not.toContain('mdc-card--outlined');
23+
});
24+
25+
it('should be able to change the card appearance', () => {
26+
const fixture = createComponent(BasicCard);
27+
fixture.detectChanges();
28+
const card = fixture.nativeElement.querySelector('.mat-mdc-card');
29+
30+
expect(card.classList).not.toContain('mdc-card--outlined');
31+
32+
fixture.componentInstance.card.appearance = 'outlined';
33+
fixture.detectChanges();
34+
35+
expect(card.classList).toContain('mdc-card--outlined');
36+
});
37+
38+
it('should be able to change the default card appearance using DI', () => {
39+
const fixture = createComponent(BasicCard, [{
40+
provide: MAT_CARD_CONFIG,
41+
useValue: {appearance: 'outlined'}
42+
}]);
43+
fixture.detectChanges();
44+
const card = fixture.nativeElement.querySelector('.mat-mdc-card');
45+
expect(card.classList).toContain('mdc-card--outlined');
46+
});
47+
48+
});
49+
50+
@Component({
51+
template: '<mat-card></mat-card>'
52+
})
53+
class BasicCard {
54+
@ViewChild(MatCard) card: MatCard;
55+
}

src/material-experimental/mdc-card/card.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ import {
1010
ChangeDetectionStrategy,
1111
Component,
1212
Directive,
13+
Inject,
14+
InjectionToken,
1315
Input,
16+
Optional,
1417
ViewEncapsulation,
1518
} from '@angular/core';
1619

1720
export type MatCardAppearance = 'outlined' | 'raised';
1821

22+
/** Object that can be used to configure the default options for the card module. */
23+
export interface MatCardConfig {
24+
/** Default appearance for cards. */
25+
appearance?: MatCardAppearance;
26+
}
27+
28+
/** Injection token that can be used to provide the default options the card module. */
29+
export const MAT_CARD_CONFIG = new InjectionToken<MatCardConfig>('MAT_CARD_CONFIG');
30+
1931
/**
2032
* Material Design card component. Cards contain content and actions about a single subject.
2133
* See https://material.io/design/components/cards.html
@@ -35,8 +47,11 @@ export type MatCardAppearance = 'outlined' | 'raised';
3547
changeDetection: ChangeDetectionStrategy.OnPush,
3648
})
3749
export class MatCard {
38-
@Input() appearance: MatCardAppearance = 'raised';
50+
@Input() appearance: MatCardAppearance;
3951

52+
constructor(@Inject(MAT_CARD_CONFIG) @Optional() config?: MatCardConfig) {
53+
this.appearance = config?.appearance || 'raised';
54+
}
4055
}
4156

4257
// TODO(jelbourn): add `MatActionCard`, which is a card that acts like a button (and has a ripple).

0 commit comments

Comments
 (0)