This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
feat(list): Add component #339
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
db8db91
feat(list): Add component
bonniezhou 19db863
implement adapter methods
bonniezhou 2f2073e
finish implementing list
bonniezhou 35d9c3c
list item
bonniezhou a035700
add screenshot test
bonniezhou 66523f4
get selection and tabindex working
bonniezhou 8fe7934
get selection working
bonniezhou 12ef3da
ListItem test
bonniezhou 682f8b9
list tests
bonniezhou f26b9ac
fix tests and lint
bonniezhou 112f19d
README
bonniezhou 1013268
Merge branch 'master' into feat/list
bonniezhou 72e6cb2
name change
bonniezhou aa96143
break graphic text and meta to subcomponents
bonniezhou 9d2a2a3
fix nits for list
bonniezhou 7c55b77
accept elements for text
bonniezhou 21c527d
merge classes instead of init
bonniezhou 4eb1f3e
condense screenshot tests
bonniezhou 706b8cb
use keys instead of indices
bonniezhou 5b526d4
fix tests
bonniezhou ccfb8a8
fixes
bonniezhou 0017019
more fixes
bonniezhou a41652a
bug
bonniezhou 167bd34
selection fix
bonniezhou d2726d2
use id instead of key
bonniezhou 38ff1c6
list tests
bonniezhou ca0a263
WIP tests
bonniezhou a9f5b84
fix all tests
bonniezhou f057f83
readme
bonniezhou 545a933
Merge branch 'master' into feat/list
bonniezhou 39ac757
lint
bonniezhou c36e005
merge with master
bonniezhou d40ae5b
update screenshot golden
bonniezhou 2ffe55c
only change list in screenshot goldens
bonniezhou d8fc6c6
fix readme
bonniezhou fdc52fd
Merge branch 'master' into feat/list
bonniezhou 585f340
fix layout grid screenshot test
bonniezhou 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
This file contains hidden or 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,3 @@ | ||
index.js | ||
ListDivider.js | ||
ListItem.js |
This file contains hidden or 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,107 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2018 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 React, {Component} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
export default class ListItem extends Component { | ||
listItemElement_ = React.createRef(); | ||
|
||
componentDidMount() { | ||
const {init} = this.props; | ||
init && init(); | ||
} | ||
|
||
get listItemElement() { | ||
return this.listItemElement_.current; | ||
} | ||
|
||
get classes() { | ||
const {className} = this.props; | ||
return classnames('mdc-list-item', className); | ||
} | ||
|
||
focus() { | ||
const element = this.listItemElement_.current; | ||
if (element) { | ||
element.focus(); | ||
} | ||
} | ||
|
||
followHref() { | ||
const element = this.listItemElement_.current; | ||
if (element && element.href) { | ||
element.click(); | ||
} | ||
} | ||
|
||
toggleCheckbox() { | ||
// TODO(bonniez): implement | ||
// https://github.com/material-components/material-components-web-react/issues/352 | ||
} | ||
|
||
render() { | ||
const { | ||
/* eslint-disable */ | ||
className, | ||
childrenTabIndex, | ||
init, | ||
id, | ||
/* eslint-enable */ | ||
children, | ||
...otherProps | ||
} = this.props; | ||
|
||
return ( | ||
<li | ||
className={this.classes} | ||
ref={this.listItemElement_} | ||
{...otherProps} | ||
> | ||
{React.Children.map(children, this.renderChild)} | ||
</li> | ||
); | ||
} | ||
|
||
renderChild = (child) => { | ||
const props = Object.assign({}, | ||
child.props, | ||
{tabIndex: this.props.childrenTabIndex} | ||
); | ||
return React.cloneElement(child, props); | ||
} | ||
} | ||
|
||
ListItem.propTypes = { | ||
id: PropTypes.string, | ||
childrenTabIndex: PropTypes.number, | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
init: PropTypes.func, | ||
}; | ||
|
||
ListItem.defaultProps = { | ||
id: '', | ||
childrenTabIndex: -1, | ||
className: '', | ||
}; |
This file contains hidden or 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,59 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2018 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 React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
const ListItemGraphic = (props) => { | ||
const { | ||
tabIndex, // eslint-disable-line no-unused-vars | ||
graphic, | ||
tabbableOnListItemFocus, | ||
className, | ||
...otherProps | ||
} = props; | ||
|
||
const graphicProps = { | ||
className: classnames('mdc-list-item__graphic', className), | ||
tabIndex: tabbableOnListItemFocus ? props.tabIndex : -1, | ||
...otherProps, | ||
}; | ||
|
||
return React.cloneElement(graphic, graphicProps); | ||
}; | ||
|
||
ListItemGraphic.propTypes = { | ||
tabbableOnListItemFocus: PropTypes.bool, | ||
className: PropTypes.string, | ||
tabIndex: PropTypes.number, | ||
graphic: PropTypes.element, | ||
}; | ||
|
||
ListItemGraphic.defaultProps = { | ||
tabbableOnListItemFocus: false, | ||
className: '', | ||
tabIndex: -1, | ||
graphic: {}, | ||
}; | ||
|
||
export default ListItemGraphic; |
This file contains hidden or 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,69 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2018 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 React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
const ListItemMeta = (props) => { | ||
const { | ||
tabIndex, // eslint-disable-line no-unused-vars | ||
meta, | ||
className, | ||
tabbableOnListItemFocus, | ||
...otherProps | ||
} = props; | ||
|
||
let metaElement = null; | ||
if (typeof meta === 'string') { | ||
metaElement = <span>{meta}</span>; | ||
} else { | ||
metaElement = meta; | ||
} | ||
|
||
const metaProps = { | ||
className: classnames('mdc-list-item__meta', className, meta.className), | ||
tabIndex: tabbableOnListItemFocus ? props.tabIndex : -1, | ||
...otherProps, | ||
}; | ||
|
||
return React.cloneElement(metaElement, metaProps); | ||
}; | ||
|
||
ListItemMeta.propTypes = { | ||
tabbableOnListItemFocus: PropTypes.bool, | ||
className: PropTypes.string, | ||
tabIndex: PropTypes.number, | ||
meta: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.element, | ||
]), | ||
}; | ||
|
||
ListItemMeta.defaultProps = { | ||
tabbableOnListItemFocus: false, | ||
className: '', | ||
tabIndex: -1, | ||
meta: null, | ||
}; | ||
|
||
export default ListItemMeta; |
This file contains hidden or 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,93 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2018 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 React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
const ListItemText = (props) => { | ||
const { | ||
primaryText, | ||
secondaryText, | ||
tabbableOnListItemFocus, | ||
tabIndex, | ||
className, | ||
...otherProps | ||
} = props; | ||
|
||
const renderText = (text, className) => { | ||
if (typeof text === 'string') { | ||
return ( | ||
<span | ||
className={className} | ||
tabIndex={tabbableOnListItemFocus ? tabIndex : -1} | ||
> | ||
{text} | ||
</span> | ||
); | ||
} | ||
const {className: textClassName, ...otherProps} = text.props; | ||
const props = Object.assign({ | ||
className: classnames(className, textClassName), | ||
}, ...otherProps); | ||
return React.cloneElement(text, props); | ||
}; | ||
|
||
if (!secondaryText) { | ||
return renderText(primaryText, classnames('mdc-list-item__text', className)); | ||
} | ||
|
||
return ( | ||
<span | ||
className={classnames('mdc-list-item__text', className)} | ||
tabIndex={tabbableOnListItemFocus ? tabIndex : -1} | ||
{...otherProps} | ||
> | ||
{renderText(primaryText, 'mdc-list-item__primary-text')} | ||
{renderText(secondaryText, 'mdc-list-item__secondary-text')} | ||
</span> | ||
); | ||
}; | ||
|
||
ListItemText.propTypes = { | ||
tabbableOnListItemFocus: PropTypes.bool, | ||
tabIndex: PropTypes.number, | ||
className: PropTypes.string, | ||
primaryText: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.element, | ||
]), | ||
secondaryText: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.element, | ||
]), | ||
}; | ||
|
||
ListItemText.defaultProps = { | ||
tabbableOnListItemFocus: false, | ||
tabIndex: -1, | ||
className: '', | ||
primaryText: '', | ||
secondaryText: '', | ||
}; | ||
|
||
export default ListItemText; |
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.
just add a defaultProp for
init
as an empty method. You don't need to guard against it on line 32.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.
If we set a default prop here the actual prop doesn't override it....