mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.74k stars 32.24k forks source link

List API component initialized with múltiple ListItem components sets initial scroll position to the bottom, no clear way to set it to top #24164

Closed javismiles closed 3 years ago

javismiles commented 3 years ago
class PickModal extends React.Component {
    constructor() {
      super();
      this.pickRef = React.createRef();
    }

    componentDidMount() {
      console.log(this.pickRef.current); // DOM node
      if (this.pickRef.current){
        console.log("scrolltop: "+this.pickRef.current.scrollTop);
      }
    }

    componentDidUpdate() {
      console.log(this.pickRef.current); // DOM node
      if (this.pickRef.current){
        console.log("scrolltop: "+this.pickRef.current.scrollTop);
      }

    }

   handleListItemClick = (value, launcherfunc) => {
    console.log(this.pickRef);
    if (this.pickRef.current){
      console.log("handleListItemClick ref: "+this.pickRef.current.scrollTop);
    }

  };

   thelist =  models.map((model) => (
      <ListItem button onClick={() => this.handleListItemClick(model, this.props.launcherfunc)} key={model}>
        <ListItemAvatar>
          <Avatar>
            <LandscapeIcon />
          </Avatar>
        </ListItemAvatar>
        <ListItemText primary={model} />
      </ListItem>
    ));

render() {
  return (
    <RootRef rootRef={this.pickRef}>
    <Dialog onClose={this.props.closeModelsModal} aria-labelledby="simple-dialog-title" open={this.props.modalModels}>
      <DialogTitle id="simple-dialog-title">Choose model</DialogTitle>
      <List > 
        {this.thelist}
      </List>
    </Dialog>
    </RootRef>
  );
}
}

const mapStateToProps = (state) => {
  return { modalModels:state.modalModels };
};

export default connect(mapStateToProps, { closeModelsModal})(PickModal);
support[bot] commented 3 years ago

👋 Thanks for using Material-UI!

We use GitHub issues exclusively as a bug and feature requests tracker, however, this issue appears to be a support request.

For support, please check out https://material-ui.com/getting-started/support/. Thanks!

If you have a question on StackOverflow, you are welcome to link to it here, it might help others. If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

javismiles commented 3 years ago

@oliviertassinari Thank you, I switched to normal ref yes, the main issue seems to be that componentDidUpdate() is not firing after render of the material-ui list is completed, but before. I can prove this by using plain javascript: var scrollPos= document.getElementsByClassName("MuiDialog-paper"); This returns undefined after componentDidUpdate fires. Yet it returns the correct value later on after a few seconds. It seems to me that componentDidUpdate is firing incorrectly in conjuntion with this material-ui component thank you

javismiles commented 3 years ago

This is the way I made it work

    componentDidUpdate() {
      setTimeout(function(){ 
        var scrollPos= document.getElementsByClassName("MuiDialog-paper");
        if (scrollPos.length>0){scrollPos[0].scrollTop=0;}
      }, 200);
}

certainly a very absurd way, but the only way I found. Seems like the rendering of the list goes beyond useEffect, componentDidUpdate etc firings, so the only way is to wait x time to then check things out, etc

It would be great if the documentation maybe provided some suggestion to detect the full completed rendering of material-ui components such as List, etc

thank you