1

I am working on a Lightning Web Component (LWC) where users can search and select multiple Contacts. Currently, the selected Contacts are displayed below the input field, but I want to show them inside the input field itself.

My HTML code:

<template>
    <div class="slds-form-element">
        <label class="slds-form-element__label">Select Contacts</label>
        <div class="slds-form-element__control">
            <input
                type="text"
                class="slds-input"
                placeholder="Search Contacts..."
                oninput={handleSearch}
                value={searchTerm}
            />
            <template if:true={isSearching}>
                <p>Searching...</p>
            </template>
            <template if:false={isSearching}>
                <ul class="slds-listbox slds-listbox_vertical">
                    <template for:each={contacts} for:item="contact">
                        <li
                            key={contact.Id}
                            data-id={contact.Id}
                            class="slds-listbox__item"
                            onclick={handleSelect}
                        >
                            {contact.Name} ({contact.Email})
                        </li>
                    </template>
                </ul>
            </template>
        </div>
        <div class="slds-m-top_medium">
            <template for:each={selectedContacts} for:item="contact">
                <div key={contact.Id} class="slds-p-around_small slds-border_bottom">
                    <span>{contact.Name} ({contact.Email})</span>
                    <button
                        class="slds-button slds-button_icon slds-button_icon-small"
                        data-id={contact.Id}
                        onclick={handleRemove}
                    >
                        <lightning-icon icon-name="utility:close" size="small"></lightning-icon>
                    </button>
                </div>
            </template>
        </div>
    </div>
</template>

How can I modify my LWC to display selected Contacts inside the input field, while keeping the input field functional for further search?

Any guidance would be greatly appreciated!

1 Answer 1

0

One solution would be to replace your current input structure with an slds-pill_container pattern, which is specifically designed for this type of multi-select functionality.

pillContainerContactSearch

The slds-pill_container works pretty well here - it's designed to contain both pills and an input field together in the same container. The input gets the flex: 1 property, allowing it to share space with the pills. This approach follows Salesforce's design patterns for multi-select input fields and has worked well for me historically.

<div class="slds-combobox_container slds-has-selection">
    <div class="slds-combobox__form-element">
        <div class="slds-pill_container">
            <!-- Selected contact pills -->
            <template for:each={selectedContacts} for:item="contact">
                <span key={contact.Id} class="slds-pill">
                    <span class="slds-avatar slds-avatar_x-small slds-avatar_circle slds-m-right_xx-small">
                        <lightning-icon icon-name="standard:contact" size="x-small"></lightning-icon>
                    </span>
                    <span class="slds-pill__label" title={contact.Name}>{contact.Name}</span>
                    <button class="slds-button slds-button_icon slds-pill__remove" 
                            data-id={contact.Id} 
                            onclick={handleRemove}>
                        <lightning-icon icon-name="utility:close" size="x-small"></lightning-icon>
                    </button>
                </span>
            </template>
            
            <!-- Search input -->
            <input type="text"
                   class="slds-input search-input"
                   placeholder={placeholderText}
                   oninput={handleSearch}
                   value={searchTerm} />
        </div>
    </div>
</div>


slds-pill_container {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    width: 100%;
}

.search-input {
    border: none;
    height: 100%;
    min-height: 30px;
    flex: 1;
    padding: 0 0.5rem;
    background: transparent;
}
1
  • 1
    Thank you @salesforceGuidance.com for the answer. I just added the handleSelect event additionally from my previous code, then it worked for me. Commented Mar 6 at 6:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.