-
Notifications
You must be signed in to change notification settings - Fork 227
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
moog16
merged 38 commits into
feat/mdcweb-typescript-update
from
feat/menu/addcomponent_try3
Apr 26, 2019
Merged
Changes from 11 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
cae6b6d
WIP: menu
e237d30
WIP: Menu
f6cadef
fix: menu with seenshot
14a1183
WIP: selection menu
e80d458
WIP: menu
8d0b52a
WIP: getting menulistitem to be interpretted as a listitem
cb1694b
fix: menu list handleActionitem
aafb2d1
fix: add readme
4900f4d
feat: add tests
3abb5f4
fix: lint and test
61b076b
fix: add golden
05c48dd
fix: Pr changes
8a2196f
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
6ee5ad7
WIP: using context to pass list item
85c0b93
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
93bf167
Merge branch 'feat/menu/addcomponent_try3' into experiemental/menu/co…
c605cef
fix: using context instead
bb4c79b
fix: context is working
06e2ba6
fix: remove context from state
8ff2d9f
WIP: tests
667ed80
fix: tests
f2c0805
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
9ded538
Merge branch 'experiemental/menu/context' into feat/menu/addcomponent…
7521f67
fix: commented tests
b10563c
fix: drawer test
395520d
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
1b9b7a1
fix: memoize listProps
8ac8668
fix: revert esmoduleinterop
a5fe91e
Update index.tsx
cb75d24
fix: esmoduleinterop
949fd9e
fix: remove * as
e70dee4
fix: screenshots
3a8a01e
fix: move memoizeone
c6930a4
Merge branch 'feat/mdcweb-typescript-update' into feat/menu/addcompon…
ed4054f
fix: packagelock
d074619
fix: remove menu/index.scss
9c50842
fix: add a postinstall to fix react adapter
f93bcdf
fix: add -f so postinstall doesn't fail
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, {}> { | ||
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :-/
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
but then we would need to redeclare every prop on the
defaultProps
of theListItem
. does that make sense?There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
theMenuListItem
?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 bothListItem
andMenuListItem
, and in the context you can put the functions you need in the item. What do you think?