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-libraryUsage
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.
<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..
<script type="module">
import { defineCustomElements } from '@tryg/ui-library/dist/loader';
defineCustomElements();
</script>
<anchor-input>Hello</anchor-input>Disabled
<anchor-input disabled>Hello</anchor-input>Read Only
<anchor-input readonly>Hello</anchor-input>Required
If you pass the required property to the input, the input will be required.
<anchor-input required>Hello</anchor-input>Help text
You can add a helpful text to the input by passing the help-text property.
<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.
<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.
<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.
<anchor-input type="password">Password</anchor-input>Controlled
You can use the value and onValueChange properties to control the input value.
<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-liveattribute for the input field.aria-describedbyattribute for the input field.aria-activedescendantattribute for the input field.
- Change, clipboard, composition, selection, and input event support.
- Required and invalid states exposed to assistive technology via ARIA.
aria-requiredattribute automatically set ifrequiredprop is set.aria-invalidattribute automatically set iferror-propprop 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
| Property | Attribute | Description | Type | Default |
|---|---|---|---|---|
ariaActivedescendant | aria-activedescendant | aria-activedescendant attribute for the input field | string | '' |
ariaDescribedBy | aria-described-by | aria-describedby attribute for the input field | string | '' |
ariaLive | aria-live | aria-live attribute for the input field | string | undefined |
autocomplete | autocomplete | Set the autocomplete attribute for the input field | string | undefined |
disabled | disabled | Disable the input field | boolean | false |
errorField | error-field | Show an error state on input field | boolean | false |
errorProp | error-prop | Show an error message | string | '' |
filled | filled | Show a checkmark icon when the input field has been succesfully filled | boolean | false |
helpText | help-text | Show a help text | string | '' |
hideSpinButtons | hide-spin-buttons | Disable the input field | boolean | false |
icon | icon | Add 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 |
iconPlacement | icon-placement | Should the icon be inside or outside the input field | "inside" | "outside" | 'inside' |
inputId | input-id | The id of the input field | string | `anchor-input-${generateGuid()}` |
inputmode | inputmode | The inputmode attribute for the input field | string | undefined |
max | max | Set a maximum number for the Input field. | number | undefined |
maxLength | max-length | Set a max width in characters for the Input field. | number | undefined |
messagePlacement | message-placement | This 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' |
min | min | Set a minimum number for the Input field. | number | undefined |
name | name | The name of the input field | string | undefined |
pattern | pattern | A 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 submitted | string | undefined |
placeholder | placeholder | Placeholder text is shown until the user interacts with the input field | string | '' |
readonly | readonly | Set the field to be read only | boolean | false |
required | required | Mark the field as required | boolean | false |
selectOnFocus | select-on-focus | This prop decides if all content inside the input should be selected when it gets focus | boolean | undefined |
showLabel | show-label | This prop should be used when you don't give the input a label, it will disable the margin of the input field | boolean | true |
size | size | Set a fixed width in characters for the Input field. | number | null |
step | step | Set the step value for the Input field. | number | undefined |
tooltipText | tooltip-text | Add an info icon with a tooltip to the right of the label | string | '' |
type | type | Set the type for the input field | string | 'text' |
validityMissingValueText | validity-missing-value-text | Text to be displayed if when value is missing an native form validation is used | string | undefined |
value | value | The value in the input field. Use this to set a value and read it | number | string | '' |
warning | warning | Show a warning message | string | '' |
Events
| Event | Description | Type |
|---|---|---|
blurevent | Event emitted when the user blurs the input field | CustomEvent<any> |
clearevent | Event emitted when the user clears the input field | CustomEvent<EventDetail> |
focusevent | Event emitted when the user is focusing the input field | CustomEvent<any> |
inputChange | Event emitted when the input is changed. Only triggers when focus leaves the input | CustomEvent<EventDetail> |
inputevent | Event emitted when the user is inputting something into the input field | CustomEvent<EventDetail> |
Methods
focusInput(timeout?: number) => Promise<void>
Parameters
| Name | Type | Description |
|---|---|---|
timeout | number |
Returns
Type: Promise<void>
CSS Custom Properties
| Name | Description |
|---|---|
--input-field-background-color | Sets the background color for input field |
--input-field-border-color | Sets the border color of the input field |
--input-field-border-color-disabled | Sets the border color for a disabled input field |
--input-field-border-color-error | Sets the border color for error states |
--input-field-border-color-focus | Sets the border color on focus |
--input-field-border-color-hover | Sets the border color on hover |
--input-field-border-color-readonly | Sets the border color for a readonly input field |
--input-field-border-color-warning | Sets the border color for warning states |
--input-field-border-radius | Sets the border radius of the input field |
--input-field-border-width | Sets the width of the input field |
--input-field-max-width | Sets the max width of the input field |
--input-field-outline | Sets the outline of the input field |
--input-field-width | Sets the width of the input field |
Dependencies
Used by
Depends on
Graph
Built with StencilJS
| Anchor version | Status | Notes |
|---|---|---|
| 3.0 | Ready for Implementation | This component is in sync with Figma. |
