-
Notifications
You must be signed in to change notification settings - Fork 227
/
index.tsx
203 lines (185 loc) · 6.38 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
// The MIT License
//
// Copyright (c) 2019 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import * as React from 'react';
import classnames from 'classnames';
import {MDCMenuFoundation} from '@material/menu/foundation';
import {MDCMenuAdapter} from '@material/menu/adapter';
import MenuSurface, {MenuSurfaceProps} from '@material/react-menu-surface';
import MenuList, {MenuListProps} from './MenuList';
import MenuListItem from './MenuListItem';
const {cssClasses} = MDCMenuFoundation;
export interface MenuProps extends MenuSurfaceProps {
children: React.ReactElement<MenuList>;
onSelected?: (index: number, item: Element) => void;
ref?: React.Ref<any>;
}
export interface MenuState {
open: boolean;
foundation?: MDCMenuFoundation;
}
class Menu extends React.Component<MenuProps, MenuState> {
menuListElement = React.createRef<MenuList>();
// need foundation on state so that it initializes MenuList with
// foundation.handleItemAction
state: MenuState = {
foundation: undefined,
open: this.props.open || false,
};
static defaultProps: Partial<MenuProps> = {
className: '',
open: false,
onKeyDown: () => {},
onSelected: () => {},
};
componentDidMount() {
const foundation = new MDCMenuFoundation(this.adapter);
foundation.init();
this.setState({foundation});
}
componentDidUpdate(prevProps: MenuProps) {
if (this.props.open !== prevProps.open) {
this.setState({open: this.props.open!});
}
}
componentWillUnmount() {
if (this.state.foundation) {
this.state.foundation.destroy();
}
}
get listElements(): Element[] {
if (
!(
this.menuListElement.current &&
this.menuListElement.current.listElements &&
this.menuListElement.current.listElements.length >= 0
)
) {
return [];
}
return this.menuListElement.current.listElements;
}
get adapter(): MDCMenuAdapter {
return {
// TODO: update to new class management system from
// https://github.com/material-components/material-components-web-react/pull/776/files
// https://github.com/material-components/material-components-web-react/issues/796
addClassToElementAtIndex: (index, className) => {
const list = this.listElements;
list[index].classList.add(className);
},
removeClassFromElementAtIndex: (index, className) => {
const list = this.listElements;
list[index].classList.remove(className);
},
addAttributeToElementAtIndex: (index, attr, value) => {
const list = this.listElements;
list[index].setAttribute(attr, value);
},
removeAttributeFromElementAtIndex: (index, attr) => {
const list = this.listElements;
list[index].removeAttribute(attr);
},
elementContainsClass: (element, className) =>
element.classList.contains(className),
closeSurface: () => this.setState({open: false}),
getElementIndex: (element) => this.listElements.indexOf(element),
getParentElement: (element) => element.parentElement,
getSelectedElementIndex: (selectionGroup) => {
const selectedListItem = selectionGroup.querySelector(
`.${cssClasses.MENU_SELECTED_LIST_ITEM}`
);
return selectedListItem
? this.listElements.indexOf(selectedListItem)
: -1;
},
notifySelected: (evtData) =>
this.props.onSelected!(evtData.index, this.listElements[evtData.index]),
};
}
handleKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (evt) => {
const {onKeyDown} = this.props;
if (onKeyDown) {
onKeyDown(evt);
}
this.state.foundation &&
this.state.foundation.handleKeydown(evt.nativeEvent);
};
handleOpen: MenuSurfaceProps['onOpen'] = () => {
const {onOpen} = this.props;
onOpen && onOpen();
};
render() {
const {
className,
/* eslint-disable @typescript-eslint/no-unused-vars */
open,
onKeyDown,
onOpen,
children,
onSelected,
/* eslint-enable @typescript-eslint/no-unused-vars */
...otherProps
} = this.props;
return (
<MenuSurface
tabIndex={-1}
open={this.state.open}
className={classnames('mdc-menu', className)}
onKeyDown={this.handleKeyDown}
onOpen={this.handleOpen}
{...otherProps}
>
{this.renderChild()}
</MenuSurface>
);
}
renderChild() {
const {children} = this.props;
const {foundation} = this.state;
if (!children) return;
let handleItemAction: MDCMenuFoundation['handleItemAction'] = () => {};
if (foundation) {
// this is to avoid a `handleItemAction` of undefined error
handleItemAction = foundation.handleItemAction.bind(foundation);
}
const updatedProps: MenuListProps = {
// children.props must appear first
...children.props,
handleItemAction,
ref: this.menuListElement,
wrapFocus: true,
};
return React.cloneElement(children, updatedProps);
}
}
export default Menu;
export {MenuList, MenuListItem};
export {
ListDivider as MenuListDivider,
ListGroup as MenuListGroup,
ListGroupSubheader as MenuListGroupSubheader,
ListItemGraphic as MenuListItemGraphic,
ListItemMeta as MenuListItemMeta,
ListItemText as MenuListItemText,
} from '@material/react-list';
export {MenuListProps} from './MenuList';
export {MenuListItemProps} from './MenuListItem';