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

Commit 5052ada

Browse files
authored
feat(select): Convert JS to TypeScript (#4386)
Refs #4225
1 parent 976352c commit 5052ada

File tree

20 files changed

+890
-1058
lines changed

20 files changed

+890
-1058
lines changed

packages/mdc-base/component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ class MDCComponent<FoundationType extends MDCFoundation> {
100100
}
101101

102102
/**
103-
* Fires a cross-browser-compatible custom event from the component root of the given type,
104-
* with the given data.
103+
* Fires a cross-browser-compatible custom event from the component root of the given type, with the given data.
105104
*/
106105
emit<T extends object>(evtType: string, evtData: T, shouldBubble = false) {
107106
let evt: CustomEvent<T>;

packages/mdc-list/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class MDCList extends MDCComponent<MDCListFoundation> {
135135
}
136136
},
137137
focusItemAtIndex: (index) => {
138-
const element = this.listElements[index] as HTMLElement;
138+
const element = this.listElements[index] as HTMLElement | undefined;
139139
if (element) {
140140
element.focus();
141141
}

packages/mdc-menu/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import {MDCList} from '@material/list/index';
2525
import {MDCMenuSurface} from '@material/menu-surface/index';
2626

27+
export type MenuItemEvent = CustomEvent<MenuItemEventDetail>;
28+
export type DefaultMenuItemEvent = CustomEvent<DefaultMenuItemEventDetail>;
29+
2730
/**
2831
* Event properties used by the adapter and foundation.
2932
*/

packages/mdc-select/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ The `MDCSelect` component API is modeled after a subset of the `HTMLSelectElemen
407407

408408
| Property | Type | Description |
409409
| --- | --- | --- |
410-
| `value` | string | The `value`/`data-value` of the currently selected option. |
411-
| `selectedIndex` | number | The index of the currently selected option. Set to -1 if no option is currently selected. Changing this property will update the select element. |
412-
| `disabled` | boolean | Whether or not the component is disabled. Setting this sets the disabled state on the component. |
413-
| `valid` | boolean | Whether or not the component is in a valid state. Setting this updates styles on the component, but does not affect the native validity state. |
414-
| `required` | boolean | Whether or not the component is required. Setting this updates the `required` or `aria-required` attribute on the component and enables validation. |
415-
| `leadingIconAriaLabel` | string (write-only) | Proxies to the foundation's `setLeadingIconAriaLabel` method. |
416-
| `leadingIconContent` | string (write-only) | Proxies to the foundation's `setLeadingIconContent` method. |
417-
| `helperTextContent` | string (write-only)| Proxies to the foundation's `setHelperTextContent` method when set. |
410+
| `value` | `string` | The `value`/`data-value` of the currently selected option. |
411+
| `selectedIndex` | `number` | The index of the currently selected option. Set to -1 if no option is currently selected. Changing this property will update the select element. |
412+
| `disabled` | `boolean` | Whether or not the component is disabled. Setting this sets the disabled state on the component. |
413+
| `valid` | `boolean` | Whether or not the component is in a valid state. Setting this updates styles on the component, but does not affect the native validity state. |
414+
| `required` | `boolean` | Whether or not the component is required. Setting this updates the `required` or `aria-required` attribute on the component and enables validation. |
415+
| `leadingIconAriaLabel` | `string` (write-only) | Proxies to the foundation's `setLeadingIconAriaLabel` method. |
416+
| `leadingIconContent` | `string` (write-only) | Proxies to the foundation's `setLeadingIconContent` method. |
417+
| `helperTextContent` | `string` (write-only)| Proxies to the foundation's `setHelperTextContent` method when set. |
418418

419419
### Events
420420

@@ -462,7 +462,7 @@ If you are using a JavaScript framework, such as React or Angular, you can creat
462462
| `handleBlur() => void` | Handles a blur event on the `select` element. |
463463
| `handleClick(normalizedX: number) => void` | Sets the line ripple center to the normalizedX for the line ripple. |
464464
| `handleChange() => void` | Handles a change to the `select` element's value. This must be called both for `change` events and programmatic changes requested via the component API. |
465-
| `handleKeydown(event: Event) => void` | Handles opening the menu (enhanced select) when the `mdc-select__selected-text` element is focused and the user presses the `Enter` or `Space` key. |
465+
| `handleKeydown(event: KeyboardEvent) => void` | Handles opening the menu (enhanced select) when the `mdc-select__selected-text` element is focused and the user presses the `Enter` or `Space` key. |
466466
| `setSelectedIndex(index: number) => void` | Handles setting the `mdc-select__selected-text` element and closing the menu (enhanced select only). Also causes the label to float and outline to notch if needed. |
467467
| `setValue(value: string) => void` | Handles setting the value through the adapter and causes the label to float and outline to notch if needed. |
468468
| `getValue() => string` | Handles getting the value through the adapter. |

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

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,159 +21,118 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/* eslint no-unused-vars: [2, {"args": "none"}] */
25-
/* eslint-disable no-unused-vars */
26-
import {MDCSelectIconFoundation} from './icon/index';
27-
import {MDCSelectHelperTextFoundation} from './helper-text/index';
28-
/* eslint-enable no-unused-vars */
29-
30-
/**
31-
* @typedef {{
32-
* leadingIcon: (!MDCSelectIconFoundation|undefined),
33-
* helperText: (!MDCSelectHelperTextFoundation|undefined),
34-
* }}
35-
*/
36-
let FoundationMapType;
37-
3824
/**
39-
* Adapter for MDC Select. Provides an interface for managing
40-
* - classes
41-
* - dom
42-
* - event handlers
43-
*
44-
* Additionally, provides type information for the adapter to the Closure
45-
* compiler.
46-
*
25+
* Defines the shape of the adapter expected by the foundation.
4726
* Implement this adapter for your framework of choice to delegate updates to
4827
* the component in your framework of choice. See architecture documentation
4928
* for more details.
5029
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
51-
*
52-
* @record
5330
*/
54-
55-
class MDCSelectAdapter {
31+
interface MDCSelectAdapter {
5632
/**
5733
* Adds class to root element.
58-
* @param {string} className
5934
*/
60-
addClass(className) {}
35+
addClass(className: string): void;
6136

6237
/**
6338
* Removes a class from the root element.
64-
* @param {string} className
6539
*/
66-
removeClass(className) {}
40+
removeClass(className: string): void;
6741

6842
/**
6943
* Returns true if the root element contains the given class name.
70-
* @param {string} className
71-
* @return {boolean}
7244
*/
73-
hasClass(className) {}
45+
hasClass(className: string): boolean;
7446

7547
/**
7648
* Activates the bottom line, showing a focused state.
7749
*/
78-
activateBottomLine() {}
50+
activateBottomLine(): void;
7951

8052
/**
8153
* Deactivates the bottom line.
8254
*/
83-
deactivateBottomLine() {}
55+
deactivateBottomLine(): void;
8456

8557
/**
8658
* Sets the value of the select.
87-
* @param {string} value
8859
*/
89-
setValue(value) {}
60+
setValue(value: string): void;
9061

9162
/**
9263
* Returns the selected value of the select element.
93-
* @return {string}
9464
*/
95-
getValue() {}
65+
getValue(): string;
9666

9767
/**
9868
* Floats label determined based off of the shouldFloat argument.
99-
* @param {boolean} shouldFloat
10069
*/
101-
floatLabel(shouldFloat) {}
70+
floatLabel(shouldFloat: boolean): void;
10271

10372
/**
10473
* Returns width of label in pixels, if the label exists.
105-
* @return {number}
10674
*/
107-
getLabelWidth() {}
75+
getLabelWidth(): number;
10876

10977
/**
11078
* Returns true if outline element exists, false if it doesn't.
111-
* @return {boolean}
11279
*/
113-
hasOutline() {}
80+
hasOutline(): boolean;
11481

11582
/**
11683
* Only implement if outline element exists.
117-
* @param {number} labelWidth
11884
*/
119-
notchOutline(labelWidth) {}
85+
notchOutline(labelWidth: number): void;
12086

12187
/**
12288
* Closes notch in outline element, if the outline exists.
12389
*/
124-
closeOutline() {}
90+
closeOutline(): void;
12591

12692
/**
12793
* Opens the menu.
12894
*/
129-
openMenu() {}
95+
openMenu(): void;
13096

13197
/**
13298
* Closes the menu.
13399
*/
134-
closeMenu() {}
100+
closeMenu(): void;
135101

136102
/**
137103
* Returns true if the menu is currently open.
138-
* @return {boolean}
139104
*/
140-
isMenuOpen() {}
105+
isMenuOpen(): boolean;
141106

142107
/**
143108
* Sets the selected index of the select to the index provided.
144-
* @param {number} index
145109
*/
146-
setSelectedIndex(index) {}
110+
setSelectedIndex(index: number): void;
147111

148112
/**
149113
* Sets the select to disabled.
150-
* @param {boolean} isDisabled
151114
*/
152-
setDisabled(isDisabled) {}
115+
setDisabled(isDisabled: boolean): void;
153116

154117
/**
155118
* Sets the line ripple transform origin center.
156-
* @param {number} normalizedX
157119
*/
158-
setRippleCenter(normalizedX) {}
120+
setRippleCenter(normalizedX: number): void;
159121

160122
/**
161123
* Emits a change event when an element is selected.
162-
* @param {string} value
163124
*/
164-
notifyChange(value) {}
125+
notifyChange(value: string): void;
165126

166127
/**
167128
* Checks if the select is currently valid.
168-
* @return {boolean} isValid
169129
*/
170-
checkValidity() {}
130+
checkValidity(): boolean;
171131

172132
/**
173133
* Adds/Removes the invalid class.
174-
* @param {boolean} isValid
175134
*/
176-
setValid(isValid) {}
135+
setValid(isValid: boolean): void;
177136
}
178137

179-
export {MDCSelectAdapter, FoundationMapType};
138+
export {MDCSelectAdapter as default, MDCSelectAdapter};

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

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

24-
/** @enum {string} */
2524
const cssClasses = {
2625
DISABLED: 'mdc-select--disabled',
27-
ROOT: 'mdc-select',
28-
OUTLINED: 'mdc-select--outlined',
2926
FOCUSED: 'mdc-select--focused',
30-
SELECTED_ITEM_CLASS: 'mdc-list-item--selected',
31-
WITH_LEADING_ICON: 'mdc-select--with-leading-icon',
3227
INVALID: 'mdc-select--invalid',
28+
OUTLINED: 'mdc-select--outlined',
3329
REQUIRED: 'mdc-select--required',
30+
ROOT: 'mdc-select',
31+
SELECTED_ITEM_CLASS: 'mdc-list-item--selected',
32+
WITH_LEADING_ICON: 'mdc-select--with-leading-icon',
3433
};
3534

36-
/** @enum {string} */
3735
const strings = {
3836
ARIA_CONTROLS: 'aria-controls',
37+
ARIA_SELECTED_ATTR: 'aria-selected',
3938
CHANGE_EVENT: 'MDCSelect:change',
40-
SELECTED_ITEM_SELECTOR: `.${cssClasses.SELECTED_ITEM_CLASS}`,
41-
LEADING_ICON_SELECTOR: '.mdc-select__icon',
42-
SELECTED_TEXT_SELECTOR: '.mdc-select__selected-text',
39+
ENHANCED_VALUE_ATTR: 'data-value',
4340
HIDDEN_INPUT_SELECTOR: 'input[type="hidden"]',
44-
MENU_SELECTOR: '.mdc-select__menu',
45-
LINE_RIPPLE_SELECTOR: '.mdc-line-ripple',
4641
LABEL_SELECTOR: '.mdc-floating-label',
42+
LEADING_ICON_SELECTOR: '.mdc-select__icon',
43+
LINE_RIPPLE_SELECTOR: '.mdc-line-ripple',
44+
MENU_SELECTOR: '.mdc-select__menu',
4745
NATIVE_CONTROL_SELECTOR: '.mdc-select__native-control',
4846
OUTLINE_SELECTOR: '.mdc-notched-outline',
49-
ENHANCED_VALUE_ATTR: 'data-value',
50-
ARIA_SELECTED_ATTR: 'aria-selected',
47+
SELECTED_ITEM_SELECTOR: `.${cssClasses.SELECTED_ITEM_CLASS}`,
48+
SELECTED_TEXT_SELECTOR: '.mdc-select__selected-text',
5149
};
5250

53-
/** @enum {number} */
5451
const numbers = {
5552
LABEL_SCALE: 0.75,
5653
};

0 commit comments

Comments
 (0)