Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 4e99b4c

Browse files
author
Matt Goo
committed
feat(list): update to MDC Web 1.0.0 (#740)
1 parent 38e9886 commit 4e99b4c

File tree

11 files changed

+766
-530
lines changed

11 files changed

+766
-530
lines changed

package-lock.json

Lines changed: 63 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"@material/layout-grid": "^0.41.0",
7676
"@material/line-ripple": "^1.0.0",
7777
"@material/linear-progress": "^0.41.0",
78-
"@material/list": "^0.41.0",
78+
"@material/list": "^1.0.0",
7979
"@material/menu-surface": "^0.41.0",
8080
"@material/notched-outline": "^0.41.0",
8181
"@material/radio": "^0.41.0",

packages/checkbox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class Checkbox extends React.Component<CheckboxProps, CheckboxState> {
187187
id={nativeControlId}
188188
checked={this.state.checked}
189189
disabled={disabled}
190-
aria-checked={this.state['aria-checked']}
190+
aria-checked={this.state['aria-checked'] || this.state.checked}
191191
name={name}
192192
onChange={this.onChange}
193193
rippleActivatorRef={this.inputElement}

packages/list/ListItem.tsx

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -22,123 +22,84 @@
2222

2323
import * as React from 'react';
2424
import classnames from 'classnames';
25+
import {MDCListFoundation} from '@material/list/foundation';
2526

2627
export interface ListItemProps<T> extends React.HTMLProps<T> {
27-
className?: string;
28-
classNamesFromList?: string[];
29-
attributesFromList?: object;
30-
childrenTabIndex?: number;
31-
tabIndex?: number;
32-
shouldFocus?: boolean;
33-
shouldFollowHref?: boolean;
34-
shouldToggleCheckbox?: boolean;
28+
checkboxList?: boolean;
29+
radioList?: boolean;
30+
onKeyDown?: React.KeyboardEventHandler<T>;
31+
onClick?: React.MouseEventHandler<T>;
32+
onFocus?: React.FocusEventHandler<T>;
33+
onBlur?: React.FocusEventHandler<T>;
3534
tag?: string;
36-
children?: React.ReactNode;
35+
activated?: boolean;
36+
selected?: boolean;
3737
};
3838

39-
function isAnchorElement(element: any): element is HTMLAnchorElement {
40-
return !!element.href;
41-
}
42-
43-
function isFocusableElement(element: any): element is HTMLElement {
44-
return typeof <HTMLElement>element.focus === 'function';
45-
}
46-
47-
export default class ListItem<T extends {} = HTMLElement> extends React.Component<
39+
// TODO: convert to functional component
40+
// https://github.com/material-components/material-components-web-react/issues/729
41+
export default class ListItem<T extends HTMLElement = HTMLElement> extends React.Component<
4842
ListItemProps<T>,
4943
{}
5044
> {
51-
listItemElement_: React.RefObject<T> = React.createRef();
45+
private listItemElement = React.createRef<T>();
5246

5347
static defaultProps: Partial<ListItemProps<HTMLElement>> = {
48+
checkboxList: false,
49+
radioList: false,
5450
className: '',
55-
classNamesFromList: [],
56-
attributesFromList: {},
57-
childrenTabIndex: -1,
5851
tabIndex: -1,
59-
shouldFocus: false,
60-
shouldFollowHref: false,
61-
shouldToggleCheckbox: false,
6252
onKeyDown: () => {},
6353
onClick: () => {},
6454
onFocus: () => {},
6555
onBlur: () => {},
6656
tag: 'li',
6757
};
6858

69-
componentDidUpdate(prevProps: ListItemProps<T>) {
70-
const {shouldFocus, shouldFollowHref, shouldToggleCheckbox} = this.props;
71-
if (shouldFocus && !prevProps.shouldFocus) {
72-
this.focus();
73-
}
74-
if (shouldFollowHref && !prevProps.shouldFollowHref) {
75-
this.followHref();
76-
}
77-
if (shouldToggleCheckbox && !prevProps.shouldToggleCheckbox) {
78-
this.toggleCheckbox();
79-
}
80-
}
81-
8259
get classes() {
83-
const {className, classNamesFromList} = this.props;
84-
return classnames('mdc-list-item', className, classNamesFromList);
60+
const {className, activated, selected} = this.props;
61+
return classnames('mdc-list-item', className, {
62+
[MDCListFoundation.cssClasses.LIST_ITEM_ACTIVATED_CLASS]: activated,
63+
[MDCListFoundation.cssClasses.LIST_ITEM_SELECTED_CLASS]: selected,
64+
});
8565
}
8666

87-
focus() {
88-
const element = this.listItemElement_.current;
89-
if (isFocusableElement(element)) {
90-
element.focus();
67+
get role() {
68+
const {checkboxList, radioList, role} = this.props;
69+
if (role) {
70+
return role;
71+
} else if (checkboxList) {
72+
return 'checkbox';
73+
} else if (radioList) {
74+
return 'radio';
9175
}
92-
}
93-
94-
followHref() {
95-
const element = this.listItemElement_.current;
96-
if (isAnchorElement(element)) {
97-
element.click();
98-
}
99-
}
100-
101-
toggleCheckbox() {
102-
// TODO(bonniez): implement
103-
// https://github.com/material-components/material-components-web-react/issues/352
76+
return null;
10477
}
10578

10679
render() {
10780
const {
108-
/* eslint-disable */
81+
/* eslint-disable no-unused-vars */
10982
className,
110-
classNamesFromList,
111-
childrenTabIndex,
112-
shouldFocus,
113-
shouldFollowHref,
114-
shouldToggleCheckbox,
115-
/* eslint-enable */
116-
attributesFromList,
11783
children,
84+
role,
85+
checkboxList,
86+
radioList,
87+
/* eslint-enable no-unused-vars */
11888
tag: Tag,
89+
11990
...otherProps
12091
} = this.props;
12192
return (
12293
// https://github.com/Microsoft/TypeScript/issues/28892
12394
// @ts-ignore
12495
<Tag
96+
role={this.role}
12597
className={this.classes}
98+
ref={this.listItemElement}
12699
{...otherProps}
127-
{...attributesFromList} // overrides attributes in otherProps
128-
ref={this.listItemElement_}
129100
>
130-
{React.Children.map(children, this.renderChild)}
101+
{this.props.children}
131102
</Tag>
132103
);
133104
}
134-
135-
renderChild = (child: React.ReactChild) => {
136-
if (typeof child === 'string' || typeof child === 'number' || child === null) {
137-
return child;
138-
}
139-
140-
const tabIndex = this.props.childrenTabIndex;
141-
const props = {...child.props, tabIndex};
142-
return React.cloneElement(child, props);
143-
};
144105
}

0 commit comments

Comments
 (0)