Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit a8a8272

Browse files
creismannshahan
authored andcommitted
Add generics support to material_dropdown_select and material_select.
PiperOrigin-RevId: 220226649
1 parent c3a8736 commit a8a8272

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

angular_components/lib/material_select/material_dropdown_select.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,24 @@ import 'package:angular_components/utils/id_generator/id_generator.dart';
7373
NgIf,
7474
PopupSourceDirective,
7575
],
76+
directiveTypes: [
77+
Typed<MaterialSelectDropdownItemComponent<String>>(on: 'deselectItem'),
78+
Typed<MaterialSelectDropdownItemComponent<String>>(on: 'emptyGroupLabel'),
79+
Typed<MaterialSelectDropdownItemComponent>.of([#T]),
80+
],
7681
templateUrl: 'material_dropdown_select.html',
7782
styleUrls: ['material_dropdown_select.scss.css'],
7883
visibility: Visibility.all, // injected by directives
7984
)
80-
class MaterialDropdownSelectComponent extends MaterialSelectBase
85+
class MaterialDropdownSelectComponent<T> extends MaterialSelectBase<T>
8186
with
8287
MaterialDropdownBase,
83-
SelectionInputAdapter,
88+
SelectionInputAdapter<T>,
8489
MaterialButtonWrapper,
8590
TrackLayoutChangesMixin,
8691
KeyboardHandlerMixin,
8792
ActivateItemOnKeyPressMixin,
88-
ShiftClickSelectionMixin
93+
ShiftClickSelectionMixin<T>
8994
implements PopupSizeProvider, OnChanges, OnDestroy {
9095
/// Function for use by NgFor for optionGroup.
9196
///
@@ -112,10 +117,10 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
112117
String buttonAriaLabelledBy;
113118

114119
/// Listener for options changes.
115-
StreamSubscription _optionsListener;
120+
StreamSubscription<List<OptionGroup<T>>> _optionsListener;
116121

117122
/// Listener for selection changes.
118-
StreamSubscription _selectionListener;
123+
StreamSubscription<List<SelectionChangeRecord<T>>> _selectionListener;
119124

120125
/// If a parent provides a [PopupSizeProvider], the provider will be used
121126
/// instead of the implementation of this class.
@@ -223,6 +228,7 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
223228

224229
/// Function to convert an option object to string.
225230
///
231+
// TODO(google): Fix this now that generics are supported.
226232
// Ideally, [value] would be a [ItemRenderer<T>], where T is also the type
227233
// parameter of the SelectionOptions and the SelectionModel, as parent
228234
// components typically use a function that accepts a specific type (T).
@@ -274,7 +280,7 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
274280
}
275281

276282
@override
277-
set options(SelectionOptions newOptions) {
283+
set options(SelectionOptions<T> newOptions) {
278284
super.options = newOptions;
279285

280286
_updateActiveModel();
@@ -308,7 +314,7 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
308314
}
309315

310316
@override
311-
set selection(SelectionModel newSelection) {
317+
set selection(SelectionModel<T> newSelection) {
312318
super.selection = newSelection;
313319
_setInitialActiveItem();
314320

@@ -493,7 +499,7 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
493499
}
494500

495501
/// If selectionOptions implements Selectable, it is called.
496-
bool isOptionDisabled(Object item) {
502+
bool isOptionDisabled(T item) {
497503
// TODO: Verify if this can be simplified to .isDisabledIn.
498504
//
499505
// The prior code did a check for `!= SelectableOption.Selected`. It is
@@ -503,7 +509,7 @@ class MaterialDropdownSelectComponent extends MaterialSelectBase
503509
}
504510

505511
/// Whether to hide [item].
506-
bool isOptionHidden(Object item) {
512+
bool isOptionHidden(T item) {
507513
return Selectable.isHiddenIn(options, item, false);
508514
}
509515

angular_components/lib/material_select/material_dropdown_select.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<ng-content></ng-content>
6363
<div class="options-wrapper" *ngIf="options != null">
6464
<material-select-dropdown-item
65+
#deselectItem
6566
*ngIf="showDeselectItem"
6667
keyboardOnlyFocusIndicator
6768
[class.empty]="options.optionGroups.length == 1"
@@ -103,6 +104,7 @@
103104
</material-select-dropdown-item>
104105
</template>
105106
<material-select-dropdown-item
107+
#emptyGroupLabel
106108
*ngIf="group.isEmpty && group.hasEmptyLabel"
107109
keyboardOnlyFocusIndicator
108110
[tabbable]="false"

angular_components/lib/material_select/material_select.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ import 'material_select_item.dart';
3333
Provider(SelectionContainer, useExisting: MaterialSelectComponent)
3434
],
3535
directives: [MaterialListComponent, MaterialSelectItemComponent, NgIf, NgFor],
36+
directiveTypes: [
37+
Typed<MaterialSelectItemComponent>.of([#T]),
38+
],
3639
templateUrl: 'material_select.html',
3740
styleUrls: ['material_select.scss.css'],
3841
)
39-
class MaterialSelectComponent extends MaterialSelectBase
42+
class MaterialSelectComponent<T> extends MaterialSelectBase<T>
4043
implements HasDisabled {
4144
@HostBinding('attr.role')
4245
static const hostRole = 'listbox';
4346

44-
List<SelectionItem> _selectItems;
47+
List<SelectionItem<T>> _selectItems;
4548

4649
/// Function for use by NgFor for optionGroup to avoid recreating the
4750
/// DOM for the optionGroup.
@@ -56,7 +59,7 @@ class MaterialSelectComponent extends MaterialSelectBase
5659
/// The [SelectionOptions] instance providing options to render.
5760
@Input()
5861
@override
59-
set options(SelectionOptions value) {
62+
set options(SelectionOptions<T> value) {
6063
super.options = value;
6164
}
6265

@@ -85,17 +88,17 @@ class MaterialSelectComponent extends MaterialSelectBase
8588
/// The [SelectionModel] for this container.
8689
@Input()
8790
@override
88-
set selection(value) {
91+
set selection(SelectionModel<T> value) {
8992
super.selection = value;
9093
_refreshItems();
9194
}
9295

9396
@override
94-
SelectionModel get selection => super.selection;
97+
SelectionModel<T> get selection => super.selection;
9598

9699
/// If selectionOptions implements Selectable, it is called to decided
97100
/// whether an item is disabled.
98-
bool isOptionDisabled(Object item) {
101+
bool isOptionDisabled(T item) {
99102
// TODO: Verify if this can be simplified to .isDisabledIn.
100103
//
101104
// The prior code did a check for `!= SelectableOption.Selected`. It is
@@ -118,19 +121,19 @@ class MaterialSelectComponent extends MaterialSelectBase
118121
String get disabledStr => '$_disabled';
119122

120123
@override
121-
ItemRenderer get itemRenderer => _itemRenderer;
122-
ItemRenderer _itemRenderer;
124+
ItemRenderer<T> get itemRenderer => _itemRenderer;
125+
ItemRenderer<T> _itemRenderer;
123126

124127
/// A rendering function to render selection options to a String, if given a
125128
/// `value`.
126129
@Input()
127-
set itemRenderer(ItemRenderer renderer) {
130+
set itemRenderer(ItemRenderer<T> renderer) {
128131
_itemRenderer = renderer;
129132
_refreshItems();
130133
}
131134

132135
@ContentChildren(SelectionItem)
133-
set selectItems(List<SelectionItem> value) {
136+
set selectItems(List<SelectionItem<T>> value) {
134137
if (value != null) {
135138
// ContentChildren call is inside change detection. We can't alter
136139
// state inside change detector therefore schedule a microtask.
@@ -144,12 +147,12 @@ class MaterialSelectComponent extends MaterialSelectBase
144147
void _refreshItems() {
145148
if (_selectItems == null) return;
146149
if (selection != null) {
147-
for (SelectionItem item in _selectItems) {
150+
for (SelectionItem<T> item in _selectItems) {
148151
item.selection = selection;
149152
}
150153
}
151154
if (itemRenderer != null) {
152-
for (SelectionItem item in _selectItems) {
155+
for (SelectionItem<T> item in _selectItems) {
153156
item.itemRenderer = itemRenderer;
154157
}
155158
}

0 commit comments

Comments
 (0)