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

Commit 90f5f05

Browse files
author
Matt Goo
authored
feat(radio): ts conversion (#4329)
1 parent 2befa88 commit 90f5f05

File tree

14 files changed

+75
-125
lines changed

14 files changed

+75
-125
lines changed

packages/mdc-checkbox/adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ interface MDCCheckboxAdapter {
5757
setNativeControlDisabled(disabled: boolean): void;
5858
}
5959

60-
export default MDCCheckboxAdapter;
60+
export {MDCCheckboxAdapter as default, MDCCheckboxAdapter};

packages/mdc-checkbox/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,4 @@ class MDCCheckboxFoundation extends MDCFoundation<MDCCheckboxAdapter> {
194194
}
195195
}
196196

197-
export default MDCCheckboxFoundation;
197+
export {MDCCheckboxFoundation as default, MDCCheckboxFoundation};

packages/mdc-linear-progress/adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ interface MDCLinearProgressAdapter {
3030
setStyle(el: HTMLElement, styleProperty: string, value: string): void;
3131
}
3232

33-
export {MDCLinearProgressAdapter};
33+
export {MDCLinearProgressAdapter as default, MDCLinearProgressAdapter};

packages/mdc-linear-progress/foundation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import MDCFoundation from '@material/base/foundation';
2626
import {MDCLinearProgressAdapter} from './adapter';
2727
import {cssClasses, strings} from './constants';
2828

29-
export default class MDCLinearProgressFoundation extends MDCFoundation<MDCLinearProgressAdapter> {
29+
class MDCLinearProgressFoundation extends MDCFoundation<MDCLinearProgressAdapter> {
3030
static get cssClasses() {
3131
return cssClasses;
3232
}
@@ -106,3 +106,5 @@ export default class MDCLinearProgressFoundation extends MDCFoundation<MDCLinear
106106
this.adapter_.setStyle(el, getCorrectPropertyName(window, 'transform'), value);
107107
}
108108
}
109+
110+
export {MDCLinearProgressFoundation as default, MDCLinearProgressFoundation};

packages/mdc-menu-surface/adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ interface MDCMenuSurfaceAdapter {
6969
notifyOpen(): void;
7070
}
7171

72-
export {MDCMenuSurfaceAdapter};
72+
export {MDCMenuSurfaceAdapter as default, MDCMenuSurfaceAdapter};

packages/mdc-menu-surface/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,4 @@ class MDCMenuSurfaceFoundation extends MDCFoundation<MDCMenuSurfaceAdapter> {
500500
}
501501
}
502502

503-
export {MDCMenuSurfaceFoundation};
503+
export {MDCMenuSurfaceFoundation as default, MDCMenuSurfaceFoundation};

packages/mdc-radio/adapter.js renamed to packages/mdc-radio/adapter.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,20 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/* eslint-disable no-unused-vars */
25-
import {MDCSelectionControlState} from '@material/selection-control/index';
26-
27-
/* eslint no-unused-vars: [2, {"args": "none"}] */
28-
2924
/**
3025
* Adapter for MDC Radio. Provides an interface for managing
31-
* - classes
32-
* - dom
33-
*
34-
* Additionally, provides type information for the adapter to the Closure
35-
* compiler.
3626
*
3727
* Implement this adapter for your framework of choice to delegate updates to
3828
* the component in your framework of choice. See architecture documentation
3929
* for more details.
4030
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
4131
*
42-
* @record
4332
*/
44-
class MDCRadioAdapter {
45-
/** @param {string} className */
46-
addClass(className) {}
47-
48-
/** @param {string} className */
49-
removeClass(className) {}
5033

51-
/** @param {boolean} disabled */
52-
setNativeControlDisabled(disabled) {}
34+
interface MDCRadioAdapter {
35+
addClass(className: string): void;
36+
removeClass(className: string): void;
37+
setNativeControlDisabled(disabled: boolean): void;
5338
}
5439

55-
export default MDCRadioAdapter;
40+
export {MDCRadioAdapter as default, MDCRadioAdapter};

packages/mdc-radio/constants.js renamed to packages/mdc-radio/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/** @enum {string} */
2524
const strings = {
2625
NATIVE_CONTROL_SELECTOR: '.mdc-radio__native-control',
2726
};
2827

29-
/** @enum {string} */
3028
const cssClasses = {
31-
ROOT: 'mdc-radio',
3229
DISABLED: 'mdc-radio--disabled',
30+
ROOT: 'mdc-radio',
3331
};
3432

3533
export {strings, cssClasses};

packages/mdc-radio/foundation.js renamed to packages/mdc-radio/foundation.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,29 @@
2222
*/
2323

2424
import MDCFoundation from '@material/base/foundation';
25-
/* eslint-disable no-unused-vars */
26-
import {MDCSelectionControlState} from '@material/selection-control/index';
27-
import MDCRadioAdapter from './adapter';
28-
/* eslint-enable no-unused-vars */
25+
26+
import {MDCRadioAdapter} from './adapter';
2927
import {cssClasses, strings} from './constants';
3028

31-
/**
32-
* @extends {MDCFoundation<!MDCRadioAdapter>}
33-
*/
34-
class MDCRadioFoundation extends MDCFoundation {
35-
/** @return enum {cssClasses} */
29+
class MDCRadioFoundation extends MDCFoundation<MDCRadioAdapter> {
30+
3631
static get cssClasses() {
3732
return cssClasses;
3833
}
3934

40-
/** @return enum {strings} */
4135
static get strings() {
4236
return strings;
4337
}
4438

45-
/** @return {!MDCRadioAdapter} */
46-
static get defaultAdapter() {
47-
return /** @type {!MDCRadioAdapter} */ ({
48-
addClass: (/* className: string */) => {},
49-
removeClass: (/* className: string */) => {},
50-
setNativeControlDisabled: (/* disabled: boolean */) => {},
51-
});
39+
static get defaultAdapter(): MDCRadioAdapter {
40+
return {
41+
addClass: () => undefined,
42+
removeClass: () => undefined,
43+
setNativeControlDisabled: () => undefined,
44+
};
5245
}
5346

54-
/** @param {boolean} disabled */
55-
setDisabled(disabled) {
47+
setDisabled(disabled: boolean) {
5648
const {DISABLED} = MDCRadioFoundation.cssClasses;
5749
this.adapter_.setNativeControlDisabled(disabled);
5850
if (disabled) {
@@ -63,4 +55,4 @@ class MDCRadioFoundation extends MDCFoundation {
6355
}
6456
}
6557

66-
export default MDCRadioFoundation;
58+
export {MDCRadioFoundation as default, MDCRadioFoundation};

packages/mdc-radio/index.js renamed to packages/mdc-radio/index.ts

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,106 +22,92 @@
2222
*/
2323

2424
import MDCComponent from '@material/base/component';
25-
/* eslint-disable no-unused-vars */
26-
import {MDCSelectionControlState, MDCSelectionControl} from '@material/selection-control/index';
27-
/* eslint-enable no-unused-vars */
28-
import MDCRadioFoundation from './foundation';
25+
import {EventType, SpecificEventListener} from '@material/dom/index';
26+
import {RippleCapableSurface} from '@material/ripple/index';
2927
import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
28+
import {MDCSelectionControl} from '@material/selection-control/index';
3029

31-
/**
32-
* @extends MDCComponent<!MDCRadioFoundation>
33-
* @implements {MDCSelectionControl}
34-
*/
35-
class MDCRadio extends MDCComponent {
36-
static attachTo(root) {
30+
import MDCRadioFoundation from './foundation';
31+
32+
class MDCRadio extends MDCComponent<MDCRadioFoundation> implements RippleCapableSurface, MDCSelectionControl {
33+
34+
static attachTo(root: Element) {
3735
return new MDCRadio(root);
3836
}
3937

40-
/** @return {boolean} */
41-
get checked() {
38+
// Public visibility for this property is required by RippleCapableSurface.
39+
root_!: Element; // assigned in MDCComponent constructor
40+
41+
private readonly ripple_: MDCRipple = this.initRipple_();
42+
43+
get checked(): boolean {
4244
return this.nativeControl_.checked;
4345
}
4446

45-
/** @param {boolean} checked */
46-
set checked(checked) {
47+
set checked(checked: boolean) {
4748
this.nativeControl_.checked = checked;
4849
}
4950

50-
/** @return {boolean} */
5151
get disabled() {
5252
return this.nativeControl_.disabled;
5353
}
5454

55-
/** @param {boolean} disabled */
56-
set disabled(disabled) {
55+
set disabled(disabled: boolean) {
5756
this.foundation_.setDisabled(disabled);
5857
}
5958

60-
/** @return {?string} */
6159
get value() {
6260
return this.nativeControl_.value;
6361
}
6462

65-
/** @param {?string} value */
66-
set value(value) {
63+
set value(value: string) {
6764
this.nativeControl_.value = value;
6865
}
6966

70-
/** @return {!MDCRipple} */
71-
get ripple() {
67+
get ripple(): MDCRipple {
7268
return this.ripple_;
7369
}
7470

75-
constructor(...args) {
76-
super(...args);
71+
destroy() {
72+
this.ripple_.destroy();
73+
super.destroy();
74+
}
7775

78-
/** @private {!MDCRipple} */
79-
this.ripple_ = this.initRipple_();
76+
getDefaultFoundation() {
77+
return new MDCRadioFoundation({
78+
addClass: (className) => this.root_.classList.add(className),
79+
removeClass: (className) => this.root_.classList.remove(className),
80+
setNativeControlDisabled: (disabled) => this.nativeControl_.disabled = disabled,
81+
});
8082
}
8183

82-
/**
83-
* @return {!MDCRipple}
84-
* @private
85-
*/
86-
initRipple_() {
84+
private initRipple_(): MDCRipple {
8785
const adapter = Object.assign(MDCRipple.createAdapter(this), {
88-
isUnbounded: () => true,
86+
deregisterInteractionHandler:
87+
<K extends EventType>(type: K, handler: SpecificEventListener<K>) =>
88+
this.nativeControl_.removeEventListener(type, handler),
8989
// Radio buttons technically go "active" whenever there is *any* keyboard interaction. This is not the
9090
// UI we desire.
9191
isSurfaceActive: () => false,
92-
registerInteractionHandler: (type, handler) => this.nativeControl_.addEventListener(type, handler),
93-
deregisterInteractionHandler: (type, handler) => this.nativeControl_.removeEventListener(type, handler),
92+
isUnbounded: () => true,
93+
registerInteractionHandler: <K extends EventType>(type: K, handler: SpecificEventListener<K>) =>
94+
this.nativeControl_.addEventListener(type, handler),
9495
});
9596
const foundation = new MDCRippleFoundation(adapter);
9697
return new MDCRipple(this.root_, foundation);
9798
}
9899

99100
/**
100101
* Returns the state of the native control element, or null if the native control element is not present.
101-
* @return {?MDCSelectionControlState}
102-
* @private
103102
*/
104-
get nativeControl_() {
103+
private get nativeControl_(): HTMLInputElement {
105104
const {NATIVE_CONTROL_SELECTOR} = MDCRadioFoundation.strings;
106-
const el = /** @type {?MDCSelectionControlState} */ (
107-
this.root_.querySelector(NATIVE_CONTROL_SELECTOR));
105+
const el = this.root_.querySelector<HTMLInputElement>(NATIVE_CONTROL_SELECTOR);
106+
if (!el) {
107+
throw new Error(`Radio component requires a ${NATIVE_CONTROL_SELECTOR} element`);
108+
}
108109
return el;
109110
}
110-
111-
destroy() {
112-
this.ripple_.destroy();
113-
super.destroy();
114-
}
115-
116-
/** @return {!MDCRadioFoundation} */
117-
getDefaultFoundation() {
118-
return new MDCRadioFoundation({
119-
addClass: (className) => this.root_.classList.add(className),
120-
removeClass: (className) => this.root_.classList.remove(className),
121-
setNativeControlDisabled: (disabled) => this.nativeControl_.disabled = disabled,
122-
});
123-
}
124111
}
125112

126-
127113
export {MDCRadio, MDCRadioFoundation};

packages/mdc-radio/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"dependencies": {
1717
"@material/animation": "^0.41.0",
1818
"@material/base": "^0.41.0",
19+
"@material/dom": "^0.41.0",
1920
"@material/ripple": "^0.44.0",
2021
"@material/selection-control": "^0.44.0",
2122
"@material/theme": "^0.43.0"

packages/mdc-ripple/adapter.ts

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

24-
/**
25-
* Adapter for MDC Ripple. Provides an interface for managing
26-
* - classes
27-
* - dom
28-
* - CSS variables
29-
* - position
30-
* - dimensions
31-
* - scroll position
32-
* - event handlers
33-
* - unbounded, active and disabled states
34-
*
35-
* Additionally, provides type information for the adapter to the Closure
36-
* compiler.
37-
*
38-
* Implement this adapter for your framework of choice to delegate updates to
39-
* the component in your framework of choice. See architecture documentation
40-
* for more details.
41-
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
42-
*
43-
*/
4424
import {EventType, SpecificEventListener} from '@material/dom/index';
4525

4626
interface Point {
4727
x: number;
4828
y: number;
4929
}
5030

31+
/**
32+
* Implement this adapter for your framework of choice to delegate updates to
33+
* the component in your framework of choice. See architecture documentation
34+
* for more details.
35+
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
36+
*/
5137
interface MDCRippleAdapter {
5238
browserSupportsCssVars(): boolean;
5339

packages/mdc-ripple/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,4 @@ class MDCRippleFoundation extends MDCFoundation<MDCRippleAdapter> {
539539
}
540540
}
541541

542-
export default MDCRippleFoundation;
542+
export {MDCRippleFoundation as default, MDCRippleFoundation};

scripts/webpack/js-bundle-factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class JsBundleFactory {
171171
menu: getAbsolutePath('/packages/mdc-menu/index.js'),
172172
menuSurface: getAbsolutePath('/packages/mdc-menu-surface/index.ts'),
173173
notchedOutline: getAbsolutePath('/packages/mdc-notched-outline/index.js'),
174-
radio: getAbsolutePath('/packages/mdc-radio/index.js'),
174+
radio: getAbsolutePath('/packages/mdc-radio/index.ts'),
175175
ripple: getAbsolutePath('/packages/mdc-ripple/index.ts'),
176176
select: getAbsolutePath('/packages/mdc-select/index.js'),
177177
selectionControl: getAbsolutePath('/packages/mdc-selection-control/index.ts'),

0 commit comments

Comments
 (0)