Skip to content

nve-select

En nedtrekksliste, også kjent som rullgardin eller drop-down list. Kjært barn har mange navn. TODO: Klarte ikke å sette feil-ikonet når validering feiler. Eneste måte å gjøre det på kunne ha vært å bruke ::after pseudo-element på noen av sl-select partene, men funka ikke med ikonet. Man kan bruke .focus() for å fokusere komponenten programatisk. Sjekk https://shoelace.style/getting-started/usage#methods for å se hvordan å bruke det. Kan være at i Vue applikasjon man må kjøre .focus i neste tick for å fokusere komponenten.

Arvet fra: SlSelect

html
<nve-select label="Ledetekst">
  <nve-option value="valg1">Hummer</nve-option>
  <nve-option value="valg2">Kanari</nve-option>
</nve-select>

Eksempler

Størrelse

Bruk size for å velge størrelse. small er standard.

html
<nve-select label="small">
  <nve-option value="valg1">Hummer</nve-option>
  <nve-option value="valg2">Kanari</nve-option>
</nve-select>

<nve-select label="medium" size="medium">
  <nve-option value="valg1">Hummer</nve-option>
  <nve-option value="valg2">Kanari</nve-option>
</nve-select>

<nve-select label="large" size="large">
  <nve-option value="valg1">Hummer</nve-option>
  <nve-option value="valg2">Kanari</nve-option>
</nve-select>

Obligatorisk

Bruk required for å tvinge bruker til å skrive noe og legg inn en feilmelding i errorMessage om du ikke vil ha standard-feilmeldinga. Bruk requiredlabel hvis du vil vise noe annet enn *Obligatorisk. Feltet må ligge i en <form> for at validering skal funke, og hver nve-option må ha en unik value.

html
<!-- Bruker preventDefault så ikke sida lastes på nytt når du trykker på knappen -->
<form onsubmit="event.preventDefault();">
  <nve-select label="Hva synes du?" required errorMessage="Her må du velge noe">
    <nve-option value="alt1">Ja</nve-option>
    <nve-option value="alt2">Tja</nve-option>
    <nve-option value="alt3">Njaa</nve-option>
  </nve-select>

  <nve-select label="What do you think?" required requiredLabel="*Required" errorMessage="Please answer">
    <nve-option value="alt1">Oh, yes!</nve-option>
    <nve-option value="alt2">Yes, please</nve-option>
    <nve-option value="alt3">Not so much</nve-option>
  </nve-select>

  <nve-button type="submit">Submit</nve-button>
</form>

Spor

NavnBeskrivelse
(standard)The listbox options. Must be `<sl-option>` elements. You can use `<sl-divider>` to group items visually.
labelThe input's label. Alternatively, you can use the `label` attribute.
prefixUsed to prepend a presentational icon or similar element to the combobox.
clear-iconAn icon to use in lieu of the default clear icon.
expand-iconThe icon to show when the control is expanded and collapsed. Rotates on open and close.
help-textText that describes how to use the input. Alternatively, you can use the `help-text` attribute.

Hendelser

NavnArvet fraBeskrivelse
sl-changeSlSelectEmitted when the control's value changes.
sl-clearSlSelectEmitted when the control's value is cleared.
sl-inputSlSelectEmitted when the control receives input.
sl-focusSlSelectEmitted when the control gains focus.
sl-blurSlSelectEmitted when the control loses focus.
sl-showSlSelectEmitted when the select's menu opens.
sl-after-showSlSelectEmitted after the select's menu opens and all animations are complete.
sl-hideSlSelectEmitted when the select's menu closes.
sl-after-hideSlSelectEmitted after the select's menu closes and all animations are complete.
sl-invalidSlSelectEmitted when the form control has been checked for validity and its constraints aren't satisfied.

Egenskaper

NavnAttributt-synkTypeStandardverdiArvet fraBeskrivelse
requiredLabelstring'*Obligatorisk'Tekst som vises for å markere at et felt er obligatorisk. Er satt til "*Obligatorisk" som standard.
errorMessagestring | undefinedBrukes til enkel constraint validation til å overskrive default nettleseren melding
popupSlPopupSlSelect
comboboxHTMLSlotElementSlSelect
displayInputHTMLInputElementSlSelect
valueInputHTMLInputElementSlSelect
listboxHTMLSlotElementSlSelect
displayLabelstring''SlSelect
currentOptionSlOptionSlSelect
selectedOptionsSlOption[][]SlSelect
namestring''SlSelectThe name of the select, submitted as a name/value pair with form data.
valuestring | string[]''SlSelectThe current value of the select, submitted as a name/value pair with form data. When `multiple` is enabled, the value attribute will be a space-delimited list of values based on the options selected, and the value property will be an array. **For this reason, values must not contain spaces.**
defaultValuestring | string[]''SlSelectThe default value of the form control. Primarily used for resetting the form control.
size'small' | 'medium' | 'large''medium'SlSelectThe select's size.
placeholderstring''SlSelectPlaceholder text to show as a hint when the select is empty.
multiplebooleanfalseSlSelectAllows more than one option to be selected.
maxOptionsVisiblenumber3SlSelectThe maximum number of selected options to show when `multiple` is true. After the maximum, "+n" will be shown to indicate the number of additional items that are selected. Set to 0 to remove the limit.
disabledbooleanfalseSlSelectDisables the select control.
clearablebooleanfalseSlSelectAdds a clear button when the select is not empty.
openbooleanfalseSlSelectIndicates whether or not the select is open. You can toggle this attribute to show and hide the menu, or you can use the `show()` and `hide()` methods and this attribute will reflect the select's open state.
hoistbooleanfalseSlSelectEnable this option to prevent the listbox from being clipped when the component is placed inside a container with `overflow: auto|scroll`. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios.
filledbooleanfalseSlSelectDraws a filled select.
pillbooleanfalseSlSelectDraws a pill-style select with rounded edges.
labelstring''SlSelectThe select's label. If you need to display HTML, use the `label` slot instead.
placement'top' | 'bottom''bottom'SlSelectThe preferred placement of the select's menu. Note that the actual placement may vary as needed to keep the listbox inside of the viewport.
helpTextstring''SlSelectThe select's help text. If you need to display HTML, use the `help-text` slot instead.
formstring''SlSelectBy default, form controls are associated with the nearest containing `<form>` element. This attribute allows you to place the form control outside of a form and associate it with the form that has this `id`. The form must be in the same document or shadow root for this to work.
requiredbooleanfalseSlSelectThe select's required attribute.
getTag(option: SlOption, index: number) => TemplateResult | string | HTMLElementSlSelectA function that customizes the tags to be rendered when multiple=true. The first argument is the option, the second is the current tag's index. The function should return either a Lit TemplateResult or a string containing trusted HTML of the symbol to render at the specified value.
validitySlSelectGets the validity state object
validationMessageSlSelectGets the validation message
tagsSlSelect

Metoder

NavnParametreReturtypeArvet fraBeskrivelse
firstUpdated
changedProperties: any
updated
changedProperties: any
focus
handleOptionClick
event: MouseEvent
handleDefaultSlotChange
getAllOptions
getFirstOption
handleDisabledChangeSlSelect
handleValueChangeSlSelect
handleOpenChangeSlSelect
showSlSelectShows the listbox.
hideSlSelectHides the listbox.
checkValiditySlSelectChecks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid.
getFormHTMLFormElement | nullSlSelectGets the associated form, if one exists.
reportValiditySlSelectChecks for validity and shows the browser's validation message if the control is invalid.
setCustomValidity
message: string
SlSelectSets a custom validation message. Pass an empty string to restore validity.
blurSlSelectRemoves focus from the control.

Deler

NavnBeskrivelse
form-controlThe form control that wraps the label, input, and help text.
form-control-labelThe label's wrapper.
form-control-inputThe select's wrapper.
form-control-help-textThe help text's wrapper.
comboboxThe container the wraps the prefix, combobox, clear icon, and expand button.
prefixThe container that wraps the prefix slot.
display-inputThe element that displays the selected option's label, an `<input>` element.
listboxThe listbox container where options are slotted.
tagsThe container that houses option tags when `multiselect` is used.
tagThe individual tags that represent each multiselect option.
tag__baseThe tag's base part.
tag__contentThe tag's content part.
tag__remove-buttonThe tag's remove button.
tag__remove-button__baseThe tag's remove button base part.
clear-buttonThe clear button.
expand-iconThe container that wraps the expand icon.