Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(menu): add component #786

Merged
merged 38 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cae6b6d
WIP: menu
Mar 14, 2019
e237d30
WIP: Menu
Mar 15, 2019
f6cadef
fix: menu with seenshot
Mar 20, 2019
14a1183
WIP: selection menu
Mar 21, 2019
e80d458
WIP: menu
Mar 27, 2019
8d0b52a
WIP: getting menulistitem to be interpretted as a listitem
Mar 28, 2019
cb1694b
fix: menu list handleActionitem
Mar 28, 2019
aafb2d1
fix: add readme
Mar 28, 2019
4900f4d
feat: add tests
Mar 29, 2019
3abb5f4
fix: lint and test
Mar 29, 2019
61b076b
fix: add golden
Mar 29, 2019
05c48dd
fix: Pr changes
Apr 1, 2019
8a2196f
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
Apr 2, 2019
6ee5ad7
WIP: using context to pass list item
Apr 2, 2019
85c0b93
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
Apr 2, 2019
93bf167
Merge branch 'feat/menu/addcomponent_try3' into experiemental/menu/co…
Apr 2, 2019
c605cef
fix: using context instead
Apr 3, 2019
bb4c79b
fix: context is working
Apr 3, 2019
06e2ba6
fix: remove context from state
Apr 3, 2019
8ff2d9f
WIP: tests
Apr 4, 2019
667ed80
fix: tests
Apr 5, 2019
f2c0805
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
Apr 5, 2019
9ded538
Merge branch 'experiemental/menu/context' into feat/menu/addcomponent…
Apr 5, 2019
7521f67
fix: commented tests
Apr 5, 2019
b10563c
fix: drawer test
Apr 5, 2019
395520d
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
Apr 17, 2019
1b9b7a1
fix: memoize listProps
Apr 19, 2019
8ac8668
fix: revert esmoduleinterop
Apr 19, 2019
a5fe91e
Update index.tsx
Apr 19, 2019
cb75d24
fix: esmoduleinterop
Apr 23, 2019
949fd9e
fix: remove * as
Apr 23, 2019
e70dee4
fix: screenshots
Apr 23, 2019
3a8a01e
fix: move memoizeone
Apr 23, 2019
c6930a4
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
Apr 26, 2019
ed4054f
fix: packagelock
Apr 26, 2019
d074619
fix: remove menu/index.scss
Apr 26, 2019
9c50842
fix: add a postinstall to fix react adapter
Apr 26, 2019
f93bcdf
fix: add -f so postinstall doesn't fail
Apr 26, 2019
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
126 changes: 126 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@material/line-ripple": "^1.0.0",
"@material/linear-progress": "^0.41.0",
"@material/list": "^1.0.0",
"@material/menu": "^1.1.0",
"@material/menu-surface": "^1.0.1",
"@material/notched-outline": "^0.41.0",
"@material/radio": "^0.41.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/list/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface ListItemProps<T> extends React.HTMLProps<T> {
tag?: string;
activated?: boolean;
selected?: boolean;
ref?: React.Ref<any>;
renderWithListItemProps?: boolean;
};

// TODO: convert to functional component
Expand All @@ -47,6 +49,7 @@ export default class ListItem<T extends HTMLElement = HTMLElement> extends React
static defaultProps: Partial<ListItemProps<HTMLElement>> = {
checkboxList: false,
radioList: false,
renderWithListItemProps: false,
className: '',
tabIndex: -1,
onKeyDown: () => {},
Expand Down Expand Up @@ -84,9 +87,9 @@ export default class ListItem<T extends HTMLElement = HTMLElement> extends React
role,
checkboxList,
radioList,
renderWithListItemProps,
/* eslint-enable no-unused-vars */
tag: Tag,

...otherProps
} = this.props;
return (
Expand Down
10 changes: 7 additions & 3 deletions packages/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ export interface ListProps<T extends HTMLElement> extends React.HTMLProps<HTMLEl
wrapFocus?: boolean;
tag?: string;
children: ListItem<T> | ListItem<T>[] | React.ReactNode;
ref?: React.Ref<any>;
};

function isReactText(element: any): element is React.ReactText {
return typeof element === 'string' || typeof element === 'number';
return typeof element === 'string' || typeof element === 'number' || !element;
}

function isListItem(element: any): element is ListItem {
Expand Down Expand Up @@ -316,8 +317,11 @@ export default class List<T extends HTMLElement = HTMLElement> extends React.Com
}

renderChild = (child: React.ReactElement<ListItemProps<T>> | React.ReactChild) => {
if (!isReactText(child) && isListItem(child)) {
return this.renderListItem(child, this.listItemCount++);
if (!isReactText(child)) {
const {renderWithListItemProps} = child.props;
if (renderWithListItemProps || isListItem(child)) {
return this.renderListItem(child, this.listItemCount++);
}
}
return child;
};
Expand Down
2 changes: 0 additions & 2 deletions packages/menu-surface/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {Corner} from '@material/menu-surface/index';
import {MDCMenuDistance} from '@material/menu-surface/types';

export interface MenuSurfaceProps extends React.HTMLProps<HTMLDivElement> {
className?: string;
anchorElement?: HTMLElement;
anchorCorner?: number;
anchorMargin?: {
Expand All @@ -45,7 +44,6 @@ export interface MenuSurfaceProps extends React.HTMLProps<HTMLDivElement> {
};
onClose?: () => void;
onOpen?: () => void;
onKeyDown?: (event: React.KeyboardEvent) => void;
quickOpen?: boolean;
open?: boolean;
fixed?: boolean;
Expand Down
104 changes: 104 additions & 0 deletions packages/menu/MenuList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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 List, {ListProps} from '@material/react-list';
// eslint-disable-next-line no-unused-vars
import {MDCMenuFoundation} from '@material/menu/foundation';

type RefCallback<T> = (node: T | null) => void;

export interface MenuListProps<T extends HTMLElement> extends Exclude<ListProps<T>, 'ref'> {
innerRef?: RefCallback<List> | React.RefObject<List>;
handleItemAction?: MDCMenuFoundation['handleItemAction'];
};

class MenuList<T extends HTMLElement = HTMLElement> extends React.Component<MenuListProps<T>, {}> {
private listInstance = React.createRef<List>();

static defaultProps: Partial<MenuListProps<HTMLElement>> = {
className: '',
handleSelect: () => {},
handleItemAction: () => {},
};

get listElements(): Element[] {
if (!this.listInstance.current) {
return [];
}
return this.listInstance.current.listElements;
}

handleSelect: ListProps<HTMLElement>['handleSelect'] = (activatedItemIndex, selected) => {
this.props.handleSelect!(activatedItemIndex, selected);
this.props.handleItemAction!(this.listElements[activatedItemIndex]);
}


attachRef = (node: List | null) => {
const {innerRef} = this.props;

// https://github.com/facebook/react/issues/13029#issuecomment-410002316
// @ts-ignore this is acceptable according to the comment above
this.listInstance.current = node;

if (!innerRef) {
return;
}

if (typeof innerRef !== 'function') {
// @ts-ignore same as above
innerRef.current = node;
} else {
innerRef(node);
}
}

render() {
const {
'aria-hidden': ariaHidden,
/* eslint-disable no-unused-vars */
handleSelect,
handleItemAction,
role,
innerRef,
children,
ref,
/* eslint-enable no-unused-vars */
...otherProps
} = this.props;

return (
<List
aria-hidden={ariaHidden !== undefined ? ariaHidden : 'true'}
role={role || 'menu'}
handleSelect={this.handleSelect}
ref={this.attachRef}
{...otherProps}
>
{children}
</List>
);
}
}

export default MenuList;
54 changes: 54 additions & 0 deletions packages/menu/MenuListItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 {ListItem, ListItemProps} from '@material/react-list';

export interface MenuListItemProps<T> extends ListItemProps<T> {
children?: React.ReactNode;
}

class MenuListItem<T extends HTMLElement = HTMLElement> extends React.Component<MenuListItemProps<T>, {}> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a functional component

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually need this to be a Compnonent to pass along the defaultProps. Any idea around that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I don't think we could reuse the default props from the list item :-/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defaultProp is the renderWithListItemProps, so it can't live on the listItem. Is that what you are suggesting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, my idea would be to use default parameters on the function (which is the recommended way to use default props on function component) like this

const MenuListItem = <T extends HTMLElement = HTMLElement>({
  renderWithListItemProps = true,
  ...props
}) => {
  // etc
}

but then we would need to redeclare every prop on the defaultProps of the ListItem. does that make sense?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ya - of course I need to pass that along...however, I don't think we need to pass the same defaultProps to the ListItem...The reason I have renderWithListItemProps on the MenuListItem, is so that the List knows to pass along certain props to the MenuListItem. Does that make sense?

Copy link
Contributor

@lucasecdb lucasecdb Apr 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see. I think that's kind a weird way to do it 😄

can't you add to the check of element.type === ListItem the MenuListItem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya I realize that's weird...but it also seemed wierd that MenuListItem is referenced in List. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that is the better more foolproof way :| -- I'll wait to hear back from you if you think of something better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create a context in the List component and use the context consumer in both ListItem and MenuListItem, and in the context you can put the functions you need in the item. What do you think?

static defaultProps: Partial<MenuListItemProps<HTMLElement>> = {
...ListItem.defaultProps,
renderWithListItemProps: true,
};

render() {
const {
role = 'menuitem',
children,
...otherProps
} = this.props;

return (
<ListItem
role={role}
{...otherProps}
>
{children}
</ListItem>
);
}
}

export default MenuListItem;
Loading