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.

Denne komponenten bygger på en Shoelace-komponent. Sjekk også dokumentasjonen i Shoelace for å få full oversikt over hvordan komponenten funker.

Feil / oppgaver / PR

Ingen som vi vet om. Hvis du finner noe muffens, registrer en feil under Issues i Github og merk den med nve-select.

 

Se også nve-option

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

Eksempler

Hente eller sette verdi

Bruk value for å hente valgt verdi eller velge noe på forhånd.

html
<nve-select value="valg2" label="Endre denne hvis du ikke vil ha foreslått valg">
  <nve-option value="valg1">Valg 1</nve-option>
  <nve-option value="valg2">Valg 2</nve-option>
  <nve-option value="valg3">Valg 3</nve-option>
</nve-select>

Mørk bakgrunn

Bruk filled for mørk bakgrunnsfarge

html
<nve-select value="filled" filled>
  <nve-option value="valg1">Valg 1</nve-option>
  <nve-option value="valg2">Valg 2</nve-option>
  <nve-option value="valg3">Valg 3</nve-option>
</nve-select>

Flervalg

Bruk multiple for å tillate valg av flere verdier. Bruk clearable for å gjøre det enkelt å fjerne valgene.

html
<nve-select multiple clearable label="Her kan du velge flere">
  <nve-option value="valg1">Hummer</nve-option>
  <nve-option value="valg2">Kanari</nve-option>
</nve-select>

Bruk value for å velge noe på forhånd. I html skilles valgene med mellomrom, så da kan ikke bruke mellomrom i nve-option sin value. I Javascript vil value være en tabell i stedet, så da er ikke mellomrom noen utfordring.

html
<nve-select multiple clearable value="valg1 valg2" label="Her kan du velge flere">
  <nve-option value="valg1">Valg 1</nve-option>
  <nve-option value="valg2">Valg 2</nve-option>
  <nve-option value="valg3">Valg 3</nve-option>
</nve-select>

Bruk max-options-visible for å begrense hvor mange valg du vil vise.

Størrelse

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

html
<nve-select label="small" size="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>

Bruk ulik tekst for select og option

Bruk textLabel i nve-option dersom du ønsker at teksten som vises i selve select-feltet ikke skal være all teksten som er i option.

html
<nve-select value="valg2" label="Endre denne hvis du ikke vil ha foreslått valg" multiple>
  <nve-option value="valg1" textLabel="Valg 1">
    <span>Valg 1</span> - <span>Denne teksten vil ikke vises når denne er valgt</span>
  </nve-option>
  <nve-option value="valg2"><span>Valg 2</span> - <span>Denne teksten blir med når den er valgt</span></nve-option>
  <nve-option value="valg3">Valg 3</nve-option>
</nve-select>

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.
suffixUsed to append 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

NavnTypeArvet fraBeskrivelseReflected
requiredLabelstring = '*Obligatorisk'Tekst som vises for å markere at et felt er obligatorisk. Er satt til "*Obligatorisk" som standard.
errorMessagestring | undefined Brukes til enkel constraint validation til å overskrive default nettleser-melding
slInvalidEventListener
valueHasChangedboolean = false
popupSlPopup SlSelect
comboboxHTMLSlotElement SlSelect
displayInputHTMLInputElement SlSelect
valueInputHTMLInputElement SlSelect
listboxHTMLSlotElement SlSelect
displayLabelstring = ''SlSelect
currentOptionSlOption SlSelect
selectedOptionsSlOption[] = []SlSelect
namestring = ''SlSelectThe name of the select, submitted as a name/value pair with form data.
value 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.
multipleboolean = falseSlSelectAllows more than one option to be selected.
maxOptionsVisiblenumber = 3SlSelectThe 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.
disabledboolean = falseSlSelectDisables the select control.
clearableboolean = falseSlSelectAdds a clear button when the select is not empty.
openboolean = falseSlSelectIndicates 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.
hoistboolean = falseSlSelectEnable 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.
filledboolean = falseSlSelectDraws a filled select.
pillboolean = falseSlSelectDraws 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.
requiredboolean = falseSlSelectThe select's required attribute.
getTag(option: SlOption, index: number) => TemplateResult | string | HTMLElement SlSelectA 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.
validity SlSelectGets the validity state object
validationMessage SlSelectGets the validation message
tags SlSelect

Metoder

NavnParametreReturtypeArvet fraBeskrivelse
focus
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, suffix, combobox, clear icon, and expand button.
prefixThe container that wraps the prefix slot.
suffixThe container that wraps the suffix 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.