Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 6b958de

Browse files
authored
feat(linear-progress): Convert JS to TypeScript (#4272)
Refs #4225
1 parent 8addf4d commit 6b958de

File tree

8 files changed

+81
-48
lines changed

8 files changed

+81
-48
lines changed

packages/material-components-web/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import * as formField from '@material/form-field/index';
3333
import * as gridList from '@material/grid-list/index';
3434
import * as iconButton from '@material/icon-button/index';
3535
import * as iconToggle from '@material/icon-toggle/index';
36-
import * as linearProgress from '@material/linear-progress/index';
36+
import * as linearProgress from '@material/linear-progress/index.ts';
3737
import * as lineRipple from '@material/line-ripple/index';
3838
import * as list from '@material/list/index';
3939
import * as menu from '@material/menu/index';

packages/mdc-animation/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ interface JsVendorProperty {
6666
standard: StandardJsEventType;
6767
}
6868

69-
const transformStyleProperties = ['transform', 'WebkitTransform', 'MozTransform', 'OTransform', 'MSTransform'];
70-
7169
// Destructure enum members to make their usages more readable.
7270
const {WEBKIT_ANIMATION, WEBKIT_TRANSFORM, WEBKIT_TRANSITION} = PrefixedCssPropertyName;
7371
const {ANIMATION, TRANSFORM, TRANSITION} = StandardCssPropertyName;
@@ -150,5 +148,4 @@ export {
150148
StandardJsEventType,
151149
getCorrectEventName,
152150
getCorrectPropertyName,
153-
transformStyleProperties,
154151
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
interface MDCLinearProgressAdapter {
25+
addClass(className: string): void;
26+
getBuffer(): HTMLElement | null;
27+
getPrimaryBar(): HTMLElement | null;
28+
hasClass(className: string): boolean;
29+
removeClass(className: string): void;
30+
setStyle(el: HTMLElement, styleProperty: string, value: string): void;
31+
}
32+
33+
export {MDCLinearProgressAdapter};

packages/mdc-linear-progress/constants.js renamed to packages/mdc-linear-progress/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ export const cssClasses = {
2828
};
2929

3030
export const strings = {
31-
PRIMARY_BAR_SELECTOR: '.mdc-linear-progress__primary-bar',
3231
BUFFER_SELECTOR: '.mdc-linear-progress__buffer',
32+
PRIMARY_BAR_SELECTOR: '.mdc-linear-progress__primary-bar',
3333
};

packages/mdc-linear-progress/foundation.js renamed to packages/mdc-linear-progress/foundation.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
import {getCorrectPropertyName, StandardCssPropertyName} from '@material/animation';
2425
import MDCFoundation from '@material/base/foundation';
25-
import {transformStyleProperties} from '@material/animation/index.ts';
26-
26+
import {MDCLinearProgressAdapter} from './adapter';
2727
import {cssClasses, strings} from './constants';
2828

29-
export default class MDCLinearProgressFoundation extends MDCFoundation {
29+
const {TRANSFORM} = StandardCssPropertyName;
30+
31+
export default class MDCLinearProgressFoundation extends MDCFoundation<MDCLinearProgressAdapter> {
3032
static get cssClasses() {
3133
return cssClasses;
3234
}
@@ -35,30 +37,30 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
3537
return strings;
3638
}
3739

38-
static get defaultAdapter() {
40+
static get defaultAdapter(): MDCLinearProgressAdapter {
3941
return {
40-
addClass: (/* className: string */) => {},
41-
getPrimaryBar: () => /* el: Element */ {},
42-
getBuffer: () => /* el: Element */ {},
43-
hasClass: (/* className: string */) => false,
44-
removeClass: (/* className: string */) => {},
45-
setStyle: (/* el: Element, styleProperty: string, value: string */) => {},
42+
addClass: (_className: string) => undefined,
43+
getBuffer: () => null,
44+
getPrimaryBar: () => null,
45+
hasClass: (_className: string) => false,
46+
removeClass: (_className: string) => undefined,
47+
setStyle: (_el: HTMLElement, _styleProperty: string, _value: string) => undefined,
4648
};
4749
}
4850

49-
constructor(adapter) {
50-
super(Object.assign(MDCLinearProgressFoundation.defaultAdapter, adapter));
51-
}
51+
private isDeterminate_: boolean;
52+
private isReversed_: boolean;
53+
private progress_: number;
5254

5355
init() {
54-
this.determinate_ = !this.adapter_.hasClass(cssClasses.INDETERMINATE_CLASS);
55-
this.reverse_ = this.adapter_.hasClass(cssClasses.REVERSED_CLASS);
56+
this.isDeterminate_ = !this.adapter_.hasClass(cssClasses.INDETERMINATE_CLASS);
57+
this.isReversed_ = this.adapter_.hasClass(cssClasses.REVERSED_CLASS);
5658
this.progress_ = 0;
5759
}
5860

59-
setDeterminate(isDeterminate) {
60-
this.determinate_ = isDeterminate;
61-
if (this.determinate_) {
61+
setDeterminate(isDeterminate: boolean) {
62+
this.isDeterminate_ = isDeterminate;
63+
if (this.isDeterminate_) {
6264
this.adapter_.removeClass(cssClasses.INDETERMINATE_CLASS);
6365
this.setScale_(this.adapter_.getPrimaryBar(), this.progress_);
6466
} else {
@@ -68,22 +70,22 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
6870
}
6971
}
7072

71-
setProgress(value) {
73+
setProgress(value: number) {
7274
this.progress_ = value;
73-
if (this.determinate_) {
75+
if (this.isDeterminate_) {
7476
this.setScale_(this.adapter_.getPrimaryBar(), value);
7577
}
7678
}
7779

78-
setBuffer(value) {
79-
if (this.determinate_) {
80+
setBuffer(value: number) {
81+
if (this.isDeterminate_) {
8082
this.setScale_(this.adapter_.getBuffer(), value);
8183
}
8284
}
8385

84-
setReverse(isReversed) {
85-
this.reverse_ = isReversed;
86-
if (this.reverse_) {
86+
setReverse(isReversed: boolean) {
87+
this.isReversed_ = isReversed;
88+
if (this.isReversed_) {
8789
this.adapter_.addClass(cssClasses.REVERSED_CLASS);
8890
} else {
8991
this.adapter_.removeClass(cssClasses.REVERSED_CLASS);
@@ -98,10 +100,11 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
98100
this.adapter_.addClass(cssClasses.CLOSED_CLASS);
99101
}
100102

101-
setScale_(el, scaleValue) {
102-
const value = 'scaleX(' + scaleValue + ')';
103-
transformStyleProperties.forEach((transformStyleProperty) => {
104-
this.adapter_.setStyle(el, transformStyleProperty, value);
105-
});
103+
private setScale_(el: HTMLElement | null, scaleValue: number) {
104+
if (!el) {
105+
return;
106+
}
107+
const value = `scaleX(${scaleValue})`;
108+
this.adapter_.setStyle(el, getCorrectPropertyName(window, TRANSFORM), value);
106109
}
107110
}

packages/mdc-linear-progress/index.js renamed to packages/mdc-linear-progress/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ import MDCLinearProgressFoundation from './foundation';
2626

2727
export {MDCLinearProgressFoundation};
2828

29-
export class MDCLinearProgress extends MDCComponent {
30-
static attachTo(root) {
29+
export class MDCLinearProgress extends MDCComponent<MDCLinearProgressFoundation> {
30+
static attachTo(root: Element) {
3131
return new MDCLinearProgress(root);
3232
}
3333

34-
set determinate(value) {
34+
set determinate(value: boolean) {
3535
this.foundation_.setDeterminate(value);
3636
}
3737

38-
set progress(value) {
38+
set progress(value: number) {
3939
this.foundation_.setProgress(value);
4040
}
4141

42-
set buffer(value) {
42+
set buffer(value: number) {
4343
this.foundation_.setBuffer(value);
4444
}
4545

46-
set reverse(value) {
46+
set reverse(value: boolean) {
4747
this.foundation_.setReverse(value);
4848
}
4949

@@ -57,12 +57,12 @@ export class MDCLinearProgress extends MDCComponent {
5757

5858
getDefaultFoundation() {
5959
return new MDCLinearProgressFoundation({
60-
addClass: (className) => this.root_.classList.add(className),
61-
getPrimaryBar: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.PRIMARY_BAR_SELECTOR),
60+
addClass: (className: string) => this.root_.classList.add(className),
6261
getBuffer: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.BUFFER_SELECTOR),
63-
hasClass: (className) => this.root_.classList.contains(className),
64-
removeClass: (className) => this.root_.classList.remove(className),
65-
setStyle: (el, styleProperty, value) => el.style[styleProperty] = value,
62+
getPrimaryBar: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.PRIMARY_BAR_SELECTOR),
63+
hasClass: (className: string) => this.root_.classList.contains(className),
64+
removeClass: (className: string) => this.root_.classList.remove(className),
65+
setStyle: (el: HTMLElement, styleProperty: string, value: string) => el.style.setProperty(styleProperty, value),
6666
});
6767
}
6868
}

scripts/webpack/js-bundle-factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class JsBundleFactory {
167167
iconToggle: getAbsolutePath('/packages/mdc-icon-toggle/index.js'),
168168
list: getAbsolutePath('/packages/mdc-list/index.js'),
169169
lineRipple: getAbsolutePath('/packages/mdc-line-ripple/index.js'),
170-
linearProgress: getAbsolutePath('/packages/mdc-linear-progress/index.js'),
170+
linearProgress: getAbsolutePath('/packages/mdc-linear-progress/index.ts'),
171171
menu: getAbsolutePath('/packages/mdc-menu/index.js'),
172172
menuSurface: getAbsolutePath('/packages/mdc-menu-surface/index.js'),
173173
notchedOutline: getAbsolutePath('/packages/mdc-notched-outline/index.js'),

test/unit/mdc-linear-progress/mdc-linear-progress.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import {assert} from 'chai';
2525
import bel from 'bel';
2626

27-
import {MDCLinearProgress, MDCLinearProgressFoundation} from '../../../packages/mdc-linear-progress/index';
27+
import {MDCLinearProgress, MDCLinearProgressFoundation} from '../../../packages/mdc-linear-progress/index.ts';
2828

2929
function getFixture() {
3030
return bel`

0 commit comments

Comments
 (0)