I am trying to enable a component to focus a particular nested child component (which happens to have been placed in it's facet by a parent component) upon some action happening in the component.
Primary Component Markup: primaryComponent.cmp
<aura:component>
<aura:attribute name="editor" type="Aura.Component[]"/>
<aura:attribute name="focusId" type="String"/>
{!v.body}
<lightning:button label="Focus It" onclick="{!c.onFocusButtonClick}"/>
{!v.editor}
</aura:component>
Primary Component Controller: primaryComponent.js
({
onFocusButtonClick: function(component, event, helper){
var focusId = component.get("v.focusId");
var editorInput = component.find(focusId);
/*var editorComponents = component.get("v.editor");
editorInput = editorComponents.find(component => {
return component.getLocalId() == focusId;
});*/
editorInput.focus();
}
})
Parent Component: parentComponent.cmp
<aura:component>
<c:primaryComponent focusId="focusPoint">
This is the body!
<aura:set attribute="editor">
<span>
<lightning:input aura:id="focusPoint" value="{!v.theValue}"/>
</span>
</aura:set>
</c:primaryComponent>
</aura:component>
I have included the "editor" facet here because it's what we are actually doing. I don't think there is any difference vs doing this within the "body" facet, but I didn't want to not give that information either.
Should component.find not work in this situation? Is it limited to use in the component where the aura:id is declared?
I have found one workaround. Just searching the facet for the component by .getLocalId(). The commented lines represent that workaround. They do work as long as the component is not nested. If the component rests directly within the facet (remove the span) this works. Once the component is nested inside the span, it does not work. So, I'm looking for a solution that does work when the component is nested.
I know that the aura:id of dynamically components cannot currently be accessed.
I don't think that's the same issue though. This is not dynamic component creation, it's just static parent component creation and addition to a child's facet. I'm just not sure if the child is intended to be able to access the component via the local id in this situation, or not. Perhaps the issue is parental insertion amounts to the same thing as dynamic creation. I'm not sure if that is true or not.