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

Commit a42d45f

Browse files
committed
fix(switch): upgrade to mdc-web v1
1 parent 23fe283 commit a42d45f

File tree

5 files changed

+95
-42
lines changed

5 files changed

+95
-42
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
@@ -81,7 +81,7 @@
8181
"@material/ripple": "^1.0.0",
8282
"@material/select": "^0.40.1",
8383
"@material/snackbar": "^0.43.0",
84-
"@material/switch": "^0.41.0",
84+
"@material/switch": "^1.0.0",
8585
"@material/tab": "^0.41.0",
8686
"@material/tab-bar": "^0.41.0",
8787
"@material/tab-indicator": "^1.0.0",

packages/switch/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
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 {MDCSwitchFoundation} from '@material/switch/foundation';
2726
import ThumbUnderlay from './ThumbUnderlay';
2827
import NativeControl from './NativeControl';
2928

@@ -44,7 +43,7 @@ interface SwitchState {
4443

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

4948
constructor(props: SwitchProps) {
5049
super(props);
@@ -66,16 +65,16 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
6665
componentDidMount() {
6766
this.foundation = new MDCSwitchFoundation(this.adapter);
6867
this.foundation.init();
69-
this.foundation.setChecked(this.props.checked);
70-
this.foundation.setDisabled(this.props.disabled);
68+
this.foundation.setChecked(this.props.checked!);
69+
this.foundation.setDisabled(this.props.disabled!);
7170
}
7271

7372
componentDidUpdate(prevProps: SwitchProps) {
7473
if (this.props.checked !== prevProps.checked) {
75-
this.foundation.setChecked(this.props.checked);
74+
this.foundation.setChecked(this.props.checked!);
7675
}
7776
if (this.props.disabled !== prevProps.disabled) {
78-
this.foundation.setDisabled(this.props.disabled);
77+
this.foundation.setDisabled(this.props.disabled!);
7978
}
8079
}
8180

@@ -112,7 +111,7 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
112111

113112
onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
114113
this.setState({nativeControlChecked: evt.target.checked});
115-
this.foundation && this.foundation.handleChange(evt);
114+
this.foundation && this.foundation.handleChange(evt.nativeEvent);
116115
};
117116

118117
render() {

packages/switch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@material/react-ripple": "^0.10.0",
2020
"@material/ripple": "^0.41.0",
21-
"@material/switch": "^0.41.0",
21+
"@material/switch": "^1.0.0",
2222
"classnames": "^2.2.6",
2323
"react": "^16.3.2"
2424
},

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)