Skip to content

Commit cc9b530

Browse files
authored
fix(material/schematics): add slider input & change template updates (#25952)
* fix(material/schematics): add slider input & change template updates * fixup! fix(material/schematics): add slider input & change template updates * fixup! fix(material/schematics): add slider input & change template updates * fixup! fix(material/schematics): add slider input & change template updates
1 parent ded4bab commit cc9b530

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

src/material/schematics/ng-generate/mdc-migration/rules/components/slider/slider-template.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,27 @@ describe('slider template migrator', () => {
134134
<mat-slider><input matSliderThumb /></mat-slider>`,
135135
);
136136
});
137+
138+
it('should remap input & output event handlers', async () => {
139+
await runMigrationTest(
140+
`
141+
<mat-slider (input)="myInputHandler($event)"></mat-slider>`,
142+
`
143+
<mat-slider #ngSlider><input matSliderThumb (input)="myInputHandler({source: ngSliderThumb, parent: ngSlider, value: ngSliderThumb.value})" #ngSliderThumb="matSliderThumb" /></mat-slider>`,
144+
);
145+
await runMigrationTest(
146+
`
147+
<mat-slider (change)="myChangeHandler($event)"></mat-slider>`,
148+
`
149+
<mat-slider #ngSlider><input matSliderThumb (change)="myChangeHandler({source: ngSliderThumb, parent: ngSlider, value: ngSliderThumb.value})" #ngSliderThumb="matSliderThumb" /></mat-slider>`,
150+
);
151+
await runMigrationTest(
152+
`
153+
<mat-slider
154+
(input)="myInputHandler($event)"
155+
(change)="myChangeHandler($event)"></mat-slider>`,
156+
`
157+
<mat-slider #ngSlider><input matSliderThumb (input)="myInputHandler({source: ngSliderThumb, parent: ngSlider, value: ngSliderThumb.value})" #ngSliderThumb="matSliderThumb" (change)="myChangeHandler({source: ngSliderThumb, parent: ngSlider, value: ngSliderThumb.value})" /></mat-slider>`,
158+
);
159+
});
137160
});

src/material/schematics/ng-generate/mdc-migration/rules/components/slider/slider-template.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class SliderTemplateMigrator extends TemplateMigrator {
4242
const inputBindings: string[] = [];
4343
const comments: string[] = [];
4444

45+
let alreadyAppendedTemplateVars = false;
46+
4547
for (let i = 0; i < bindings.length; i++) {
4648
const binding = bindings[i];
4749

@@ -71,7 +73,29 @@ export class SliderTemplateMigrator extends TemplateMigrator {
7173
updates.push(this._removeBinding(originalHtml, binding.node));
7274
}
7375

74-
// TODO(wagnermaciel): Finish the remapping of other bindings.
76+
if (binding.name === 'input' || binding.name === 'change') {
77+
// Replace $event with a MatSliderChange object.
78+
const sourceSpan = binding.node.sourceSpan;
79+
const oldBindingStr = originalHtml.slice(
80+
sourceSpan.start.offset,
81+
sourceSpan.end.offset,
82+
);
83+
const newBindingStr = oldBindingStr.replace(
84+
'$event',
85+
'{source: ngSliderThumb, parent: ngSlider, value: ngSliderThumb.value}',
86+
);
87+
88+
// Remove the old binding and add the new binding to the <input>.
89+
inputBindings.push(newBindingStr);
90+
updates.push(this._removeBinding(originalHtml, binding.node));
91+
92+
if (!alreadyAppendedTemplateVars) {
93+
// Add the necessary template vars used in the MatSliderChange object.
94+
inputBindings.push('#ngSliderThumb="matSliderThumb"');
95+
updates.push(this._insertTemplateVar(node, 'ngSlider'));
96+
alreadyAppendedTemplateVars = true;
97+
}
98+
}
7599
}
76100

77101
if (comments.length) {
@@ -94,7 +118,18 @@ export class SliderTemplateMigrator extends TemplateMigrator {
94118
return updates;
95119
}
96120

97-
/** Returns an update the adds the given comments before the given template ast element. */
121+
/** Returns an update that inserts a template var at the end of the given node. */
122+
private _insertTemplateVar(node: compiler.TmplAstElement, varName: string): Update {
123+
return {
124+
offset: node.startSourceSpan.end.offset - 1,
125+
updateFn: html =>
126+
html.slice(0, node.startSourceSpan.end.offset - 1) + // -1 to stop before the closing '>'
127+
` #${varName}` +
128+
html.slice(node.startSourceSpan.end.offset - 1),
129+
};
130+
}
131+
132+
/** Returns an update that adds the given comments before the given template ast element. */
98133
private _addComments(node: compiler.TmplAstElement, comments: string[]): Update {
99134
const whitespace = this._parseIndentation(node);
100135
const indentation = '\n' + this._parseIndentation(node);

src/material/slider/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
export {MatSlider} from './slider';
1010
export {MatSliderVisualThumb} from './slider-thumb';
1111
export {MatSliderThumb, MatSliderRangeThumb} from './slider-input';
12-
export {MatSliderDragEvent} from './slider-interface';
12+
export {MatSliderDragEvent, MatSliderChange} from './slider-interface';
1313
export {MatSliderModule} from './module';

src/material/slider/slider-interface.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ export interface MatSliderDragEvent {
6565
value: number;
6666
}
6767

68+
/**
69+
* A simple change event emitted by the MatSlider component.
70+
* @deprecated Use event bindings directly on the MatSliderThumbs for `change` and `input` events. See https://material.angular.io/guide/mdc-migration for information about migrating.
71+
* @breaking-change 17.0.0
72+
*/
73+
export class MatSliderChange {
74+
/** The MatSliderThumb that was interacted with. */
75+
source: _MatSliderThumb;
76+
77+
/** The MatSlider that was interacted with. */
78+
parent: _MatSlider;
79+
80+
/** The new value of the source slider. */
81+
value: number;
82+
}
83+
6884
export interface _MatSlider {
6985
/** Gets the slider thumb input of the given thumb position. */
7086
_getInput(thumbPosition: _MatThumb): _MatSliderThumb | _MatSliderRangeThumb | undefined;

tools/public_api_guard/material/slider.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ export class MatSlider extends _MatSliderMixinBase implements AfterViewInit, Can
120120
static ɵfac: i0.ɵɵFactoryDeclaration<MatSlider, [null, null, null, null, { optional: true; }, { optional: true; }, { optional: true; }]>;
121121
}
122122

123+
// @public @deprecated
124+
export class MatSliderChange {
125+
parent: _MatSlider;
126+
source: _MatSliderThumb;
127+
value: number;
128+
}
129+
123130
// @public
124131
export interface MatSliderDragEvent {
125132
parent: _MatSlider;

0 commit comments

Comments
 (0)