2

I am able to dynamically create a lightning:radioGroup component, but when I add the 'onchange' property it fails to render. As soon as I remove that property it works again. I get the same error with 'onfocus', 'onblur'. These are all valid properties defined in the documentation for lightning:radioGroup.

I should note that I am able to successfully assign a function reference to the 'onclick' property of a lightning:buttonStateful in the same way.

The the error is "Action failed: c:radioButtonTest$controller$doInit [h.descriptor is undefined]".

I have seen this in two different orgs. Is this a bug?

Is there a workaround short of creating a listener for the radiobuttons? I need to create them dynamically because I need to assign a dynamic aura:id.

component:

<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
    <aura:attribute name="radioGroupOptions" type="List" default="[{'label': 'Button One', 'value': 'one'},{'label': 'Button Two', 'value': 'two'}]"/>
    <aura:attribute name="radioGroup" type="Aura.Component" />
    <aura:attribute name="radioGroupValue" type="String" default="one"/>

    <aura:attribute name="stateButton" type="Aura.Component" />
    <aura:attribute name="buttonState" type="Boolean" default="false"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <div>{!v.radioGroup}</div>
    <div>{!v.stateButton}</div>
</aura:component>

controller:

({
    doInit : function(component, event, helper) {
            $A.createComponent("lightning:radioGroup",
            {
                "aura:id": "findableAuraId",
                "name": "radioGroup1",
                "label": "Radio Group Test",
                "type": "button",
                "options": component.getReference("v.radioGroupOptions"),
                "value": component.getReference("v.radioGroupValue"),
                "onchange": component.getReference("c.onRadioGroupChange")
            },                      
            function(newRadioGroup, status, errorMessage){
                if (status === "SUCCESS") {
                    component.set("v.radioGroup", newRadioGroup);
                }
            }
        );

            $A.createComponent("lightning:buttonStateful",
            {
                "aura:id": "findableAuraId",
                "name": "button1",
                "variant": "neutral",
                "labelWhenOff": "Off",
                "labelWhenOn": "On",
                "labelWhenHover": "Change",
                "title": "On-Off Button",
                "state": component.getReference("v.buttonState"),
                "onclick": component.getReference("c.onButtonChange")
            },                      
            function(newButton, status, errorMessage){
                if (status === "SUCCESS") {
                    component.set("v.stateButton", newButton);
                }
            }
        );

    },

    onRadioGroupChange : function(component, event, helper) {
        console.log('changing...');
    },

    onButtonChange : function(component, event, helper) {
        var buttonstate = component.get('v.buttonState');
        component.set('v.buttonState', !buttonstate);
    }
})

1 Answer 1

0

You can dynamically add events to the components using

addEventHandler(String event, Function handler, String phase, String includeFacets)

According to SF documentation

You can also add an event handler to a component that is created dynamically in the callback function of $A.createComponent(). For more information, see Dynamically Creating Components.

Source: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_dynamic_handler.htm

I havent tried but according to SF documentation it should work, you can give it a shot and tell how that goes.

1
  • Thank you Pranay - I should have noted that this same approach is working for the 'onclick' property of a statefulButton. I've updated my question with that information. I'd rather not add an event handler if I don't have to, but this seems like a bug I'll have to work around.
    – eagano
    Commented May 11, 2018 at 22:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .