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

Commit a3d014a

Browse files
author
Matt Goo
committed
feat(select): typescript (#540)
1 parent 4582a7d commit a3d014a

File tree

8 files changed

+389
-359
lines changed

8 files changed

+389
-359
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/select/NativeControl.js renamed to packages/select/NativeControl.tsx

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,40 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
// THE SOFTWARE.
2222

23-
import React from 'react';
24-
import PropTypes from 'prop-types';
25-
import classnames from 'classnames';
23+
import * as React from 'react';
24+
import * as classnames from 'classnames';
25+
// no mdc .d.ts file
26+
// @ts-ignore
27+
import {MDCSelectFoundation} from '@material/select/dist/mdc.select';
2628

27-
export default class NativeControl extends React.Component {
28-
nativeControl_ = React.createRef();
29+
export interface NativeControlProps extends React.HTMLProps<HTMLSelectElement> {
30+
className: string;
31+
disabled: boolean;
32+
foundation: MDCSelectFoundation;
33+
setRippleCenter: (lineRippleCenter: number) => void;
34+
handleDisabled: (disabled: boolean) => void;
35+
}
36+
37+
export default class NativeControl extends React.Component<
38+
NativeControlProps,
39+
{}
40+
> {
41+
nativeControl_: React.RefObject<HTMLSelectElement> = React.createRef();
42+
43+
static defaultProps: NativeControlProps = {
44+
className: '',
45+
children: null,
46+
disabled: false,
47+
foundation: {
48+
handleFocus: () => {},
49+
handleBlur: () => {},
50+
},
51+
setRippleCenter: () => {},
52+
handleDisabled: () => {},
53+
};
2954

30-
componentDidUpdate(prevProps) {
55+
56+
componentDidUpdate(prevProps: NativeControlProps) {
3157
if (this.props.disabled !== prevProps.disabled) {
3258
this.props.handleDisabled(this.props.disabled);
3359
}
@@ -37,37 +63,37 @@ export default class NativeControl extends React.Component {
3763
return classnames('mdc-select__native-control', this.props.className);
3864
}
3965

40-
handleFocus = (evt) => {
66+
handleFocus = (evt: React.FocusEvent<HTMLSelectElement>) => {
4167
const {foundation, onFocus} = this.props;
4268
foundation.handleFocus(evt);
43-
onFocus(evt);
44-
}
69+
onFocus && onFocus(evt);
70+
};
4571

46-
handleBlur = (evt) => {
72+
handleBlur = (evt: React.FocusEvent<HTMLSelectElement>) => {
4773
const {foundation, onBlur} = this.props;
4874
foundation.handleBlur(evt);
49-
onBlur(evt);
50-
}
75+
onBlur && onBlur(evt);
76+
};
5177

52-
handleMouseDown = (evt) => {
78+
handleMouseDown = (evt: React.MouseEvent<HTMLSelectElement>) => {
5379
const {onMouseDown} = this.props;
54-
this.setRippleCenter(evt.clientX, evt.target);
55-
onMouseDown(evt);
56-
}
80+
this.setRippleCenter(evt.clientX, evt.target as HTMLSelectElement);
81+
onMouseDown && onMouseDown(evt);
82+
};
5783

58-
handleTouchStart = (evt) => {
84+
handleTouchStart = (evt: React.TouchEvent<HTMLSelectElement>) => {
5985
const {onTouchStart} = this.props;
6086
const clientX = evt.touches[0] && evt.touches[0].clientX;
61-
this.setRippleCenter(clientX, evt.target);
62-
onTouchStart(evt);
63-
}
87+
this.setRippleCenter(clientX, evt.target as HTMLSelectElement);
88+
onTouchStart && onTouchStart(evt);
89+
};
6490

65-
setRippleCenter = (xCoordinate, target) => {
91+
setRippleCenter = (xCoordinate: number, target: HTMLSelectElement) => {
6692
if (target !== this.nativeControl_.current) return;
6793
const targetClientRect = target.getBoundingClientRect();
6894
const normalizedX = xCoordinate - targetClientRect.left;
6995
this.props.setRippleCenter(normalizedX);
70-
}
96+
};
7197

7298
render() {
7399
const {
@@ -104,44 +130,3 @@ export default class NativeControl extends React.Component {
104130
);
105131
}
106132
}
107-
108-
NativeControl.propTypes = {
109-
className: PropTypes.string,
110-
children: PropTypes.node,
111-
disabled: PropTypes.bool,
112-
foundation: PropTypes.shape({
113-
handleFocus: PropTypes.func,
114-
handleBlur: PropTypes.func,
115-
}),
116-
id: PropTypes.string,
117-
onBlur: PropTypes.func,
118-
onChange: PropTypes.func,
119-
onFocus: PropTypes.func,
120-
onTouchStart: PropTypes.func,
121-
onMouseDown: PropTypes.func,
122-
setRippleCenter: PropTypes.func,
123-
handleDisabled: PropTypes.func,
124-
value: PropTypes.oneOfType([
125-
PropTypes.string,
126-
PropTypes.number,
127-
]),
128-
};
129-
130-
NativeControl.defaultProps = {
131-
className: '',
132-
children: null,
133-
disabled: false,
134-
foundation: {
135-
handleFocus: () => {},
136-
handleBlur: () => {},
137-
},
138-
id: null,
139-
onBlur: () => {},
140-
onChange: () => {},
141-
onFocus: () => {},
142-
onTouchStart: () => {},
143-
onMouseDown: () => {},
144-
setRippleCenter: () => {},
145-
handleDisabled: () => {},
146-
value: '',
147-
};

0 commit comments

Comments
 (0)