-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
216 lines (198 loc) · 6.48 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React from 'react';
import classnames from 'classnames';
import TabScroller from '@material/react-tab-scroller';
import Tab, {TabProps} from '@material/react-tab'; // eslint-disable-line @typescript-eslint/no-unused-vars
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>[];
}
interface TabBarState {
previousActiveIndex: number;
}
class TabBar extends React.Component<TabBarProps, TabBarState> {
tabBarRef: React.RefObject<HTMLDivElement> = React.createRef();
tabScrollerRef: React.RefObject<TabScroller> = React.createRef();
tabList: Tab[] = [];
foundation!: MDCTabBarFoundation;
constructor(props: TabBarProps) {
super(props);
this.state = {
previousActiveIndex: props.activeIndex,
};
}
static defaultProps: Partial<TabBarProps> = {
indexInView: 0,
activeIndex: 0,
handleActiveIndexUpdate: () => {},
className: '',
children: [],
isRtl: false,
};
componentDidMount() {
this.foundation = new MDCTabBarFoundation(this.adapter);
this.foundation.init();
const {activeIndex, indexInView} = this.props;
if (this.tabList[activeIndex]) {
// new DOMRect is not IE11 compatible
const defaultDOMRect = {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
};
this.tabList[activeIndex].activate(
defaultDOMRect /* previousIndicatorClientRect */
);
}
this.foundation.scrollIntoView(indexInView!);
}
componentDidUpdate(prevProps: TabBarProps) {
if (this.props.activeIndex !== prevProps.activeIndex) {
this.setState({previousActiveIndex: prevProps.activeIndex}, () =>
this.foundation.activateTab(this.props.activeIndex)
);
}
if (this.props.indexInView !== prevProps.indexInView) {
this.foundation.scrollIntoView(this.props.indexInView!);
}
}
componentWillUnmount() {
this.foundation.destroy();
}
get classes() {
return classnames('mdc-tab-bar', this.props.className);
}
get adapter(): MDCTabBarAdapter {
return {
scrollTo: (scrollX: number) => {
this.tabScrollerRef.current &&
this.tabScrollerRef.current.scrollTo(scrollX);
},
incrementScroll: (scrollXIncrement: number) => {
this.tabScrollerRef.current &&
this.tabScrollerRef.current.incrementScroll(scrollXIncrement);
},
getScrollPosition: () => {
if (!this.tabScrollerRef.current) return 0;
return this.tabScrollerRef.current.getScrollPosition();
},
getScrollContentWidth: () => {
if (!this.tabScrollerRef.current) return 0;
return this.tabScrollerRef.current.getScrollContentWidth();
},
getOffsetWidth: () => {
if (this.tabBarRef.current === null) return 0;
return this.tabBarRef.current.offsetWidth;
},
isRTL: () => !!this.props.isRtl,
setActiveTab: (index: number) => {
if (!this.props.handleActiveIndexUpdate) return;
this.props.handleActiveIndexUpdate(index);
},
activateTabAtIndex: (index: number, clientRect: ClientRect) =>
this.tabList[index].activate(clientRect),
deactivateTabAtIndex: (index: number) => this.tabList[index].deactivate(),
focusTabAtIndex: (index: number) => this.tabList[index].focus(),
getTabIndicatorClientRectAtIndex: (index: number) =>
this.tabList[index].computeIndicatorClientRect(),
getTabDimensionsAtIndex: (index: number) =>
this.tabList[index].computeDimensions(),
getPreviousActiveTabIndex: () => this.state.previousActiveIndex,
getFocusedTabIndex: () => {
const activeElement = document.activeElement;
// cannot use findIndex, not supported in IE11
// cannot use forEach, return statement inside the forEach loop will not return
for (let i = 0; i < this.tabList.length; i++) {
if (this.tabList[i].tabRef.current === activeElement) {
return i;
}
}
return -1;
},
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),
};
}
pushToTabList = (el: Tab) => {
this.tabList.push(el);
};
onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
// Persist the synthetic event to access its `key`.
e.persist();
this.setState({previousActiveIndex: this.props.activeIndex}, () =>
this.foundation.handleKeyDown(e.nativeEvent)
);
if (this.props.onKeyDown) {
this.props.onKeyDown(e);
}
};
onClickTab = (
e: React.MouseEvent<HTMLButtonElement>,
index: number,
onClick?: React.MouseEventHandler<HTMLButtonElement>
) => {
this.setState({previousActiveIndex: this.props.activeIndex}, () =>
this.adapter.setActiveTab(index)
);
if (onClick) {
onClick(e);
}
};
render() {
const {
/* eslint-disable @typescript-eslint/no-unused-vars */
className,
indexInView,
activeIndex,
handleActiveIndexUpdate,
onKeyDown,
/* eslint-enable @typescript-eslint/no-unused-vars */
isRtl,
children,
...otherProps
} = this.props;
return (
<div
dir={isRtl ? 'rtl' : 'ltr'}
className={this.classes}
role='tablist'
onKeyDown={this.onKeyDown}
ref={this.tabBarRef}
{...otherProps}
>
<TabScroller ref={this.tabScrollerRef}>
{React.Children.map(
children as React.ReactElement<TabProps>[],
this.renderTab
)}
</TabScroller>
</div>
);
}
renderTab = (tab: React.ReactElement<TabProps>, index: number) => {
const {children, onClick, ...otherProps} = tab.props;
const props = {
onClick: (e: React.MouseEvent<HTMLButtonElement>) =>
this.onClickTab(e, index, onClick),
ref: this.pushToTabList,
...otherProps,
} as TabProps;
return React.cloneElement(tab, props, children);
};
}
export {Tab};
export default TabBar;