In this blog post series, we will break down the development of a Multiselect Picklist Lightning Web Component (LWC) from scratch. We will cover each step in detail, including how to handle event bubbling to close the dropdown when clicking outside the component. By the end of this series, you’ll have a fully functional multiselect picklist ready for your Salesforce projects.
Before we dive into the making our hands dirt, let’s declare the variables that we are gonna use for this component.
Explanation:
We are using getter and setter for the value property as this might change from the parent, and we want to react to the changes which are made from the parent. Another reason is to split the value string which would come as semi-colon separated values.
recordTypeId is declared with value 012000000000000AAA which would help us fetch the master picklist value when it was not provided. If we need to fetch picklist values for particular record type then this property need to be sent from the parents.
Fetch Picklist Values
We use the lightning/uiObjectInfoApi module’s getPicklistValues to fetch the picklist options dynamically. The values are wired to our component using a reactive variable.
Explanation: This method wires the picklist values from the server to our component, updating the options to checked based on values we get from the parent.
Define HTML Template
Lets write the HTML for input element and drop down container with the associated properties and event handlers.
Handle Toggle Dropdown
One of the critical functionality of this component is the dropdown behavior. The dropdown should remain open when interacting with the component and close when clicking anywhere outside the component. We achieve this by leveraging event bubbling. There might be other ways too, like by stopping the event propagation, but I prefer this way as it seems easy to understand and maintain.
Explanation:
Event Bubbling is a mechanism where an event propagates up from the innermost element to the outer elements. We listen for a click event on the entire document and decide whether to close the dropdown based on where the click originated. More on event bubbling - Geeksforgeeks (Event Bubbling).
If the click happens inside the component, we prevent the dropdown from closing. If it happens outside, we hide the dropdown. This is controlled via the clickedOnComponent flag.
Handle User Inputs
When the user clicks on the input box, we show the dropdown with all available picklist values. As they type, we filter the picklist values based on the input values.
Explanation: This method captures user input, filters the picklist options, and updates the options.
Toggle Values
When a user selects an option, we either add or remove the value from the selected list.
Explanation:
The handleListClick method handles the event when a user clicks on an option.
The selectPicklistValue method updates the values and send custom event to parent.
The toggleChecked method updates the options by toggling the matching option.
Display Selected Values
The selected values are displayed as pills below the input box. Each pill has a close icon to remove the value from the list.
The user can remove values by clicking on the close icon, which triggers the handleCrossIconClick method.
Styling the Component
We want the input element, dropdown and pills to appear neatly aligned, with scrollable options for dropdown.
Here’s the CSS file that we need:
Explanation:
We give the dropdown container a subtle shadow for better visual separation from the page.
The max-height property ensures that if there are many options, the list doesn’t extend too far down the page. Instead, it scrolls within a defined height.
We also add hover effects on list items for visual feedback.
Conclusion
In this post, we walked through the structure and logic of building a multiselect picklist in Salesforce LWC. We covered how to handle picklist values, filter options based on user input, manage selected values, and control dropdown visibility using event bubbling.