I have a below combobox in the HTML:
<lightning-layout>
<lightning-layout-item>
<lightning-combobox class="slds-cell-wrap slds-text" label="combo-box" options={comboOptions}>
</lightning-combobox>
</lightning-layout-item>
</lightning-layout>
JS:
comboOptions = [{ value: 'This is a very very long label', label: 'This is a very very long label' },
{ value: 'This is a very very long label', label: 'This is a very very long label' },
{ value: 'This is a very very long label', label: 'This is a very very long label' },
{ value: 'This is a very very long label', label: 'This is a very very long label' },
{ value: 'This is a very very long label', label: 'This is a very very long label' },
];
it renders like below:
If the option string is longer then it truncates the string, I tried using slds-cell-wrap but still options are being truncated instead of wrapping, could someone please suggest how to wrap the combobox options.
Update
using below component from the lightning base component recipe doesn't have an option property instead tried to set items property but it shows something like below
Please confirm if is there any way to wrap the option text of combobox
Best Answer
Consider building a custom combobox component, if you need text wrapping. Easiest approach would be to create a replica of the base components involved. You can check out the source code here for
baseCombobox
&baseComboboxItem
components. A one line code change (i.e., remove theslds-truncate
) in your replicated custom component'sinline.html
file ofbaseComboboxItem
should work. Note that I've personally not tried this out, but should be possible.You cannot use class attribute (CSS) on the LWC base component (combobox) or unless SF provides a new attribute (or some kind of variant). Reason being,
As mentioned in point # 3, the slds you specify in class attribute doesn't get applied to the inner elements of the component.
Combobox would get rendered something like this in chrome (below HTML shows only for one element). Note that innermost span element has the
slds-truncate
which causes option labels to get truncated. Point # 2 mentioned above recommends not to depend on this current HTML structure for styling.