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

Recursively extending child in selectable list for nested lists #2320

Merged
merged 1 commit into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions docs/src/app/components/pages/components/lists.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,12 @@ import ListItem from 'material-ui/lib/lists/list-item';
<ListItem
value={1}
primaryText="Brendan Lim"
leftAvatar={<Avatar src="images/ok-128.jpg" />} />
<ListItem value={2}
primaryText="Grace Ng"
leftAvatar={<Avatar src="images/uxceo-128.jpg" />} />
leftAvatar={<Avatar src="images/ok-128.jpg" />}
nestedItems={[
<ListItem value={2}
primaryText="Grace Ng"
leftAvatar={<Avatar src="images/uxceo-128.jpg" />} />,
]} />
<ListItem value={3}
primaryText="Kerem Suer"
leftAvatar={<Avatar src="images/kerem-128.jpg" />} />
Expand Down
20 changes: 13 additions & 7 deletions docs/src/app/components/raw-code/lists-code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,19 @@
<ListItem
value={1}
primaryText="Brendan Lim"
leftAvatar={<Avatar src="images/ok-128.jpg" />}/>
<ListItem
value={2}
primaryText="Grace Ng"
leftAvatar={<Avatar src="images/uxceo-128.jpg" />} />
<ListItem
value={3}
leftAvatar={<Avatar src="images/ok-128.jpg" />}
nestedItems={[
<ListItem value={2}
primaryText="Grace Ng"
leftAvatar={<Avatar src="images/uxceo-128.jpg" />} />
]} />
<ListItem value={3}
primaryText="Kerem Suer"
leftAvatar={<Avatar src="images/kerem-128.jpg" />} />
<ListItem value={4}
primaryText="Eric Hoffman"
leftAvatar={<Avatar src="images/kolage-128.jpg" />} />
<ListItem value={5}
primaryText="Raquel Parrado"
leftAvatar={<Avatar src="images/raquelromanp-128.jpg" />} />
</SelectableList>
61 changes: 30 additions & 31 deletions src/hoc/selectable-enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export const SelectableContainerEnhance = (Component) => {

render() {
const {children, selectedItemStyle} = this.props;
let listItems;
let keyIndex = 0;
this._keyIndex = 0;
let styles = {};

if (!selectedItemStyle) {
Expand All @@ -65,35 +64,7 @@ export const SelectableContainerEnhance = (Component) => {
};
}

listItems = React.Children.map(children, (child) => {
if (child.type.displayName === 'ListItem') {
let selected = this._isChildSelected(child, this.props);
let selectedChildrenStyles = {};
if (selected) {
selectedChildrenStyles = this.mergeStyles(styles, selectedItemStyle);
}

let mergedChildrenStyles = this.mergeStyles(
child.props.style || {},
selectedChildrenStyles
);

keyIndex += 1;

return React.cloneElement(child, {
onTouchTap: (e) => {
this._handleItemTouchTap(e, child);
if (child.props.onTouchTap) { child.props.onTouchTap(e); }
},
key: keyIndex,
style: mergedChildrenStyles,
});
}
else {
return child;
}
});
let newChildren = listItems;
let newChildren = React.Children.map(children, (child) => this._extendChild(child, styles, selectedItemStyle));

return (
<Component {...this.props} {...this.state}>
Expand All @@ -102,6 +73,34 @@ export const SelectableContainerEnhance = (Component) => {
);
},

_extendChild(child, styles, selectedItemStyle) {
if (child.type.displayName === 'ListItem') {
let selected = this._isChildSelected(child, this.props);
let selectedChildrenStyles = {};
if (selected) {
selectedChildrenStyles = this.mergeStyles(styles, selectedItemStyle);
}

let mergedChildrenStyles = this.mergeStyles(child.props.style || {}, selectedChildrenStyles);

this._keyIndex += 1;

return React.cloneElement(child, {
onTouchTap: (e) => {
this._handleItemTouchTap(e, child);
if (child.props.onTouchTap) {
child.props.onTouchTap(e);
}
},
key: this._keyIndex,
style: mergedChildrenStyles,
nestedItems: child.props.nestedItems.map((child) => this._extendChild(child, styles, selectedItemStyle)),
});
} else {
return child;
}
},

_isChildSelected(child, props) {
let itemValue = this.getValueLink(props).value;
let childValue = child.props.value;
Expand Down