Power Apps Component With An OnSelect Property

Power Apps Component With An OnSelect Property



This week Power Apps components received an important update: they now support behavior properties such as OnSelect & OnChange. Ever since components were released it was seen as missing feature. Power Apps developers had to resort to work-arounds to trigger code in an app based on when a behavior-property fired. Well, no more. The true potential of canvas app components is now unlocked!


In this article I will teach you how to build a Power Apps Component with an OnSelect property.

Table Of Contents
Introduction: Pop-Up Menu Component
Enable Enhanced Component Properties
Add A Behavior Property For The Cancel Button
Cancel Button OnSelect Property In The App
Add A Behavior Property For The OK Button
OK Button OnSelect Property In The App




Introduction: Pop-Up Menu Component

In the sample app we will build together the user clicks a button on Screen 1 to navigate to Screen 2. When the button is clicked a pop-up menu component appears and asks “do you really want to go to the next screen?” The user can choose either OK to proceed or Cancel to stay on the same screen.




Enable Enhanced Component Properties

Behavior type custom properties are an experimental feature at this time. To use them you must open the advanced settings menu and turn on Enhanced Component Properties.




Add A Behavior Property For The Cancel Button

Open Power Apps Studio and create a new canvas app from blank. Then go to the components editor and create a new component named cmp_PopUpMenu. Insert a label onto the screen to show the Title, another label for the pop-up message and two buttons with the text OK & Cancel. You can also place another label at the bottom of the component with a dark-colored border to define its edges.



Next, create a new custom property called OnSelectCancel. Choose the Behavior property type then click Create.



In the OnSelect property of the Cancel button…



…write this code. Now when we place the pop-up menu component inside an app the OnSelect property of the cancel button will be exposed.

cmp_PopUpMenu.OnSelectCancel()




Cancel Button OnSelect Property In The App

When the user clicks a button on Screen 1 and pop-up button appears and asks “do you really want to go to Screen 2”. The next thing we will do is create Screen 1.

Open Screen 1 and add a large button in the center of the screen with the text “Go to Screen 2.”



Add this code to the OnSelect property of the button. We can use the variable it sets to true to control the pop-up menu’s visibility

UpdateContext({locShowPopUpMenu: true})



Go to the Custom button on the ribbon and select the cmp_PopUpMenu component. Place it directly over top of the button.



Change the Visible property of the component to locShowPopUpMenu. It will show the menu when the variable’s value is true and hide it when false.



Now we can write some code to tell the app what to do when the Cancel button is clicked. Select the OnSelectCancel property of the component…



…and write this code to change the locShowPopUpMenu variable to false.

UpdateContext({locShowPopUpMenu: false});



Clicking the Cancel button will cause the pop-up menu to disappear. By using a behavior property we have managed to successfully trigger the Cancel button’s OnSelect property from inside the component and update a variable value in the app!




Add A Behavior Property For The OK Button

We are half-way done. The next step is to create a behavior property for the cancel button. Go back to the component and add a new custom property called OnSelectOK. Choose the Behavior property type and click Create.



Select the OK button…



…and put this code in the OnSelect property to expose the OK button to the app.

cmp_PopUpMenu.OnSelectOK()




OK Button OnSelect Property In The App

Before we code the OK button we’ll need to create a 2nd screen. Insert a new screen and call it Screen 2.



Go back to screen one and select the pop-up menu component.



Then write this code in the OnSelectOK property.

Navigate('Screen 2', ScreenTransition.Fade);
UpdateContext({locShowPopUpMenu: false});


When the OK button is clicked it the app will take the user to Screen 2.





Questions?

If you have any questions or feedback about Power Apps Component With An OnSelect Property please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest
12 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Sean Hodkinson
Sean Hodkinson
1 year ago

Thank you Matthew, very cool feature.

Ben Rattigan
Ben Rattigan
1 year ago

Thanks for this although even after turning this on in Enhanced Component Features the behaviour option still isnt available for me.

Joan
Joan
4 months ago

thanks!!!!, forgive my google English, I had been looking for exactly this for more than fifteen days, but… I want to put the buttons in a gallery, I pass the texts with a Collection where I also put the possible OnSelect that it has to execute on each button, but it ignores me. Can be done? greetings and thanks

Joan
Joan
3 months ago

Hi Matthew, I send you a summary of what I’m trying to do, put the code instead of the number, so you don’t have to remember which number corresponds to each option each time, greetings and thanks,

PopUp.png
Scott Rowlan
Scott Rowlan
3 months ago

Hi Matthew, how would I add an OnChange for a Component? I have a Calendar Component and I want it set that if the date changes to reset a dropdown.

Thanks.

Ben Gin
Ben Gin
3 months ago

Is it possible to Update a Global or Context Variable in the App that isn’t in the Common Component? I want the “OnSelect” and sometimes the “OnChange” to update a variable in the App. I also want to reuse this component many times in the App, and therefore, I need the Global or Context Variable name as an input parameter such that each instance of this Common Component will update different Global or Context Variables.

Janson Chacko
Janson Chacko
2 months ago

Hello Mathew, cool feature and nicely explained. Thanks.

Btw, this is still considered as “experimental” with the disclosure that this may “change, break or disappear any time”. Do you know if this would move into “Preview” and eventually Production ready?