Anchor Design System
Form

Input3.0

Input is a component that allows users to enter text. It can be used to get user inputs in forms, search fields, and more.

Installation

npm i @tryg/ui-library

Usage

Individual only imports the desired component, and only needs to be included once within the application. Which is beneficial to keep the application size small.

Webcomponent
<script type="module">
  import { defineCustomElement } from '@tryg/ui-library/dist/components/anchor-input';
  defineCustomElement();
</script>
<anchor-input>Hello</anchor-input>

Global only needs to be defined once in the application, but it will import all components even the ones that are not in use..

Webcomponent
<script type="module">
  import { defineCustomElements } from '@tryg/ui-library/dist/loader';
  defineCustomElements();
</script>
<anchor-input>Hello</anchor-input>

Disabled

Webcomponent
<anchor-input disabled>Hello</anchor-input>

Read Only

Webcomponent
<anchor-input readonly>Hello</anchor-input>

Required

If you pass the required property to the input, the input will be required.

Webcomponent
<anchor-input required>Hello</anchor-input>

Help text

You can add a helpful text to the input by passing the help-text property.

Webcomponent
<anchor-input help-text="This is a help text">Hello</anchor-input>

With Error Message

You can combine the error-prop properties to show an invalid input.

Webcomponent
<anchor-input error-prop="This is a error message">Hello</anchor-input>

Label Placements

You can change the position of the label by setting the messagePlacement property to inside, or below.

Webcomponent
<anchor-input error-prop="This is a error message">Hello</anchor-input>

Note

If the label is not passed, the messagePlacement property will be outside by default.

Password Input

You can use the type property to change the input type to password.

Webcomponent
<anchor-input type="password">Password</anchor-input>

Controlled

You can use the value and onValueChange properties to control the input value.

Webcomponent
<script type="text/javascript">
  window.addEventListener("load", (event) => {
    document.querySelector('anchor-input').addEventListener('inputevent', function(data) {
      document.querySelector('#input-preview').innerHTML = data.detail.value
    })
  });
</script>
<anchor-input>Your name please</anchor-input>
<div id="input-preview"></div>

Accessibility

  • Built with a native <input> element.
  • Visual and ARIA labeling support.
    • aria-live attribute for the input field.
    • aria-describedby attribute for the input field.
    • aria-activedescendant attribute for the input field.
  • Change, clipboard, composition, selection, and input event support.
  • Required and invalid states exposed to assistive technology via ARIA.
    • aria-required attribute automatically set if required prop is set.
    • aria-invalid attribute automatically set if error-prop prop is set.
  • Support for description and error message help text linked to the input via ARIA.

API

anchor-input

Basic text input field

<anchor-input name="text input"></anchor-input>

Text input field with label

Input fields should always have a label to help the user navigate in a form. Any text you put between the tags will be shown as the label.

<anchor-input name="text input">This is the label text </anchor-input>

Disabled text input

<anchor-input disabled></anchor-input>

or

<anchor-input disabled="true"></anchor-input>

Read only text input

<anchor-input value="Text you cannot change" readonly></anchor-input>

or

<anchor-input value="Text you cannot change" readonly="true"></anchor-input>

Input with help text

Any text provided in the 'warning' attribute, will be shown as a message below the input field, and the input field itself will have a visual warning cue.

<anchor-input help-text="Text to help or guide the user"></anchor-input>

Input with warning state

Any text provided in the 'warning' attribute, will be shown as a message below the input field, and the input field itself will have a visual warning cue.

<anchor-input value="Possibly faulty input" warning="Are you sure this is correct?"></anchor-input>

Input with error state

There are two ways of triggering the error state in the input field and showing an error message:

As a prop

Any text provided in the 'error-prop' attribute, will be shown as a message below the input field, and the input field itself will have a visual error cue.

<anchor-input value="Faulty input" error-prop="Please correct the error"></anchor-input>

or

As a slot

For more control over the message shown, you can provide html and text as a slot. You need to wrap the text and any markup in an HTML tag with the attribute: slot="error-msg". Without naming the slot as "error-msg", the content will be treated as a label.

<anchor-input value="Faulty input" readonly="true">
  <span slot="error-msg">Please correct the error</span>
</anchor-input>

Events and how to capture them

The Input component emits a couple of events that you can capture in your application.

See the exact list of events further down this page.

Input value

The 'inputevent' is the way to get the value of the Input element.

Set up an event listener or similar, and listen for the event 'inputevent'.

Here's an example using a standard javascript event listener:

document.querySelector('#anchor-input').addEventListener('inputevent', event => {
  console.log('input value', event.detail.target.value);
});
ℹ️ Notice that the syntax for getting the input value is [your variable].detail.target.value.
You can find all relevant information in the "details" node. For instance [your variable].detail.data contains the latest input. This will often be a single character, but could also be a string.

Properties

PropertyAttributeDescriptionTypeDefault
ariaActivedescendantaria-activedescendantaria-activedescendant attribute for the input fieldstring''
ariaDescribedByaria-described-byaria-describedby attribute for the input fieldstring''
ariaLivearia-livearia-live attribute for the input fieldstringundefined
autocompleteautocompleteSet the autocomplete attribute for the input fieldstringundefined
disableddisabledDisable the input fieldbooleanfalse
errorFielderror-fieldShow an error state on input fieldbooleanfalse
errorProperror-propShow an error messagestring''
filledfilledShow a checkmark icon when the input field has been succesfully filledbooleanfalse
helpTexthelp-textShow a help textstring''
hideSpinButtonshide-spin-buttonsDisable the input fieldbooleanfalse
iconiconAdd an icon at the right side of the input field, by putting in the name of an Anchor icon"activity" | "airplay" | "alarm" | "alert-circle" | "alert-octagon" | "alert-triangle" | "align-center" | "align-justify" | "align-left" | "align-right" | "anchor" | "aperture" | "archive" | "arrow-down" | "arrow-down-circle" | "arrow-down-left" | "arrow-down-right" | "arrow-left" | "arrow-left-circle" | "arrow-right" | "arrow-right-circle" | "arrow-up" | "arrow-up-circle" | "arrow-up-left" | "arrow-up-right" | "at-sign" | "award" | "band-aid" | "bar-chart" | "bar-chart-2" | "basket" | "battery" | "battery-charging" | "bell" | "bell-off" | "bluetooth" | "bold" | "bonus-tryghedsgruppen" | "book" | "book-open" | "bookmark" | "box" | "briefcase" | "calculator" | "calendar" | "camera" | "camera-off" | "cast" | "check" | "check-circle" | "check-square" | "chevron-down" | "chevron-left" | "chevron-right" | "chevron-up" | "chevrons-down" | "chevrons-left" | "chevrons-right" | "chevrons-up" | "chrome" | "circle" | "clipboard" | "clock" | "cloud" | "cloud-drizzle" | "cloud-lightning" | "cloud-off" | "cloud-rain" | "cloud-snow" | "code" | "codepen" | "codesandbox" | "coffee" | "columns" | "command" | "company" | "compass" | "copy" | "corner-down-left" | "corner-down-right" | "corner-left-down" | "corner-left-up" | "corner-right-down" | "corner-right-up" | "corner-up-left" | "corner-up-right" | "cpu" | "credit-card" | "crop" | "crosshair" | "database" | "delete" | "disc" | "divide" | "divide-circle" | "divide-square" | "dollar-sign" | "download" | "download-cloud" | "dribbble" | "droplet" | "edit" | "edit-2" | "edit-3" | "external-link" | "eye" | "eye-off" | "facebook" | "fast-forward" | "feather" | "figma" | "file" | "file-minus" | "file-pdf" | "file-plus" | "file-text" | "film" | "filter" | "flag" | "folder" | "folder-minus" | "folder-plus" | "framer" | "frown" | "gift" | "git-branch" | "git-commit" | "git-merge" | "git-pull-request" | "github" | "gitlab" | "globe" | "grid" | "hard-drive" | "hash" | "headphones" | "heart" | "help-circle" | "hexagon" | "home" | "image" | "inbox" | "info" | "instagram" | "italic" | "key" | "layers" | "layout" | "life-buoy" | "link" | "link-2" | "linkedin" | "list" | "loader" | "lock" | "log-in" | "log-out" | "mail" | "map" | "map-pin" | "maximize" | "maximize-2" | "megaphone" | "meh" | "menu" | "message-circle" | "message-square" | "mic" | "mic-off" | "minimize" | "minimize-2" | "minus" | "minus-circle" | "minus-square" | "monitor" | "moon" | "more-horizontal" | "more-vertical" | "mouse-pointer" | "move" | "music" | "navigation" | "navigation-2" | "octagon" | "offer" | "package" | "paperclip" | "pause" | "pause-circle" | "pen-tool" | "percent" | "phone" | "phone-call" | "phone-forwarded" | "phone-incoming" | "phone-missed" | "phone-off" | "phone-outgoing" | "pie-chart" | "play" | "play-circle" | "plus" | "plus-circle" | "plus-square" | "pocket" | "power" | "printer" | "radio" | "refresh-ccw" | "refresh-cw" | "repeat" | "rewind" | "rotate-ccw" | "rotate-cw" | "rss" | "save" | "scissors" | "search" | "send" | "server" | "settings" | "share" | "share-2" | "shield" | "shield-off" | "shopping-bag" | "shopping-cart" | "shuffle" | "sidebar" | "skip-back" | "skip-forward" | "slack" | "slash" | "sliders" | "smartphone" | "smile" | "speaker" | "sprout" | "square" | "star" | "star-filled" | "stop-circle" | "storm" | "sun" | "sunrise" | "sunset" | "table" | "tablet" | "tag" | "target" | "terminal" | "thermometer" | "thumbs-down" | "thumbs-up" | "toggle-left" | "toggle-right" | "tool" | "trash" | "trash-2" | "trello" | "trending-down" | "trending-up" | "triangle" | "truck" | "trygfonden" | "tv" | "twitch" | "twitter" | "type" | "umbrella" | "underline" | "unlock" | "upload" | "upload-cloud" | "user" | "user-check" | "user-minus" | "user-plus" | "user-x" | "users" | "video" | "video-off" | "voicemail" | "volume" | "volume-1" | "volume-2" | "volume-x" | "watch" | "wifi" | "wifi-off" | "wind" | "x" | "x-circle" | "x-octagon" | "x-square" | "youtube" | "zap" | "zap-off" | "zoom-in" | "zoom-out"undefined
iconPlacementicon-placementShould the icon be inside or outside the input field"inside" | "outside"'inside'
inputIdinput-idThe id of the input fieldstring`anchor-input-${generateGuid()}`
inputmodeinputmodeThe inputmode attribute for the input fieldstringundefined
maxmaxSet a maximum number for the Input field.numberundefined
maxLengthmax-lengthSet a max width in characters for the Input field.numberundefined
messagePlacementmessage-placementThis prop decides if the messages should render between the label of an input and the input field, or below the input field."below" | "inside"'inside'
minminSet a minimum number for the Input field.numberundefined
namenameThe name of the input fieldstringundefined
patternpatternA pattern to validate the input field against Note that the pattern does not restrict the input field, it only validates the input when the form is submittedstringundefined
placeholderplaceholderPlaceholder text is shown until the user interacts with the input fieldstring''
readonlyreadonlySet the field to be read onlybooleanfalse
requiredrequiredMark the field as requiredbooleanfalse
selectOnFocusselect-on-focusThis prop decides if all content inside the input should be selected when it gets focusbooleanundefined
showLabelshow-labelThis prop should be used when you don't give the input a label, it will disable the margin of the input fieldbooleantrue
sizesizeSet a fixed width in characters for the Input field.numbernull
stepstepSet the step value for the Input field.numberundefined
tooltipTexttooltip-textAdd an info icon with a tooltip to the right of the labelstring''
typetypeSet the type for the input fieldstring'text'
validityMissingValueTextvalidity-missing-value-textText to be displayed if when value is missing an native form validation is usedstringundefined
valuevalueThe value in the input field. Use this to set a value and read itnumber | string''
warningwarningShow a warning messagestring''

Events

EventDescriptionType
blureventEvent emitted when the user blurs the input fieldCustomEvent<any>
cleareventEvent emitted when the user clears the input fieldCustomEvent<EventDetail>
focuseventEvent emitted when the user is focusing the input fieldCustomEvent<any>
inputChangeEvent emitted when the input is changed. Only triggers when focus leaves the inputCustomEvent<EventDetail>
inputeventEvent emitted when the user is inputting something into the input fieldCustomEvent<EventDetail>

Methods

focusInput(timeout?: number) => Promise<void>

Parameters

NameTypeDescription
timeoutnumber

Returns

Type: Promise<void>

CSS Custom Properties

NameDescription
--input-field-background-colorSets the background color for input field
--input-field-border-colorSets the border color of the input field
--input-field-border-color-disabledSets the border color for a disabled input field
--input-field-border-color-errorSets the border color for error states
--input-field-border-color-focusSets the border color on focus
--input-field-border-color-hoverSets the border color on hover
--input-field-border-color-readonlySets the border color for a readonly input field
--input-field-border-color-warningSets the border color for warning states
--input-field-border-radiusSets the border radius of the input field
--input-field-border-widthSets the width of the input field
--input-field-max-widthSets the max width of the input field
--input-field-outlineSets the outline of the input field
--input-field-widthSets the width of the input field

Dependencies

Used by

Depends on

Graph


Built with StencilJS

Anchor versionStatusNotes
3.0Ready for ImplementationThis component is in sync with Figma.

On this page