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

[List/ListItem] Introduce selected property #1581 #1976

Merged
merged 2 commits into from Nov 18, 2015

Conversation

frankf-cgn
Copy link

Implementation of Issue #1581:

  • following the ReactLink pattern, List has now a selectedLink prop
  • selectedLink is a plain js object holding the index of the currently selected ListItem and a callback function to change that value
  • ListItem has new prop index
  • callback-fn of selectedLink is called when onTouchTap is fired by a ListItem
  • the original onTouchTap is propagated, too.

Example implementation for the container component for the List:

export default class MyListContainer extends Component {
  constructor(props) {
    super(props);
    this.state = { selectedIndex: 1 }
  }

  handleUpdateSelectedIndex = (index) => {
    this.setState(Object.assign({}, this.state, {selectedIndex: index}));
  }

  render() {
    var selectedLink = {
      value: this.state.selectedIndex,
      requestChange: this.handleUpdateSelectedIndex
    };
    return(
      <List style={styles.paper} 
        subheader="Contacts"
        selectedLink={selectedLink}>
        <ListItem index={1} ....../>
        <ListItem index={2} ....../>
      </List>
    );
  }
}

@frankf-cgn
Copy link
Author

Is there anything I can do to get this merged?


const styles = {
root: {
backgroundColor: (this.state.isKeyboardFocused || this.state.hovered) &&
backgroundColor: (this.props.selected && !this.state.hovered) ? selectedColor : ((this.state.isKeyboardFocused || this.state.hovered) &&
Copy link
Member

Choose a reason for hiding this comment

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

the ...other is going to have the selected property then passed to the EnhancedButton. Is this intentional? Otherwise, can you remove it.

@frankf-cgn
Copy link
Author

I've set the default value as suggested and fixed the unintentionally passing of selected to the EnhancedButton.

@@ -85,16 +88,39 @@ const List = React.createClass({
subheaderElement = <div style={mergedSubheaderStyles}>{subheader}</div>;
}

let listItems = React.Children.map(children, (child) => {
if ( this.props.selectedLink && (child.type.displayName === "ListItem") ) {
Copy link
Member

Choose a reason for hiding this comment

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

I would move if(this.props.selectedLink out of the loop, for perf reasons. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

I fully agree.

@oliviertassinari oliviertassinari added PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI and removed Review: review-needed labels Nov 4, 2015
@oliviertassinari
Copy link
Member

@frankf-cgn Thanks. Could you have a look at my feedbacks? I think that this PR is going in the right direction 👍. I will also ask you to squash down the number of commit (to 2 or 3). I will keep the history clean.

@frankf-cgn
Copy link
Author

@oliviertassinari Thanks for your review and time. I fixed the loop and I'll do the documentation stuff. And of cource I'm going to shrink down the number of commits.

@frankf-cgn
Copy link
Author

@oliviertassinari I have fixed the loop and added the documentation. Concerning the documentation I have tried to choose the wording to be close to the tabs-stuff cos it's somehwat similar to this. Let me know if you are fine with it and I'll clean up the commit history and rebase to the current master.

@oliviertassinari oliviertassinari added Review: review-needed and removed PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI labels Nov 4, 2015
@frankf-cgn
Copy link
Author

@oliviertassinari Squashed the commits

@frankf-cgn
Copy link
Author

@oliviertassinari If you are ready to merge this, come back to me and I will rebase to the actual master.

}

handleUpdateSelectedIndex = (index) => {
this.setState(Object.assign({}, this.state, {selectedIndex: index}));
Copy link
Member

Choose a reason for hiding this comment

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

Why not doing?

this.setState({
  selectedIndex: index,
});

Copy link
Author

Choose a reason for hiding this comment

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

The way I proposed to set the state ensures that whatever is currently in the state does not get overwritten / thrown away unintentionally, isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

As far as I know, we have the same result with the above. See the setState doc:

Performs a shallow merge of nextState into current state

Copy link
Author

Choose a reason for hiding this comment

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

Uups, didn't rember that. So I'll change it.

@frankf-cgn
Copy link
Author

@oliviertassinari Thanks for the hints. I have incorporated your suggestions.
One question. In list-item.jsx around line 102, should i add index to const {..., ...other}, so that index is not further propagated to EnhancedButton like you suggested for selected?

@oliviertassinari
Copy link
Member

should i add index to const {..., ...other}, so that index is not further propagated to EnhancedButton like you suggested for selected?

Sounds like a good idea

@shaurya947
Copy link
Contributor

@oliviertassinari OK to merge?

@oliviertassinari
Copy link
Member

@shaurya947 I didn't try it but it looks good to me.

</Paper>
);
},

_getSelected(index) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure we need to create two methods since they are called at only one place.

@shaurya947
Copy link
Contributor

@oliviertassinari I think you should check it out before you merge since you've reviewed the code (and are more familiar with it).

@oliviertassinari
Copy link
Member

before you merge

I wasn't going to merge yet 😁

@shaurya947
Copy link
Contributor

Well, eventually, right? :-P

@@ -54,6 +85,12 @@ export default class ListsPage extends React.Component {
desc: 'If true, the subheader will be indented by 72px.',
},
{
name: 'selectedItemStyle',
type: 'object',
header: 'optional, only available it HOC SelectableContainerEnhance is used',
Copy link
Contributor

Choose a reason for hiding this comment

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

only available if HOC

@shaurya947
Copy link
Contributor

@frankf-cgn thus far we've used simple html tags with some CodeBlock components for documentation. I think we should try to stick to that for consistency and avoid the whole markdown thing. See here for an example.

@frankf-cgn frankf-cgn force-pushed the issue/1581 branch 2 times, most recently from ec3b30e to 1e04820 Compare November 17, 2015 11:21
@frankf-cgn
Copy link
Author

The typos are fixed, some rewording has taken place and I have removed the markdown stuff. Hope we have reached the last itterations ;-)

<ul>
<li> enhance <code>&lt;List&gt;</code> with HOC</li>
<li> decide where to put state</li>
<li> implement end set valueLink</li>
Copy link
Contributor

Choose a reason for hiding this comment

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

should this read "implement and set valueLink" ?

@shaurya947
Copy link
Contributor

I just pointed out a few quick things @frankf-cgn. Once you can take care of them, we should merge this.

I'll also give @oliviertassinari a chance to look over this.

@frankf-cgn
Copy link
Author

@shaurya947 I was so sure that I have find all typos during my proofreading. Ok, everthing done by now and pushed.
I have removed this nasty /* eslint-disable */which effectivly disabled eslint for the code.

@oliviertassinari
Copy link
Member

@shaurya947 Let's see

</p>
<div style={styles.codeblock}>
<CodeBlock>
{`import { SelectableContainerEnhance } from 'material-ui/hoc/selectable-enhance';
Copy link
Member

Choose a reason for hiding this comment

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

missing lib/?

oliviertassinari added a commit that referenced this pull request Nov 18, 2015
[List/ListItem] Introduce selected property #1581
@oliviertassinari oliviertassinari merged commit e1aed16 into mui:master Nov 18, 2015
@oliviertassinari
Copy link
Member

@frankf-cgn Thank you!

@frankf-cgn
Copy link
Author

@oliviertassinari @shaurya947 I have to thank you both!

@shaurya947
Copy link
Contributor

Yay! Great job @frankf-cgn

@frankf-cgn frankf-cgn deleted the issue/1581 branch November 20, 2015 07:23
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Nov 10, 2020
@zannager zannager added the component: list This is the name of the generic UI component, not the React module! label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: list This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants