Skip to content

nve-input

Et tekstfelt. Vil du ha info-ikon med hjelpetekst etter ledeteksten, putt en nve-label i label-slot. pill skal ikke brukes. TODO: Feltet blir bredere hvis validering feiler, fordi vi må ha plass til feil-ikonet.

Arvet fra: SlInput

html
<nve-input label="Har kan du skrive akkurat hva du vil"></nve-input>

Eksempler

Mørk bakgrunn

Bruk filled for mørk bakgrunnsfarge

html
<nve-input value="filled" filled></nve-input>

<nve-input value="ikke filled"></nve-input>

Verktøyhint

nve-input kan ha ledetekst med verktøyhint hvis du putter ledeteksten i sporet label

html
<nve-input>
    <nve-label value="Svev over ikonet" slot="label" tooltip="Hjelpetekst"><nve-label>
</nve-input>

Sletteknapp

Bruk clearable for å vise en sletteknapp bak teksten

html
<nve-input value="Slett dette ved trykke ikonet til høyre" clearable></nve-input>

Deaktivering

Bruk disabled for å deaktivere feltet. Du kan også vise et låse-ikon ved å legge nve-icon i sporet suffix.

html
<nve-input value="aktiv"></nve-input>
<nve-input disabled value="deaktivert"></nve-input>
<nve-input disabled value="deaktivert med ikon">
  <nve-icon slot="suffix" name="Lock"></nve-icon>
</nve-input>

Skrivebeskyttet

Bruk readonly for å stenge mulighet for å endre innholdet

html
<nve-input readonly value="Dette får du ikke endret"></nve-input>

<nve-input value="men dette kan du endre"></nve-input>

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.

html
<!-- Bruker preventDefault så ikke sida lastes på nytt når du trykker på knappen -->
<form onsubmit="event.preventDefault();">
  <nve-input label="Hva synes du?" required errorMessage="Her må du skrive noe"></nve-input>
  <nve-input label="What do you think?" required requiredLabel="*Required" errorMessage="Please answer"></nve-input>
  <nve-button type="submit">Submit</nve-button>
</form>

Constraint validation

I tillegg til required støtter vi de samme reglene som SlInput, men pass på at du setter en feilmelding i errorMessage. Feltet må ligge i en <form> for at validering skal funke. Her er noen eksempler.

html
<!-- Bruker preventDefault så ikke sida lastes på nytt når du trykker på knappen -->
<form onsubmit="event.preventDefault();">
  <nve-input
    required
    minlength="3"
    maxLength="5"
    label="Minimum 3 og maks 5 tegn"
    errorMessage="Mellom 3 og 5 tegn her, takk"
  ></nve-input>

  <nve-input
    required
    type="number"
    min="42"
    max="42"
    label="Svaret på livet, universet og alt"
    errorMessage="Helt feil!"
  ></nve-input>

  <nve-input
    required
    type="date"
    min="1980-01-01"
    max="1989-12-31"
    label="Velg en dato på 80-tallet"
    errorMessage="Her er det bare 80-tallet som gjelder"
  ></nve-input>

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

Datatyper

I tillegg til tekst, støtter vi de samme datatypene som SlInput.

step i kombinasjon med number helper deg å håndterer desimaltall. Du kan bruke både komma og punktum som desimalskilletegn. Her er noen eksempler:

html
<nve-input type="number" label="Kun heltall"></nve-input>
<nve-input type="number" step="0.1" label="Maks en desimal"></nve-input>
<nve-input type="number" step="any" label="Bruk så mange desimaler du vil"></nve-input>
<nve-input type="datetime-local" label="Skriv inn eller velg dato og tidspunkt"></nve-input>

Størrelse

Bruk size for å velge høyde. Ledeteksten tilpasses automatisk, mens skrifttypen til innholdet er fast uansett. medium er standard.

html
<nve-input size="small" label="small" value="innhold"></nve-input>
<nve-input label="medium" value="innhold"></nve-input>
<nve-input size="large" label="large" value="innhold"></nve-input>

Spor

NavnBeskrivelse
labelThe input's label. Alternatively, you can use the `label` attribute.
prefixUsed to prepend a presentational icon or similar element to the input.
suffixUsed to append a presentational icon or similar element to the input.
clear-iconAn icon to use in lieu of the default clear icon.
show-password-iconAn icon to use in lieu of the default show password icon.
hide-password-iconAn icon to use in lieu of the default hide password icon.
help-textText that describes how to use the input. Alternatively, you can use the `help-text` attribute.

Hendelser

NavnArvet fraBeskrivelse
sl-blurSlInputEmitted when the control loses focus.
sl-changeSlInputEmitted when an alteration to the control's value is committed by the user.
sl-clearSlInputEmitted when the clear button is activated.
sl-focusSlInputEmitted when the control gains focus.
sl-inputSlInputEmitted when the control receives input.
sl-invalidSlInputEmitted 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
errorMessagestring | undefinedFeilmelding som vises hvis validering feiler
testIdstring''Brukes for å kunne identifisere komponenten i tester
inputIdstring | undefinedBrukes for å kunne identifisere komponenten
alreadyInvalidbooleanfalse
inputHTMLInputElementSlInput
titlestring''SlInput
type| 'date' | 'datetime-local' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'time' | 'url''text'SlInputThe type of input. Works the same as a native `<input>` element, but only a subset of types are supported. Defaults to `text`.
namestring''SlInputThe name of the input, submitted as a name/value pair with form data.
valuestring''SlInputThe current value of the input, submitted as a name/value pair with form data.
defaultValuestring''SlInputThe default value of the form control. Primarily used for resetting the form control.
size'small' | 'medium' | 'large''medium'SlInputThe input's size.
filledbooleanfalseSlInputDraws a filled input.
pillbooleanfalseSlInputDraws a pill-style input with rounded edges.
labelstring''SlInputThe input's label. If you need to display HTML, use the `label` slot instead.
helpTextstring''SlInputThe input's help text. If you need to display HTML, use the `help-text` slot instead.
clearablebooleanfalseSlInputAdds a clear button when the input is not empty.
disabledbooleanfalseSlInputDisables the input.
placeholderstring''SlInputPlaceholder text to show as a hint when the input is empty.
readonlybooleanfalseSlInputMakes the input readonly.
passwordTogglebooleanfalseSlInputAdds a button to toggle the password's visibility. Only applies to password types.
passwordVisiblebooleanfalseSlInputDetermines whether or not the password is currently visible. Only applies to password input types.
noSpinButtonsbooleanfalseSlInputHides the browser's built-in increment/decrement spin buttons for number inputs.
formstring''SlInputBy 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.
requiredbooleanfalseSlInputMakes the input a required field.
patternstringSlInputA regular expression pattern to validate input against.
minlengthnumberSlInputThe minimum length of input that will be considered valid.
maxlengthnumberSlInputThe maximum length of input that will be considered valid.
minnumber | stringSlInputThe input's minimum value. Only applies to date and number input types.
maxnumber | stringSlInputThe input's maximum value. Only applies to date and number input types.
stepnumber | 'any'SlInputSpecifies the granularity that the value must adhere to, or the special value `any` which means no stepping is implied, allowing any numeric value. Only applies to date and number input types.
autocapitalize'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters'SlInputControls whether and how text input is automatically capitalized as it is entered by the user.
autocorrect'off' | 'on'SlInputIndicates whether the browser's autocorrect feature is on or off.
autocompletestringSlInputSpecifies what permission the browser has to provide assistance in filling out form field values. Refer to [this page on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) for available values.
autofocusbooleanSlInputIndicates that the input should receive focus on page load.
enterkeyhint'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'SlInputUsed to customize the label or icon of the Enter key on virtual keyboards.
spellcheckbooleantrueSlInputEnables spell checking on the input.
inputmode'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url'SlInputTells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices.
valueAsDateSlInputGets or sets the current value as a `Date` object. Returns `null` if the value can't be converted. This will use the native `<input type="{{type}}">` implementation and may result in an error.
valueAsNumberSlInputGets or sets the current value as a number. Returns `NaN` if the value can't be converted.
validitySlInputGets the validity state object
validationMessageSlInputGets the validation message

Metoder

NavnParametreReturtypeArvet fraBeskrivelse
firstUpdated
updated
changedProperties: any
updateInputId
makeInvalid
resetValidation
showErrorMessage
showErrorIcon
hideErrorIcon
handleDisabledChangeSlInput
handleStepChangeSlInput
handleValueChangeSlInput
focus
options: FocusOptions
SlInputSets focus on the input.
blurSlInputRemoves focus from the input.
selectSlInputSelects all the text in the input.
setSelectionRange
selectionStart: number
selectionEnd: number
selectionDirection: 'forward' | 'backward' | 'none'
SlInputSets the start and end positions of the text selection (0-based).
setRangeText
replacement: string
start: number
end: number
selectMode: 'select' | 'start' | 'end' | 'preserve'
SlInputReplaces a range of text with a new string.
showPickerSlInputDisplays the browser picker for an input element (only works if the browser supports it for the input type).
stepUpSlInputIncrements the value of a numeric input type by the value of the step attribute.
stepDownSlInputDecrements the value of a numeric input type by the value of the step attribute.
checkValiditySlInputChecks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid.
getFormHTMLFormElement | nullSlInputGets the associated form, if one exists.
reportValiditySlInputChecks for validity and shows the browser's validation message if the control is invalid.
setCustomValidity
message: string
SlInputSets a custom validation message. Pass an empty string to restore validity.

Deler

NavnBeskrivelse
form-controlThe form control that wraps the label, input, and help text.
form-control-labelThe label's wrapper.
form-control-inputThe input's wrapper.
form-control-help-textThe help text's wrapper.
baseThe component's base wrapper.
inputThe internal `<input>` control.
prefixThe container that wraps the prefix.
clear-buttonThe clear button.
password-toggle-buttonThe password toggle button.
suffixThe container that wraps the suffix.