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

Commit 44cca84

Browse files
guguMatt Goo
authored and
Matt Goo
committed
feat(switch): upgrade to mdc-web v1 (#757)
1 parent 580c850 commit 44cca84

File tree

5 files changed

+97
-43
lines changed

5 files changed

+97
-43
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"@material/ripple": "^1.0.0",
8383
"@material/select": "^0.40.1",
8484
"@material/snackbar": "^1.0.0",
85-
"@material/switch": "^0.41.0",
85+
"@material/switch": "^1.0.0",
8686
"@material/tab": "^1.0.0",
8787
"@material/tab-bar": "^1.0.0",
8888
"@material/tab-indicator": "^1.0.0",

packages/switch/index.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import * as React from 'react';
2424
import classnames from 'classnames';
25-
// @ts-ignore no .d.ts file
26-
import {MDCSwitchFoundation} from '@material/switch/dist/mdc.switch';
25+
import {MDCSwitchAdapter} from '@material/switch/adapter';
26+
import {MDCSwitchFoundation} from '@material/switch/foundation';
2727
import ThumbUnderlay from './ThumbUnderlay';
2828
import NativeControl from './NativeControl';
2929

@@ -44,7 +44,7 @@ interface SwitchState {
4444

4545
export default class Switch extends React.Component<SwitchProps, SwitchState> {
4646
rippleActivator: React.RefObject<HTMLInputElement> = React.createRef();
47-
foundation?: MDCSwitchFoundation;
47+
foundation!: MDCSwitchFoundation;
4848

4949
constructor(props: SwitchProps) {
5050
super(props);
@@ -66,16 +66,16 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
6666
componentDidMount() {
6767
this.foundation = new MDCSwitchFoundation(this.adapter);
6868
this.foundation.init();
69-
this.foundation.setChecked(this.props.checked);
70-
this.foundation.setDisabled(this.props.disabled);
69+
this.foundation.setChecked(this.props.checked!);
70+
this.foundation.setDisabled(this.props.disabled!);
7171
}
7272

7373
componentDidUpdate(prevProps: SwitchProps) {
7474
if (this.props.checked !== prevProps.checked) {
75-
this.foundation.setChecked(this.props.checked);
75+
this.foundation.setChecked(this.props.checked!);
7676
}
7777
if (this.props.disabled !== prevProps.disabled) {
78-
this.foundation.setDisabled(this.props.disabled);
78+
this.foundation.setDisabled(this.props.disabled!);
7979
}
8080
}
8181

@@ -89,7 +89,7 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
8989
return classnames('mdc-switch', Array.from(classList), className);
9090
}
9191

92-
get adapter() {
92+
get adapter(): MDCSwitchAdapter {
9393
return {
9494
addClass: (className: string) => {
9595
const {classList} = this.state;
@@ -112,7 +112,7 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
112112

113113
onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
114114
this.setState({nativeControlChecked: evt.target.checked});
115-
this.foundation && this.foundation.handleChange(evt);
115+
this.foundation && this.foundation.handleChange(evt.nativeEvent);
116116
};
117117

118118
render() {

packages/switch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@material/react-ripple": "^0.11.0",
20-
"@material/switch": "^0.41.0",
20+
"@material/switch": "^1.0.0",
2121
"classnames": "^2.2.6",
2222
"react": "^16.3.2"
2323
},

test/unit/switch/index.test.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import {assert} from 'chai';
33
import {shallow, mount} from 'enzyme';
44
import * as td from 'testdouble';
55
import Switch from '../../../packages/switch/index';
6+
import {coerceForTesting} from '../helpers/types';
67

78
suite('Switch');
89

10+
const getAdapter = (instance: Switch) => {
11+
// @ts-ignore
12+
return instance.foundation.adapter_;
13+
};
14+
915
test('creates foundation', () => {
1016
const wrapper = shallow<Switch>(<Switch />);
1117
assert.exists(wrapper.instance().foundation);
@@ -47,42 +53,42 @@ test('has checked class when props.checked is true', () => {
4753

4854
test('#foundation.setChecked gets called when prop.checked updates', () => {
4955
const wrapper = shallow<Switch>(<Switch />);
50-
wrapper.instance().foundation.setChecked = td.func();
56+
wrapper.instance().foundation.setChecked = td.func<(setChecked: boolean) => null>();
5157
wrapper.setProps({checked: true});
5258
td.verify(wrapper.instance().foundation.setChecked(true), {times: 1});
5359
});
5460

5561
test('#foundation.setDisabled gets called when prop.disabled updates', () => {
5662
const wrapper = shallow<Switch>(<Switch />);
57-
wrapper.instance().foundation.setDisabled = td.func();
63+
wrapper.instance().foundation.setDisabled = td.func<(disabled: boolean) => null>();
5864
wrapper.setProps({disabled: true});
5965
td.verify(wrapper.instance().foundation.setDisabled(true), {times: 1});
6066
});
6167

6268
test('#componentWillUnmount destroys foundation', () => {
6369
const wrapper = shallow<Switch>(<Switch />);
6470
const foundation = wrapper.instance().foundation;
65-
foundation.destroy = td.func();
71+
foundation.destroy = td.func<() => null>();
6672
wrapper.unmount();
6773
td.verify(foundation.destroy(), {times: 1});
6874
});
6975

7076
test('#adapter.addClass adds class to state.classList', () => {
7177
const wrapper = shallow<Switch>(<Switch />);
72-
wrapper.instance().foundation.adapter_.addClass('test-class-name');
78+
getAdapter(wrapper.instance()).addClass('test-class-name');
7379
assert.isTrue(wrapper.state().classList.has('test-class-name'));
7480
});
7581

7682
test('#adapter.removeClass removes class from state.classList', () => {
7783
const wrapper = shallow<Switch>(<Switch />);
7884
wrapper.setState({classList: new Set(['test-class-name'])});
79-
wrapper.instance().foundation.adapter_.removeClass('test-class-name');
85+
getAdapter(wrapper.instance()).removeClass('test-class-name');
8086
assert.isFalse(wrapper.state().classList.has('test-class-name'));
8187
});
8288

8389
test('#adapter.setNativeControlChecked updates state.nativeControlChecked', () => {
8490
const wrapper = shallow<Switch>(<Switch />);
85-
wrapper.instance().foundation.adapter_.setNativeControlChecked(true);
91+
getAdapter(wrapper.instance()).setNativeControlChecked(true);
8692
assert.isTrue(wrapper.state().nativeControlChecked);
8793
});
8894

@@ -94,7 +100,7 @@ test('#state.nativeControlChecked updates NativeControl', () => {
94100

95101
test('#adapter.setNativeControlDisabled updates state.nativeControlDisabled', () => {
96102
const wrapper = shallow<Switch>(<Switch />);
97-
wrapper.instance().foundation.adapter_.setNativeControlDisabled(true);
103+
getAdapter(wrapper.instance()).setNativeControlDisabled(true);
98104
assert.isTrue(wrapper.state().nativeControlDisabled);
99105
});
100106

@@ -116,12 +122,17 @@ test('calls foundation.handleChange in NativeControl props.onChange', () => {
116122
const onChange = td.func() as React.ChangeEventHandler<HTMLInputElement>;
117123
const wrapper = mount<Switch>(<Switch onChange={onChange} />);
118124
const nativeControl = wrapper.find('.mdc-switch__native-control');
119-
const mockEvt = {
125+
const mockEvt = coerceForTesting<React.ChangeEvent<HTMLInputElement>>({
120126
target: {
121127
checked: true,
122128
},
123-
} as React.ChangeEvent<HTMLInputElement>;
124-
wrapper.instance().foundation.handleChange = td.func();
129+
nativeEvent: {
130+
target: {
131+
checked: true,
132+
},
133+
},
134+
});
135+
wrapper.instance().foundation.handleChange = td.func<(evt: Event) => null>();
125136
nativeControl.props().onChange!(mockEvt);
126-
td.verify(wrapper.instance().foundation.handleChange(mockEvt), {times: 1});
137+
td.verify(wrapper.instance().foundation.handleChange(mockEvt.nativeEvent), {times: 1});
127138
});

0 commit comments

Comments
 (0)