-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Site Selector: fix bug with highlighted site refs #20783
Conversation
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.
This looks good based on my manual testing. Keyboard navigation works as expected and I can no longer reproduce the issue using the same steps I tried before.
@@ -293,11 +301,20 @@ class SiteSelector extends Component { | |||
return siteElements; | |||
} | |||
|
|||
setHighlightedSiteRef = isHighlighted => component => { | |||
if ( isHighlighted && component ) { |
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.
Have we tried calling this.isHighlighted() a second time so we can avoid generating functions in render?
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.
Yeah, I spent some time thinking about how to avoid generating functions in render. However, as you can see, this.isHighlighted
accepts siteId
. So, whether a site component is highlighted or not depends on which siteId it is. We would have to create the same number of functions as there are sites in order to not have to supply siteId
.
Or am I missing something?
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.
Ah ok there are two usages, so we'd need one function for all sites, and another for site. I believe site should be available as a prop? Still might be worth giving it a try.
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.
Well, site is not available as a prop because this component (SiteSelector
) renders all the sites. A site is supplied to the renderSite
function from renderSites
.
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.
ah got it, the rest of the code looked fine btw, so if manual tests are good, feel free to 🚢
Today, I noticed a bug with
SiteSelector
: if you have two sites which end in numbers (likelamostytransfer1280.blog
andlamostytransfer1281.blog
) and you type in80
or81
into site selector, then select the site and then open site selector and remove the numbers, it will break Calypso: http://cld.wthms.co/bHCnSh.@rachelmcr reported that: "I get the error when I clear any search that’s 4 characters or less (regardless of whether it’s letters or numbers)" and @bperson "I get a similar React error: “Unable to find node on an unmounted component.” if I clear the text from the switch site filter input ( by clicking on the right cross )".
It's not 100% deterministic and sometimes doesn't happen but I could reproduce it pretty reliably.
The error is about using
ReactDom.findDOMNode
on an already unmounted React component. When using keyboard or mouse navigation to scroll through sites inSiteSelector
, we were assigning thethis.refs.highlightedSite
with the currently highlighted site. It's necessary to have a ref to this component because when we navigate the list with keyboard (arrow down or up), we want the items to move (withscrollIntoView
). This was introduced in #5808 by @marekhrabe.We could fix it by wrapping
ReactDom.findDOMNode
in atry...catch
. However, I think we should not usefindDOMNode
on unmounted components and also using string refs throughthis.refs
is deprecated. Therefore, I decided to fix it by adding a fnsetHighlightedSiteRef
which will assign the currently selected site component into thethis.highlightedSiteRef
class property. If the currently selected component is being unmounted , we assignnull
so we prevent the error.Testing
Play with the
SiteSelector
: try to repro the original bug, try using keyboard navigation (it should move the list when navigating to a site that's not currently visible).