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

feat(switch): upgrade to mdc-web v1 #757

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 140 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"@material/ripple": "^1.0.0",
"@material/select": "^0.40.1",
"@material/snackbar": "^1.0.0",
"@material/switch": "^0.41.0",
"@material/switch": "^1.0.0",
"@material/tab": "^1.0.0",
"@material/tab-bar": "^0.41.0",
"@material/tab-indicator": "^1.0.0",
Expand Down
18 changes: 9 additions & 9 deletions packages/switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import * as React from 'react';
import classnames from 'classnames';
// @ts-ignore no .d.ts file
import {MDCSwitchFoundation} from '@material/switch/dist/mdc.switch';
import {MDCSwitchAdapter} from '@material/switch/adapter';
import {MDCSwitchFoundation} from '@material/switch/foundation';
import ThumbUnderlay from './ThumbUnderlay';
import NativeControl from './NativeControl';

Expand All @@ -44,7 +44,7 @@ interface SwitchState {

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

constructor(props: SwitchProps) {
super(props);
Expand All @@ -66,16 +66,16 @@ export default class Switch extends React.Component<SwitchProps, SwitchState> {
componentDidMount() {
this.foundation = new MDCSwitchFoundation(this.adapter);
this.foundation.init();
this.foundation.setChecked(this.props.checked);
this.foundation.setDisabled(this.props.disabled);
this.foundation.setChecked(this.props.checked!);
this.foundation.setDisabled(this.props.disabled!);
}

componentDidUpdate(prevProps: SwitchProps) {
if (this.props.checked !== prevProps.checked) {
this.foundation.setChecked(this.props.checked);
this.foundation.setChecked(this.props.checked!);
}
if (this.props.disabled !== prevProps.disabled) {
this.foundation.setDisabled(this.props.disabled);
this.foundation.setDisabled(this.props.disabled!);
}
}

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

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

onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
this.setState({nativeControlChecked: evt.target.checked});
this.foundation && this.foundation.handleChange(evt);
this.foundation && this.foundation.handleChange(evt.nativeEvent);
};

render() {
Expand Down
2 changes: 1 addition & 1 deletion packages/switch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"@material/react-ripple": "^0.11.0",
"@material/switch": "^0.41.0",
"@material/switch": "^1.0.0",
"classnames": "^2.2.6",
"react": "^16.3.2"
},
Expand Down
Loading