Skip to content

Commit db7ff98

Browse files
devversionannieyw
authored andcommitted
feat(material/list): introduce type for checkbox position
In order to provide a better way to type the checkbox position of a list option, we now provide an exported type that can be used instead of manually repeating `'before' | 'after'` in projects setting the checkbox position through a class member for example. This also allows us to improve type safety in the test harnesses.
1 parent c9692c8 commit db7ff98

File tree

9 files changed

+25
-11
lines changed

9 files changed

+25
-11
lines changed

src/dev-app/list/list-demo.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Component} from '@angular/core';
10+
import {MatListOptionCheckboxPosition} from '@angular/material/list';
1011

1112

1213
@Component({
@@ -27,7 +28,7 @@ export class ListDemo {
2728
{name: 'Bobby', headline: 'UX designer'}
2829
];
2930

30-
checkboxPosition: 'before'|'after' = 'before';
31+
checkboxPosition: MatListOptionCheckboxPosition = 'before';
3132

3233
messages: {from: string, subject: string, message: string, image: string}[] = [
3334
{

src/material-experimental/mdc-list/list-styling.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {Directive, Inject, Optional} from '@angular/core';
1010
import {LIST_OPTION, ListOption} from './list-option-types';
1111

1212
/**
13-
* Directive whose purpose is to add the MDC list-item `graphic` or `meta`
14-
* class depending on checkbox position in list options.
13+
* MDC uses the very intuitively named classes `.mdc-list-item__graphic` and `.mat-list-item__meta`
14+
* to position content such as icons or checkboxes that comes either before or after the text
15+
* content respectively. This directive detects the placement of the checkbox and applies the
16+
* correct MDC class to position the icon/avatar on the opposite side.
1517
* @docs-private
1618
*/
1719
@Directive({

src/material-experimental/mdc-list/list.scss

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
height: $size;
5555
border-radius: 50%;
5656

57-
// If the avatar is displayed in the graphic section of a list-item (i.e. at the start),
58-
// we override the default "graphic" horizontal spacing that is designed for icons, to
59-
// work for actual avatar images. The avatar by default is used within the `graphic` section,
60-
// but could be also outside in case of a selection-list option with a checkbox at start.
57+
// Avatars that come before the text have the .mdc-list-item__graphic class.
58+
// MDC's .mdc-list--avatar-list class would normally apply overrides to set the appropriate
59+
// margins for avatar images, but since ours is a per-list-item setting we need to add similar
60+
// styles ourselves.
6161
&.mdc-list-item__graphic {
6262
margin-left: 0;
6363
margin-right: $margin-value;

src/material/list/selection-list.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export class MatSelectionListChange {
8282
public options: MatListOption[]) {}
8383
}
8484

85+
/**
86+
* Type describing possible positions of a checkbox in a list option
87+
* with respect to the list item's text.
88+
*/
89+
export type MatListOptionCheckboxPosition = 'before'|'after';
90+
8591
/**
8692
* Component for list-options of selection-list. Each list-option can automatically
8793
* generate a checkbox and can put current item into the selectionModel of selection-list
@@ -131,7 +137,7 @@ export class MatListOption extends _MatListOptionMixinBase implements AfterConte
131137
@ViewChild('text') _text: ElementRef;
132138

133139
/** Whether the label should appear before or after the checkbox. Defaults to 'after' */
134-
@Input() checkboxPosition: 'before' | 'after' = 'after';
140+
@Input() checkboxPosition: MatListOptionCheckboxPosition = 'after';
135141

136142
/** Theme color of the list option. This sets the color of the checkbox. */
137143
@Input()

src/material/list/testing/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ts_library(
1313
"//src/cdk/coercion",
1414
"//src/cdk/testing",
1515
"//src/material/divider/testing",
16+
"//src/material/list",
1617
],
1718
)
1819

src/material/list/testing/list-item-harness-base.ts

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class MatSubheaderHarness extends ComponentHarness {
5353
/** Selectors for the various list item sections that may contain user content. */
5454
export const enum MatListItemSection {
5555
CONTENT = '.mat-list-item-content'
56+
// TODO(mmalerba): consider adding sections for leading/trailing icons.
5657
}
5758

5859
/**

src/material/list/testing/selection-list-harness.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {HarnessPredicate} from '@angular/cdk/testing';
10+
import {MatListOptionCheckboxPosition} from '@angular/material/list';
1011
import {MatListHarnessBase} from './list-harness-base';
1112
import {
1213
ListItemHarnessFilters,
@@ -88,7 +89,7 @@ export class MatListOptionHarness extends MatListItemHarnessBase {
8889
private _itemContent = this.locatorFor('.mat-list-item-content');
8990

9091
/** Gets the position of the checkbox relative to the list option content. */
91-
async getCheckboxPosition(): Promise<'before' | 'after'> {
92+
async getCheckboxPosition(): Promise<MatListOptionCheckboxPosition> {
9293
return await (await this._itemContent()).hasClass('mat-list-item-content-reverse') ?
9394
'after' : 'before';
9495
}

tools/public_api_guard/material/list.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export declare class MatListOption extends _MatListOptionMixinBase implements Af
5353
_icon: MatListIconCssMatStyler;
5454
_lines: QueryList<MatLine>;
5555
_text: ElementRef;
56-
checkboxPosition: 'before' | 'after';
56+
checkboxPosition: MatListOptionCheckboxPosition;
5757
get color(): ThemePalette;
5858
set color(newValue: ThemePalette);
5959
get disabled(): any;
@@ -85,6 +85,8 @@ export declare class MatListOption extends _MatListOptionMixinBase implements Af
8585
static ɵfac: i0.ɵɵFactoryDef<MatListOption, never>;
8686
}
8787

88+
export declare type MatListOptionCheckboxPosition = 'before' | 'after';
89+
8890
export declare class MatListSubheaderCssMatStyler {
8991
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatListSubheaderCssMatStyler, "[mat-subheader], [matSubheader]", never, {}, {}, never>;
9092
static ɵfac: i0.ɵɵFactoryDef<MatListSubheaderCssMatStyler, never>;

tools/public_api_guard/material/list/testing.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export declare class MatListOptionHarness extends MatListItemHarnessBase {
5252
blur(): Promise<void>;
5353
deselect(): Promise<void>;
5454
focus(): Promise<void>;
55-
getCheckboxPosition(): Promise<'before' | 'after'>;
55+
getCheckboxPosition(): Promise<MatListOptionCheckboxPosition>;
5656
isDisabled(): Promise<boolean>;
5757
isFocused(): Promise<boolean>;
5858
isSelected(): Promise<boolean>;

0 commit comments

Comments
 (0)