Open ndtreviv opened 5 years ago
I've also tried:
anchorElement={this.refs.input && this.refs.input.inputElement}
Same behaviour. I think it's because the anchor element is null first time around.
Looks like the the first time around, the dom element is there, but the coordinates are wrong:
Ok, now I'm thinking it's because the whole lot is in a Dialog, which is still "opening" when the DOM reference is obtained
That wasn't it. Now I'm setting the anchor on componentDidMount.
I've even tried using coordinates instead:
if (!this.state.listCoordinates) {
const rect = ReactDOM.findDOMNode(this.inputRef.current).getBoundingClientRect();
const listCoordinates = {
x: rect.x,
y: rect.y + rect.height
}
this.setState({listCoordinates: listCoordinates});
}
The coordinates come out as null on first render, obviously, and then correct on second render, but even after second render the menu still doesn't render in the right place the first time around.
First time it opens there is no top
in the inline style:
transform-origin: center bottom; left: 440px; bottom: 499px; max-height: 388px;
Second time it opens, top is correct. So it's clearly getting left correctly, but not top?
If I take off the anchorCorner property, it renders aligned top top-left of the TextField element first time. This would be ok, except that it's overlaying the TextField.
If I set anchorCorner to TOP_RIGHT
then it's fine as well. Just BOTTOM_
options are screwed in this case.
If I leave off anchorCorner (so that it would anchor to the TOP_LEFT Corner of the TextField), and then set anchorMargin as:
anchorMargin={{top: 56, bottom: 0, left: 0, right: 0}}
that also doesn't work. It just renders like the first picture in the original post.
At the moment, the hacky workaround is to set the damn style myself:
style={{left: anchorElement.getBoundingClientRect().x, top: anchorElement.getBoundingClientRect().y + anchorElement.getBoundingClientRect().height}}
I'm off for a drink 🤪
@ndtreviv https://github.com/material-components/material-components-web-react/issues/913#issuecomment-499436789 this might be because we have a default anchor rect element, but once the anchor element is created, it should take the default's place. Do you have a test project that you're testing this all in that you can share?
Code
First time I click the Input or the trailing icon (anchoring is incorrect):
Subsequent clicks (anchoring is correct):
I followed the example in the docs for this, except for this bit:
which was incorrect in the docs, as they were setting
anchorElement
to just beelement
.Debug shows that the anchorElement doesn't change (when I rollover both it highlights the same input):