6

I have an lightning:input like this (already tried with not(v.customAddress)):

<form class="slds-form--inline slds-m-bottom--small">

  <lightning:select
    aura:id="sendTo"
    label="{! $Label.c.Send_To }"
    name="{! $Label.c.Send_To }"
  >
    <option value="{! null }" selected="{! true }">{! 'Select a mail' }</option>

    <aura:iteration items="{! v.addresses }" var="address" >
      <option value="{! address.id }">
              {! join(' ', join(', ',address.street, address.houseNumber), address.postalCode, address.city) }
      </option>
    </aura:iteration>

  </lightning:select>
  <lightning:buttonIcon
    name="Add selected address"
    iconName="utility:add"
    alternativeText="{! 'Select address' }"
    onclick="{! c.selectAddress }"
  />
</form>

<!-- STREET -->
<form class="slds-form--inline slds-m-bottom--small">
    <lightning:input
        aura:id="street"
        type="text"
        label="{! 'Street address' }"
        name="{! 'street' }"
        onchange="{! c.updateAddress }"
        disabled="{! !v.customAddress }"
        value="{! v.address.street }"
        required="{! true }"
    />
</form>

An aura:attributelike this:

<aura:attribute 
  name="customAddress"  
  type="Boolean"  
  default="true"  
  required="false" 
  access="private" 
/>

And a Javascript function in my controller like this:

({

  selectAddress:      function(component, event, helper) {
    const input         = component.find('sendTo'); // A lightning:select
    const addressId     = input.get('v.value'); // The default lightning:option has a {! null } value

    if (addressId && addressId.trim() !== '') {
      component.set('v.address', /* myProcessedObject */);
      component.set('v.customAddress', false); // lightning:input has to be disabled
    } else {
      component.set('v.address', {});
      component.set('v.customAddress', true); // lightning:input has to be enabled
    }

  }

})

The first time my function is called AND component.set('v.customAddress', false); is reached, field is not disabled.

Sometimes, when component.set('v.customAddress', true); is reached, the field is disabled.

Example:

Initial step: Step 1: initial state

I selected the address in the dropdown and clicked + => lightning:inputis field and disabled OK Step 2: I selected the address in the dropdown and clicked +

I selected the null value from the dropdown and clicked + => lightning:inputis cleaned but not enable NOK Step 3: I selected the null value in the dropdown and clicked +

With a different behaviour of the click function:

  selectAddress:      function(component, event, helper) {
    const input         = component.find('sendTo');
    const addressId     = input.get('v.value');

    if (addressId && addressId.trim() !== '') {
      component.find("street").set("v.disabled", true);
    } else {
      component.find("street").set("v.disabled", false);
    }

  }

Answering comment: https://salesforce.stackexchange.com/a/156171/37707:

Default state

Step one: (has to be disabled) Step one

Step two: (has to be enabled) Step two

0

3 Answers 3

0

could you just try this one to disable and enable?

Disable: component.find("street").set("v.disabled", true); 
Enable: component.find("street").set("v.disabled", false); 
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Commented Feb 3, 2017 at 20:18
0

The following should work. (The readonly attribute behaves like the disabled attribute)

<lightning:input
    aura:id="street"
    type="text"
    label="Street address"
    name="street"
    onchange="{!c.updateAddress}"
    readonly="{!not(v.customAddress)}"
    value="{!v.address.street}"
    required="true"
/>
0

I am not sure if it will work or not, but here is my suggestion:

  1. Remove the default="true" for the "customAddress" Boolean attribute (now the default value is false).
  2. According to above condition change 'disabled="{!v.customAddress }"' for lightning:input and do other changes if needed
0

You must log in to answer this question.

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