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

feat: Convert packages to TypeScript #4451

Merged
merged 74 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
8bcf546
feat(infrastructure): add tsconfig, tslint, and build setup (#4241)
Jan 18, 2019
1600b54
docs: Update closure-compiler.md to reflect TypeScript conversion (#4…
kfranqueiro Jan 18, 2019
66c8d81
feat(dom): Convert JS to TypeScript (#4269)
acdvorak Jan 19, 2019
87a5e8e
feat(animation): Convert JS to TypeScript (#4271)
acdvorak Jan 23, 2019
8addf4d
Merge remote-tracking branch 'origin' into feat/typescript
acdvorak Jan 23, 2019
6b958de
feat(linear-progress): Convert JS to TypeScript (#4272)
acdvorak Jan 23, 2019
c649de5
fix: add strict true to tsconfig (#4286)
Jan 24, 2019
47a9c38
feat(ripple): convert to ts (#4300)
Jan 29, 2019
54d25c1
Merge remote-tracking branch 'origin/master' into feat/typescript
Jan 31, 2019
aeb3f87
feat(form-field): ts convert (#4327)
Jan 31, 2019
8abbb53
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Jan 31, 2019
13d169d
feat(switch): Convert JS to TypeScript (#4320)
mmalerba Jan 31, 2019
869e44c
refactor(animation): Replace enums with string literal type aliases (…
acdvorak Jan 31, 2019
a2ac8bc
feat(menu-surface): Convert JS to TypeScript (#4273)
acdvorak Feb 5, 2019
0f8a271
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 5, 2019
2befa88
feat(checkbox): typescript conversion (#4319)
Feb 5, 2019
90f5f05
feat(radio): ts conversion (#4329)
Feb 6, 2019
0f354ae
feat(base): Add generic type signatures to listen/unlisten/emit (#4358)
acdvorak Feb 6, 2019
f595a99
feat(icon-button): Convert to TypeScript (#4325)
abhiomkar Feb 6, 2019
0b28992
feat(list): ts conversion (#4334)
Feb 6, 2019
dc4e92c
feat(menu): Convert JS to TypeScript (#4342)
acdvorak Feb 6, 2019
33e26d7
feat(dialog): ts conversion (#4333)
Feb 7, 2019
3279cc9
style: Make TS import and export syntax consistent (#4360)
acdvorak Feb 7, 2019
1c7335b
feat(chips): ts conversion (#4332)
Feb 7, 2019
ed7f8d7
feat(grid-list): ts conversion (#4337)
Feb 7, 2019
d4250ca
docs: update best coding practices with typescript lessons learned (#…
Feb 7, 2019
9936f01
feat(ripple): updated ripple ts rewrite to remove extraneous code/typ…
Feb 7, 2019
77e8bab
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 7, 2019
5407ada
feat(floating-label): Convert JS to TypeScript (#4374)
acdvorak Feb 8, 2019
50eb8c7
feat(line-ripple): Convert JS to TypeScript (#4373)
acdvorak Feb 8, 2019
197f7fe
feat(notched-outline): Convert JS to TypeScript (#4375)
acdvorak Feb 8, 2019
08fb283
feat(slider): Convert JS to TypeScript (#4378)
acdvorak Feb 8, 2019
e95ff8a
feat(snackbar): Convert JS to TypeScript (#4363)
acdvorak Feb 9, 2019
b859440
chore: Remove Object.assign from TS files (#4389)
acdvorak Feb 11, 2019
e8c947c
Merge branch 'master' into feat/typescript
acdvorak Feb 11, 2019
6fce645
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 11, 2019
9526da2
Merge branch 'feat/typescript' of github.com:material-components/mate…
acdvorak Feb 11, 2019
667aeee
feat(tab-scroller): Convert JS to TypeScript (#4392)
acdvorak Feb 12, 2019
5f50d60
feat(tab-indicator): Convert JS to TypeScript (#4391)
acdvorak Feb 12, 2019
ab9879d
feat(tab): Convert JS to TypeScript (#4393)
acdvorak Feb 12, 2019
b2fa9a5
feat(tab-bar): Convert JS to TypeScript (#4394)
acdvorak Feb 12, 2019
976352c
feat(text-field): Convert JS to TypeScript (#4377)
acdvorak Feb 13, 2019
5052ada
feat(select): Convert JS to TypeScript (#4386)
acdvorak Feb 13, 2019
f8ba48f
feat(drawer): Convert JS to TypeScript (#4390)
acdvorak Feb 14, 2019
b8b1988
feat(top-app-bar): Convert JS to TypeScript (#4397)
acdvorak Feb 14, 2019
4675c95
feat(auto-init): Convert JS to TypeScript (#4395)
acdvorak Feb 14, 2019
6136db3
feat(toolbar): Convert JS to TypeScript (#4410)
acdvorak Feb 14, 2019
db6fc6d
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 14, 2019
6d6bff8
WIP: Fix borked merge
acdvorak Feb 14, 2019
ebefb78
feat(package): Convert JS to TypeScript (#4396)
acdvorak Feb 14, 2019
ff5ad6a
chore: Fix broken TypeScript rewrite script (#4398)
acdvorak Feb 14, 2019
96afbb1
WIP: Remove `icon-toggle` from `npm run test:dependency`
acdvorak Feb 14, 2019
36ce7b9
WIP: Fix GitHub authentication issue for screenshot tests on Travis CI
acdvorak Feb 15, 2019
03b791d
fix(chips): Reference ripple/index in types for consistency (#4417)
kfranqueiro Feb 15, 2019
409a6a6
feat(tabs): Convert JS to TypeScript (#4412)
acdvorak Feb 15, 2019
1ec12b6
chore: remove icon-toggle (#4415)
Feb 15, 2019
72e8b66
refactor: Make TS easier to wrap (#4407)
acdvorak Feb 21, 2019
c487445
WIP: Add comment
acdvorak Feb 22, 2019
f441f8f
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 22, 2019
cabeabc
chore(package): Remove need for selection-control (#4442)
kfranqueiro Feb 22, 2019
b6b6983
chore: Fix internal tslint errors (#4440)
acdvorak Feb 22, 2019
6faaabe
chore: Fix regression in `MDCChipFoundation.getDimensions()` (#4443)
acdvorak Feb 23, 2019
2bbcfea
WIP: Fix internal tslint error
acdvorak Feb 23, 2019
c6b609c
feat(infrastructure): add esmodules support for treeshaking (#4409)
Feb 26, 2019
ad96947
style: Add explanations to `tslint:disable` comments (#4448)
acdvorak Feb 26, 2019
21b2403
WIP: Update `.ts` file paths in READMEs
acdvorak Feb 26, 2019
c54310c
WIP: Export `MDCComponentClass` in `mdc-auto-init`
acdvorak Feb 26, 2019
cbb0fe6
WIP: Change `any` to `unknown` and remove `tslint:disable` comment
acdvorak Feb 26, 2019
f3a09a1
Merge remote-tracking branch 'origin/master' into feat/typescript
acdvorak Feb 26, 2019
216d975
WIP: Revert changes to logging
acdvorak Feb 20, 2019
d349fea
style: grammar mistakes on script comments (#4452)
Feb 26, 2019
8cd8b26
WIP autoinit: MDCComponentClass -> MDCAttachable
Feb 27, 2019
60c9498
WIP Remove object-assign polyfill
Feb 27, 2019
2abfb30
WIP Revert "WIP Remove object-assign polyfill"
Feb 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(switch): Convert JS to TypeScript (#4320)
  • Loading branch information
mmalerba authored and kfranqueiro committed Jan 31, 2019
commit 13d169dbd8dec5ca294232f6155f3562754c1777
2 changes: 1 addition & 1 deletion packages/material-components-web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import * as select from '@material/select/index';
import * as selectionControl from '@material/selection-control/index.ts';
import * as slider from '@material/slider/index';
import * as snackbar from '@material/snackbar/index';
import * as switchControl from '@material/switch/index';
import * as switchControl from '@material/switch/index.ts';
import * as tab from '@material/tab/index';
import * as tabBar from '@material/tab-bar/index';
import * as tabIndicator from '@material/tab-indicator/index';
Expand Down
2 changes: 1 addition & 1 deletion packages/mdc-base/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ class MDCComponent<FoundationType extends MDCFoundation> {
}
}

export default MDCComponent;
export {MDCComponent as default, MDCComponent};
2 changes: 1 addition & 1 deletion packages/mdc-base/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class MDCFoundation<AdapterType extends {} = {}> {
}
}

export default MDCFoundation;
export {MDCFoundation as default, MDCFoundation};
2 changes: 1 addition & 1 deletion packages/mdc-switch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ If you are using a JavaScript framework, such as React or Angular, you can creat
| `handleChange(evt: Event) => void` | Handles a change event from the native control. |

### `MDCSwitchFoundation` Event Handlers
If wrapping the switch component it is necessary to add an event handler for native control change events that calls the `handleChange` foundation method. For an example of this, see the [MDCSwitch](index.js) component `initialSyncWithDOM` method.
If wrapping the switch component it is necessary to add an event handler for native control change events that calls the `handleChange` foundation method. For an example of this, see the [MDCSwitch](index.ts) component `initialSyncWithDOM` method.
acdvorak marked this conversation as resolved.
Show resolved Hide resolved

| Event | Element Selector | Foundation Handler |
| --- | --- | --- |
Expand Down
27 changes: 10 additions & 17 deletions packages/mdc-switch/adapter.js → packages/mdc-switch/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,28 @@
* THE SOFTWARE.
*/

/* eslint no-unused-vars: [2, {"args": "none"}] */

/**
* Adapter for MDC Switch. Provides an interface for managing
* - classes
* - dom
*
* Additionally, provides type information for the adapter to the Closure
* compiler.
*
* Implement this adapter for your framework of choice to delegate updates to
* the component in your framework of choice. See architecture documentation
* for more details.
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
*
* @record
*/
class MDCSwitchAdapter {
/** @param {string} className */
addClass(className) {}
interface MDCSwitchAdapter {
/** Adds a CSS class to the root element. */
addClass(className: string): void;

/** @param {string} className */
removeClass(className) {}
/** Removes a CSS class from the root element. */
removeClass(className: string): void;

/** @param {boolean} checked */
setNativeControlChecked(checked) {}
/** Sets checked state of the native HTML control underlying the switch. */
setNativeControlChecked(checked: boolean): void;

/** @param {boolean} disabled */
setNativeControlDisabled(disabled) {}
/** Sets the disabled state of the native HTML control underlying the switch. */
setNativeControlDisabled(disabled: boolean): void;
}

export default MDCSwitchAdapter;
export {MDCSwitchAdapter as default, MDCSwitchAdapter};
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
* THE SOFTWARE.
*/

/** @enum {string} */
/** CSS classes used by the switch. */
const cssClasses = {
/** Class used for a switch that is in the "checked" (on) position. */
CHECKED: 'mdc-switch--checked',
/** Class used for a switch that is disabled. */
DISABLED: 'mdc-switch--disabled',
};

/** @enum {string} */
/** String constants used by the switch. */
const strings = {
/** A CSS selector used to locate the native HTML control for the switch. */
NATIVE_CONTROL_SELECTOR: '.mdc-switch__native-control',
/** A CSS selector used to locate the ripple surface element for the switch. */
RIPPLE_SURFACE_SELECTOR: '.mdc-switch__thumb-underlay',
};


export {cssClasses, strings};
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,49 @@
* THE SOFTWARE.
*/

import MDCFoundation from '@material/base/foundation';
import MDCSwitchAdapter from './adapter';
/* eslint-enable no-unused-vars */
import {MDCFoundation} from '@material/base/foundation';
import {MDCSwitchAdapter} from './adapter';
import {cssClasses, strings} from './constants';

/**
* @extends {MDCFoundation<!MDCSwitchAdapter>}
* Foundation for the MDC Switch. Encapsulates business logic for the switch.
*
* See architecture documentation for more details.
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
*/
class MDCSwitchFoundation extends MDCFoundation {
/** @return enum {string} */
class MDCSwitchFoundation extends MDCFoundation<MDCSwitchAdapter> {
/** The string constants used by the switch. */
static get strings() {
return strings;
}

/** @return enum {string} */
/** The CSS classes used by the switch. */
static get cssClasses() {
return cssClasses;
}

/** @return {!MDCSwitchAdapter} */
static get defaultAdapter() {
return /** @type {!MDCSwitchAdapter} */ ({
addClass: (/* className: string */) => {},
removeClass: (/* className: string */) => {},
setNativeControlChecked: (/* checked: boolean */) => {},
setNativeControlDisabled: (/* disabled: boolean */) => {},
});
/** The default Adapter for the switch. */
static get defaultAdapter(): MDCSwitchAdapter {
return {
addClass: () => undefined,
removeClass: () => undefined,
setNativeControlChecked: () => undefined,
setNativeControlDisabled: () => undefined,
};
}

constructor(adapter) {
constructor(adapter: MDCSwitchAdapter) {
super(Object.assign(MDCSwitchFoundation.defaultAdapter, adapter));
}

/** @param {boolean} checked */
setChecked(checked) {
/** Sets the checked state of the switch. */
setChecked(checked: boolean) {
this.adapter_.setNativeControlChecked(checked);
this.updateCheckedStyling_(checked);
}

/** @param {boolean} disabled */
setDisabled(disabled) {
/** Sets the disabled state of the switch. */
setDisabled(disabled: boolean) {
this.adapter_.setNativeControlDisabled(disabled);
if (disabled) {
this.adapter_.addClass(cssClasses.DISABLED);
Expand All @@ -70,20 +72,14 @@ class MDCSwitchFoundation extends MDCFoundation {
}
}

/**
* Handles the change event for the switch native control.
* @param {!Event} evt
*/
handleChange(evt) {
this.updateCheckedStyling_(evt.target.checked);
/** Handles the change event for the switch native control. */
handleChange(evt: Event) {
const nativeControl = evt.target as HTMLInputElement;
this.updateCheckedStyling_(nativeControl.checked);
}

/**
* Updates the styling of the switch based on its checked state.
* @param {boolean} checked
* @private
*/
updateCheckedStyling_(checked) {
/** Updates the styling of the switch based on its checked state. */
private updateCheckedStyling_(checked: boolean) {
if (checked) {
this.adapter_.addClass(cssClasses.CHECKED);
} else {
Expand All @@ -92,4 +88,4 @@ class MDCSwitchFoundation extends MDCFoundation {
}
}

export default MDCSwitchFoundation;
export {MDCSwitchFoundation as default, MDCSwitchFoundation};
117 changes: 52 additions & 65 deletions packages/mdc-switch/index.js → packages/mdc-switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,30 @@
* THE SOFTWARE.
*/

import MDCComponent from '@material/base/component';
/* eslint-disable no-unused-vars */
import {MDCSelectionControlState, MDCSelectionControl} from '@material/selection-control/index';
/* eslint-enable no-unused-vars */
import MDCSwitchFoundation from './foundation';
import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
import {MDCComponent} from '@material/base/component';
import {MDCRipple, MDCRippleFoundation, RippleCapableSurface} from '@material/ripple/index';
import {getMatchesProperty} from '@material/ripple/util';
import {MDCSelectionControl} from '@material/selection-control/index';
import {MDCSwitchFoundation} from './foundation';

/**
* @extends MDCComponent<!MDCSwitchFoundation>
* @implements {MDCSelectionControl}
* An implementation of the switch component defined by the Material Design spec.
*
* https://material.io/design/components/selection-controls.html#switches
*/
class MDCSwitch extends MDCComponent {
static attachTo(root) {
class MDCSwitch extends MDCComponent<MDCSwitchFoundation> implements MDCSelectionControl, RippleCapableSurface {
/** Creates an instance of MDCSwitch bound to the given root element. */
static attachTo(root: HTMLElement) {
return new MDCSwitch(root);
}

constructor(...args) {
super(...args);
// Initialized in super class constructor, re-declared as public to fulfill to the `RippleCapableSurface` interface.
root_!: Element;

/** @private {!MDCRipple} */
this.ripple_ = this.initRipple_();
private ripple_ = this.initRipple_();

/** @private {!Function} */
this.changeHandler_;
}
// Initialized in `initialSyncWithDOM`.
private changeHandler_!: EventListener;

destroy() {
super.destroy();
Expand All @@ -55,7 +53,7 @@ class MDCSwitch extends MDCComponent {
}

initialSyncWithDOM() {
this.changeHandler_ = this.foundation_.handleChange.bind(this.foundation_);
this.changeHandler_ = (...args) => this.foundation_.handleChange(...args);
this.nativeControl_.addEventListener('change', this.changeHandler_);

// Sometimes the checked state of the input element is saved in the history.
Expand All @@ -64,75 +62,64 @@ class MDCSwitch extends MDCComponent {
this.checked = this.checked;
}

/**
* Returns the state of the native control element, or null if the native control element is not present.
* @return {?MDCSelectionControlState}
* @private
*/
get nativeControl_() {
const {NATIVE_CONTROL_SELECTOR} = MDCSwitchFoundation.strings;
const el = /** @type {?MDCSelectionControlState} */ (
this.root_.querySelector(NATIVE_CONTROL_SELECTOR));
return el;
}

/**
* @return {!MDCRipple}
* @private
*/
initRipple_() {
const {RIPPLE_SURFACE_SELECTOR} = MDCSwitchFoundation.strings;
const rippleSurface = /** @type {!Element} */ (this.root_.querySelector(RIPPLE_SURFACE_SELECTOR));

const MATCHES = getMatchesProperty(HTMLElement.prototype);
const adapter = Object.assign(MDCRipple.createAdapter(this), {
isUnbounded: () => true,
isSurfaceActive: () => this.nativeControl_[MATCHES](':active'),
addClass: (className) => rippleSurface.classList.add(className),
removeClass: (className) => rippleSurface.classList.remove(className),
registerInteractionHandler: (type, handler) => this.nativeControl_.addEventListener(type, handler),
deregisterInteractionHandler: (type, handler) => this.nativeControl_.removeEventListener(type, handler),
updateCssVariable: (varName, value) => rippleSurface.style.setProperty(varName, value),
computeBoundingRect: () => rippleSurface.getBoundingClientRect(),
});
const foundation = new MDCRippleFoundation(adapter);
return new MDCRipple(this.root_, foundation);
}

/** @return {!MDCSwitchFoundation} */
/** Gets the default Foundation for this switch. */
getDefaultFoundation() {
return new MDCSwitchFoundation({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
setNativeControlChecked: (checked) => this.nativeControl_.checked = checked,
setNativeControlDisabled: (disabled) => this.nativeControl_.disabled = disabled,
addClass: (className: string) => this.root_.classList.add(className),
removeClass: (className: string) => this.root_.classList.remove(className),
setNativeControlChecked: (checked: boolean) => this.nativeControl_.checked = checked,
setNativeControlDisabled: (disabled: boolean) => this.nativeControl_.disabled = disabled,
});
}

/** @return {!MDCRipple} */
/** The MDCRipple associated with this switch. */
get ripple() {
return this.ripple_;
}

/** @return {boolean} */
/** The checked state of this switch. */
get checked() {
return this.nativeControl_.checked;
}

/** @param {boolean} checked */
set checked(checked) {
this.foundation_.setChecked(checked);
}

/** @return {boolean} */
/** The disabled state of this switch. */
get disabled() {
return this.nativeControl_.disabled;
}

/** @param {boolean} disabled */
set disabled(disabled) {
this.foundation_.setDisabled(disabled);
}

private initRipple_() {
const {RIPPLE_SURFACE_SELECTOR} = MDCSwitchFoundation.strings;
const rippleSurface = this.root_.querySelector(RIPPLE_SURFACE_SELECTOR) as HTMLElement;

const MATCHES = getMatchesProperty(HTMLElement.prototype);
const adapter = Object.assign(MDCRipple.createAdapter(this), {
addClass: (className: string) => rippleSurface.classList.add(className),
computeBoundingRect: () => rippleSurface.getBoundingClientRect(),
deregisterInteractionHandler: (type: string, handler: EventListener) =>
this.nativeControl_.removeEventListener(type, handler),
isSurfaceActive: () => this.nativeControl_[MATCHES]!(':active'),
isUnbounded: () => true,
registerInteractionHandler: (type: string, handler: EventListener) =>
this.nativeControl_.addEventListener(type, handler),
removeClass: (className: string) => rippleSurface.classList.remove(className),
updateCssVariable: (varName: string, value: string) =>
rippleSurface.style.setProperty(varName, value),
});
const foundation = new MDCRippleFoundation(adapter);
return new MDCRipple(this.root_, foundation);
}

/** Returns the state of the native control element. */
private get nativeControl_() {
const {NATIVE_CONTROL_SELECTOR} = MDCSwitchFoundation.strings;
return this.root_.querySelector(NATIVE_CONTROL_SELECTOR) as HTMLInputElement;
}
}

export {MDCSwitchFoundation, MDCSwitch};
2 changes: 1 addition & 1 deletion scripts/webpack/js-bundle-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class JsBundleFactory {
selectionControl: getAbsolutePath('/packages/mdc-selection-control/index.ts'),
slider: getAbsolutePath('/packages/mdc-slider/index.js'),
snackbar: getAbsolutePath('/packages/mdc-snackbar/index.js'),
switch: getAbsolutePath('/packages/mdc-switch/index.js'),
switch: getAbsolutePath('/packages/mdc-switch/index.ts'),
tab: getAbsolutePath('/packages/mdc-tab/index.js'),
tabBar: getAbsolutePath('/packages/mdc-tab-bar/index.js'),
tabIndicator: getAbsolutePath('/packages/mdc-tab-indicator/index.js'),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mdc-switch/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import {assert} from 'chai';
import td from 'testdouble';

import MDCSwitchFoundation from '../../../packages/mdc-switch/foundation';
import {MDCSwitchFoundation} from '../../../packages/mdc-switch/foundation';

suite('MDCSwitchFoundation');

Expand Down