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

fix(tab-bar): upgrade mdc-web to v1 #770

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
176 changes: 101 additions & 75 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 @@ -84,7 +84,7 @@
"@material/snackbar": "^1.0.0",
"@material/switch": "^0.41.0",
"@material/tab": "^1.0.0",
"@material/tab-bar": "^0.41.0",
"@material/tab-bar": "^1.0.0",
"@material/tab-indicator": "^1.0.0",
"@material/tab-scroller": "^1.0.0",
"@material/textfield": "^0.41.0",
Expand Down
24 changes: 13 additions & 11 deletions packages/tab-bar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import * as React from 'react';
import classnames from 'classnames';
import TabScroller from '@material/react-tab-scroller';
import Tab, {TabProps} from '@material/react-tab'; // eslint-disable-line no-unused-vars
// @ts-ignore No mdc .d.ts files
import {MDCTabBarFoundation} from '@material/tab-bar/dist/mdc.tabBar';
import {MDCTabBarFoundation} from '@material/tab-bar/foundation';
import {MDCTabBarAdapter} from '@material/tab-bar/adapter';

export interface TabBarProps extends React.HTMLAttributes<HTMLDivElement> {
indexInView?: number;
activeIndex: number;
handleActiveIndexUpdate?: (index: number) => void;
onActivated?: (index: number) => void;
className?: string;
isRtl?: boolean;
children: React.ReactElement<TabProps> | React.ReactElement<TabProps>[];
Expand All @@ -24,7 +25,7 @@ class TabBar extends React.Component<
tabBarRef: React.RefObject<HTMLDivElement> = React.createRef();
tabScrollerRef: React.RefObject<TabScroller> = React.createRef();
tabList: Tab[] = [];
foundation?: MDCTabBarFoundation;
foundation!: MDCTabBarFoundation;

constructor(props: TabBarProps) {
super(props);
Expand Down Expand Up @@ -60,7 +61,7 @@ class TabBar extends React.Component<
};
this.tabList[activeIndex].activate(defaultDOMRect /* previousIndicatorClientRect */);
}
this.foundation.scrollIntoView(indexInView);
this.foundation.scrollIntoView(indexInView!);
}

componentDidUpdate(prevProps: TabBarProps) {
Expand All @@ -70,7 +71,7 @@ class TabBar extends React.Component<
);
}
if (this.props.indexInView !== prevProps.indexInView) {
this.foundation.scrollIntoView(this.props.indexInView);
this.foundation.scrollIntoView(this.props.indexInView!);
}
}

Expand All @@ -82,7 +83,7 @@ class TabBar extends React.Component<
return classnames('mdc-tab-bar', this.props.className);
}

get adapter() {
get adapter(): MDCTabBarAdapter {
return {
scrollTo: (scrollX: number) => {
this.tabScrollerRef.current && this.tabScrollerRef.current.scrollTo(scrollX);
Expand All @@ -91,15 +92,15 @@ class TabBar extends React.Component<
this.tabScrollerRef.current && this.tabScrollerRef.current.incrementScroll(scrollXIncrement);
},
getScrollPosition: () => {
if (!this.tabScrollerRef.current) return;
if (!this.tabScrollerRef.current) return 0;
return this.tabScrollerRef.current.getScrollPosition();
},
getScrollContentWidth: () => {
if (!this.tabScrollerRef.current) return;
if (!this.tabScrollerRef.current) return 0;
return this.tabScrollerRef.current.getScrollContentWidth();
},
getOffsetWidth: () => {
if (this.tabBarRef.current === null) return;
if (this.tabBarRef.current === null) return 0;
return this.tabBarRef.current.offsetWidth;
},
isRTL: () => !!this.props.isRtl,
Expand Down Expand Up @@ -127,8 +128,9 @@ class TabBar extends React.Component<
}
return -1;
},
getIndexOfTab: (tabToFind: Tab) => this.tabList.indexOf(tabToFind),
getIndexOfTabById: (id: string) => this.tabList.map((tab) => tab.props.id).indexOf(id),
getTabListLength: () => this.tabList.length,
notifyTabActivated: (index: number) => this.props.onActivated && this.props.onActivated(index),
};
}

Expand All @@ -140,7 +142,7 @@ class TabBar extends React.Component<
// Persist the synthetic event to access its `key`.
e.persist();
this.setState({previousActiveIndex: this.props.activeIndex}, () =>
this.foundation.handleKeyDown(e)
this.foundation.handleKeyDown(e.nativeEvent)
);
if (this.props.onKeyDown) {
this.props.onKeyDown(e);
Expand Down
2 changes: 1 addition & 1 deletion packages/tab-bar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@material/react-tab": "^0.11.0",
"@material/react-tab-scroller": "^0.11.0",
"@material/tab-bar": "^0.41.0",
"@material/tab-bar": "^1.0.0",
"classnames": "^2.2.6",
"react": "^16.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class Tab extends React.Component<TabProps, TabState> {
}

computeIndicatorClientRect = () => {
if (!this.tabIndicatorRef.current) return;
if (!this.tabIndicatorRef.current) return {} as ClientRect;
return this.tabIndicatorRef.current.computeContentClientRect();
};

Expand Down
Loading