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 30 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
1,244 changes: 894 additions & 350 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@material/line-ripple": "^1.0.0",
"@material/linear-progress": "^1.1.0",
"@material/list": "^1.0.0",
"@material/menu": "^1.1.0",
"@material/menu-surface": "^1.0.1",
"@material/notched-outline": "^1.1.1",
"@material/radio": "^1.1.0",
Expand Down Expand Up @@ -122,8 +123,8 @@
"cp-file": "^6.0.0",
"cross-env": "^5.2.0",
"css-loader": "^0.28.10",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"eslint": "^5.9.0",
"eslint-config-google": "^0.9.1",
"eslint-plugin-react": "^7.7.0",
Expand Down
135 changes: 120 additions & 15 deletions packages/list/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
import * as React from 'react';
import classnames from 'classnames';
import {MDCListFoundation} from '@material/list/foundation';
import {ListItemContext, ListItemContextShape} from './index';
import {closest} from '@material/dom/ponyfill';

export interface ListItemProps<T> extends React.HTMLProps<T> {
export interface ListItemProps<T extends HTMLElement = HTMLElement> extends React.HTMLProps<T>, ListItemContextShape {
checkboxList?: boolean;
radioList?: boolean;
onKeyDown?: React.KeyboardEventHandler<T>;
onClick?: React.MouseEventHandler<T>;
onFocus?: React.FocusEventHandler<T>;
onBlur?: React.FocusEventHandler<T>;
tag?: string;
activated?: boolean;
selected?: boolean;
onDestroy?: () => void;
ref?: React.Ref<any>;
};

export interface ListItemState {
tabIndex?: number;
}

export default class ListItem<T extends HTMLElement = HTMLElement> extends React.Component<
ListItemProps<T>,
{}
> {
export class ListItemBase<T extends HTMLElement = HTMLElement> extends React.Component<
ListItemProps<T>, ListItemState> {
private listItemElement = React.createRef<T>();

static defaultProps: Partial<ListItemProps<HTMLElement>> = {
Expand All @@ -54,15 +54,54 @@ export default class ListItem<T extends HTMLElement = HTMLElement> extends React
onBlur: () => {},
onDestroy: () => {},
tag: 'li',
handleClick: () => {},
Copy link
Contributor

Choose a reason for hiding this comment

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

The naming here seems inconsistent. You're using onBlur and handleClick. I recommend being consistent with your event handler names (and generally prefer on{EventName})

Copy link
Author

Choose a reason for hiding this comment

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

onClick is a native React click handler. I don't want to collide and override this behavior since its native to React. Therefore I must use a different name (handleClick) so that I don't collide with the naming.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

handleKeyDown: () => {},
handleBlur: () => {},
handleFocus: () => {},
getListItemInitialTabIndex: () => -1,
getClassNamesFromList: () => ({}),
};

state = {
tabIndex: this.props.tabIndex,
};

get listElements(): Element[] {
if (this.listItemElement.current) {
const listElement = closest(this.listItemElement.current, `.${MDCListFoundation.cssClasses.ROOT}`);
if (!listElement) return [];
return [].slice.call(
listElement.querySelectorAll(MDCListFoundation.strings.ENABLED_ITEMS_SELECTOR)
);
}
return [];
}

componentDidMount() {
this.initializeTabIndex();
}

componentDidUpdate(prevProps: ListItemProps) {
if (prevProps.tabIndex !== this.props.tabIndex) {
this.setState({tabIndex: this.props.tabIndex});
}
}

componentWillUnmount() {
this.props.onDestroy!();
if (this.listItemElement.current) {
const index = this.getIndex(this.listItemElement.current);
this.props.onDestroy!(index);
}
}

get classes() {
const {className, activated, disabled, selected} = this.props;
return classnames('mdc-list-item', className, {
const {className, activated, disabled, selected, getClassNamesFromList} = this.props;
let classesFromList = [''];
if (this.listItemElement.current) {
const index = this.getIndex(this.listItemElement.current);
classesFromList = getClassNamesFromList!()[index];
}
return classnames('mdc-list-item', className, classesFromList, {
[MDCListFoundation.cssClasses.LIST_ITEM_ACTIVATED_CLASS]: activated,
[MDCListFoundation.cssClasses.LIST_ITEM_SELECTED_CLASS]: selected,
'mdc-list-item--disabled': disabled,
Expand All @@ -81,6 +120,42 @@ export default class ListItem<T extends HTMLElement = HTMLElement> extends React
return null;
}

private initializeTabIndex = () => {
if (this.listItemElement.current) {
const index = this.getIndex(this.listItemElement.current);
const tabIndex = this.props.getListItemInitialTabIndex!(index);
this.setState({tabIndex});
}
}

getIndex = (listElement: Element) => {
return this.listElements.indexOf(listElement);
}

handleClick = (e: React.MouseEvent<any>) => {
Copy link
Author

Choose a reason for hiding this comment

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

These are moved from list/index.tsx

const {onClick} = this.props;
onClick!(e);
this.props.handleClick!(e, this.getIndex(e.currentTarget));
}

handleKeyDown = (e: React.KeyboardEvent<any>) => {
const {onKeyDown} = this.props;
onKeyDown!(e);
this.props.handleKeyDown!(e, this.getIndex(e.currentTarget));
}

handleFocus = (e: React.FocusEvent<any>) => {
const {onFocus} = this.props;
onFocus!(e);
this.props.handleFocus!(e, this.getIndex(e.currentTarget));
}

handleBlur = (e: React.FocusEvent<any>) => {
const {onBlur} = this.props;
onBlur!(e);
this.props.handleBlur!(e, this.getIndex(e.currentTarget));
}

render() {
const {
/* eslint-disable no-unused-vars */
Expand All @@ -90,21 +165,51 @@ export default class ListItem<T extends HTMLElement = HTMLElement> extends React
checkboxList,
radioList,
onDestroy,
onClick,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused, what is the difference between onClick and handleClick ?

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

onKeyDown,
onFocus,
onBlur,
handleClick,
handleKeyDown,
handleFocus,
handleBlur,
getListItemInitialTabIndex,
getClassNamesFromList,
tabIndex,
/* eslint-enable no-unused-vars */
tag: Tag,
...otherProps
} = this.props;

return (
// https://github.com/Microsoft/TypeScript/issues/28892
// @ts-ignore
<Tag
{...otherProps}
{...this.context}
role={this.role}
className={this.classes}
ref={this.listItemElement}
{...otherProps}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
tabIndex={this.state.tabIndex}
>
{this.props.children}
{children}
</Tag>
);
}
}

const ListItem: React.FunctionComponent<ListItemProps> = (props) => {
return (
<ListItemContext.Consumer>
{(context) => (
<ListItemBase {...context} {...props}/>
)}
</ListItemContext.Consumer>
);
};

export default ListItem;
Loading