Input
<ahi-input> | AhiInput
An input field is a text field that users can type into. It has a range of options and supports several text formats including numbers.
Use to allow users to provide text input when the expected input is short. For longer input, use the textarea component.
<ahi-input label="Name"></ahi-input>
This component works with standard <form> elements. Please refer to the section on
form controls to learn
more about form submission and client-side validation.
Examples
Input types
Number
Use when input text should be a number.
<ahi-input label="Quantity" type="number"></ahi-input>
Use when the text input should be an email address.
<ahi-input label="Email" type="email" autocomplete="email" value="test@example.com"></ahi-input>
Date picker
Only use this kind of date input when users need to find a date in relation to another, or if they need to know the day, week, or month that the date relates to.
<ahi-input label="Appointment date" type="date"></ahi-input>
Placeholder
Use to provide a short, non-essential hint about the expected input. Placeholder text is low-contrast, so don’t rely on it for important information.
<ahi-input label="First name" placeholder="e.g. John"></ahi-input>
Help text
Use to show short instructional content below the text field. Help text helps users understand how to fix errors that result from incorrect formatting (such as dates or passwords with specific character requirements).
<ahi-input
label="Account email"
type="email"
autocomplete="email"
value="test@example.com"
help-text="We'll use this address if we need to contact you about your account."
></ahi-input>
Clearable
Add the clearable attribute to add a clear button when the input has content.
<ahi-input label="Clearable field" value="clearable" clearable></ahi-input>
Password toggle
Add the password-toggle attribute to add a toggle button that will show the password when
activated.
<ahi-input label="Password" type="password" value="secret" password-toggle></ahi-input>
Filled
Add the filled attribute to draw a filled input.
<ahi-input placeholder="Search" filled></ahi-input>
Prefix or suffix
Use as a special form of help text that works best inline. Use a prefix for things like currency symbols (“$”, “¥”, “£”). Use suffix for things like units of measure (“in”, “cm”, “lbs“, “oz”).
<ahi-input label="Price" type="number">
<span slot="prefix">$</span>
</ahi-input>
<br />
<ahi-input label="Weight" type="number">
<span slot="suffix">lbs</span>
</ahi-input>
<br />
<ahi-input placeholder="Search" filled>
<ahi-icon slot="prefix" name="search" size="minor"></ahi-icon>
</ahi-input>
Disabled
Use to show that an input is not available for interaction. Most often used in forms when information is required only in a particular state.
<ahi-input label="Name" disabled></ahi-input>
Character Count
Use to show the character count of an input. If the input also has a maxlength attribute, the
number of characters left will be displayed.
<ahi-input label="Name" character-count maxlength="25"></ahi-input>
<ahi-input label="Bio" character-count></ahi-input>
Customizing label position
Use CSS parts to customize the way form controls are drawn. This example uses CSS grid to position the label to the left of the control, but the possible orientations are nearly endless. The same technique works for inputs, textareas, radio groups, and similar form controls.
<ahi-input class="input-label-on-left" label="Name"></ahi-input><br />
<ahi-input class="input-label-on-left" label="Email" type="email"></ahi-input>
<style>
.input-label-on-left {
--label-width: 3.75rem;
--gap-width: 1rem;
}
.input-label-on-left + .input-label-on-left {
margin-top: var(--ahi-space-4);
}
.input-label-on-left::part(form-control) {
display: grid;
grid: auto / var(--label-width) 1fr;
gap: var(--ahi-space-05) var(--gap-width);
align-items: center;
}
.input-label-on-left::part(form-control-label) {
text-align: right;
}
.input-label-on-left::part(form-control-help-text) {
grid-column-start: 2;
}
</style>
Importing
To import this component using a script tag:
<script type="module" src="/path/to/azalea-kit-ui/dist/components/input/input.js"></script>
To import this component using a bundler:
import '@azaleahealth/azalea-kit-ui/dist/components/input/input.js';
Slots
| Name | Description |
|---|---|
label
|
The input’s label. Alternatively, you can use the label attribute. |
prefix
|
Used to prepend a presentational icon or similar element to the input. |
suffix
|
Used to append a presentational icon or similar element to the input. |
clear-icon
|
An icon to use in lieu of the default clear icon. |
show-password-icon
|
An icon to use in lieu of the default show password icon. |
hide-password-icon
|
An icon to use in lieu of the default hide password icon. |
help-text
|
Text that describes how to use the input. Alternatively, you can use the
help-text attribute.
|
error
|
An error message shown once the control is invalid. Plain text can be set with the
error property instead; use this slot when the message needs markup.
|
Learn more about using slots.
Properties
| Name | Description | Type | Default | |
|---|---|---|---|---|
error
|
An error message to show on the control. Setting it makes the control genuinely invalid: it enters
the user-invalid state, matches :invalid, and blocks native form
submission — which is what a consumer binding a server-side rejection expects. Clearing it clears
that validity. Use the error slot instead when the message needs markup rather than
plain text.
|
string
|
''
|
|
autocapitalize
|
Controls whether and how text input is automatically capitalized as it is entered by the user. |
'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters'
|
- | |
autocorrect
|
Indicates whether the browser’s autocorrect feature is on or off. As an attribute, use
"on" or "off"; as a property it is a boolean, matching the platform’s
autocorrect IDL attribute.
|
boolean
|
- | |
autocomplete
|
Specifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values. |
string
|
- | |
autofocus
|
Indicates that the input should receive focus on page load. |
boolean
|
- | |
characterCount
character-count
|
Enables a character count display under the input. |
boolean
|
false
|
|
clearable
|
Adds a clear button when the input is not empty. |
boolean
|
false
|
|
defaultValue
|
The default value of the form control. Primarily used for resetting the form control. |
string
|
''
|
|
disabled
|
Disabled the input. |
boolean
|
false
|
|
enterkeyhint
|
Used to customize the label or icon of the Enter key on virtual keyboards. |
'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'
|
- | |
filled
|
Draws a filled input. |
boolean
|
false
|
|
helpText
help-text
|
The input’s help text. If you need to display HTML, you can use the
help-text slot instead.
|
string
|
''
|
|
inputmode
|
Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices. |
'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url'
|
- | |
label
|
The input’s label. If you need to display HTML, use the label slot instead. |
string
|
''
|
|
max
|
The input’s maximum value. Only applies to date and number input types. |
number | string
|
- | |
maxlength
|
The maximum length of input that will be considered valid. |
number
|
- | |
min
|
The input’s minimum value. Only applies to date and number input types. |
number | string
|
- | |
minlength
|
The minimum length of input that will be considered valid. |
number
|
- | |
name
|
The name of the input, submitted as a name/value pair with form data. |
string
|
''
|
|
passwordToggle
password-toggle
|
Adds a button to toggle the password’s visibility. Only applies to password types. |
boolean
|
false
|
|
passwordVisible
password-visible
|
Determines whether or not the password is currently visible. Only applies to password input types. |
boolean
|
false
|
|
pattern
|
A regular expression pattern to validate input against. |
string
|
- | |
placeholder
|
Placeholder text to show as a hint when the input is empty. |
string
|
''
|
|
readonly
|
Makes the input read only. |
boolean
|
false
|
|
required
|
Makes the input a required field. |
boolean
|
false
|
|
spellcheck
|
Enables spell checking on the input. |
boolean
|
true
|
|
step
|
Specifies 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.
|
number | 'any'
|
- | |
type
|
The type of input. Works the same as a native <input> element, but only a subset
of types are supported. Defaults to text.
|
'date' | 'datetime-local' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'time'
| 'url'
|
'text'
|
|
value
|
The current value of the input, submitted as a name/value pair with form data. |
string
|
''
|
|
validity
|
Gets the validity state object | - | - | |
validationMessage
|
Gets the validation message | - | - | |
valueAsDate
|
Gets 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.
|
- | - | |
valueAsNumber
|
Gets or sets the current value as a number. Returns NaN if the value can’t be converted
|
- | - | |
formId
form
|
By 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.
|
string
|
''
|
|
form
|
The form the control is associated with, following the native form-control contract: reading returns
the associated <form> element (or null if there is none), assigning
takes a form id — assign an empty string to restore association with the nearest containing form.
|
HTMLFormElement | null
|
- | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about properties and attributes.
Events
| Name | Description | Event Detail |
|---|---|---|
ahi-blur |
Emitted when the control loses focus. | - |
ahi-change |
Emitted when an alteration to the control’s value is committed by the user. | - |
ahi-clear |
Emitted when the clear button is activated. | - |
ahi-focus |
Emitted when the control gains focus. | - |
ahi-input |
Emitted when the control receives input. | - |
ahi-invalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
Learn more about events.
Methods
| Name | Description | Arguments |
|---|---|---|
focus() |
Sets focus on the input. |
options: FocusOptions
|
blur() |
Removes focus from the input. | - |
select() |
Selects all the text in the input. | - |
setSelectionRange() |
Sets the start and end positions of the text selection (0-based). |
selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'
|
setRangeText() |
Replaces a range of text with a new string. |
replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' |
'preserve'
|
showPicker() |
Displays the browser picker for an input element (only works if the browser supports it for the input type). | - |
stepUp() |
Increments the value of a numeric input type by the value of the step attribute. | - |
stepDown() |
Decrements the value of a numeric input type by the value of the step attribute. | - |
checkValidity() |
Checks for validity but does not show a validation message. Returns true when valid and
false when invalid.
|
- |
reportValidity() |
Checks for validity and shows the browser’s validation message if the control is invalid. | - |
setCustomValidity() |
Sets a custom validation message. If message is not empty, the field will be considered
invalid.
|
message: string
|
resetValidity() |
Hides validity again, returning the control to its pristine state: it stops matching
:user-invalid and drops its error styling until the user next interacts with it. The
value and the validity are both untouched — an empty required control is still invalid and still
blocks submission. This only changes whether that invalidity is shown. Interaction state
otherwise has exactly one automatic exit, form.reset(). Call this when the application
replaces a value itself and the old error no longer describes anything the user did: loading a
draft, switching which record is being edited, or clearing a filter row.
|
- |
Learn more about methods.
CSS Parts
| Name | Description |
|---|---|
form-control |
The form control that wraps the label, input, and help text. |
form-control-label |
The label’s wrapper. |
form-control-input |
The input’s wrapper. |
form-control-help-text |
The help text’s wrapper. |
form-control-error |
The error’s wrapper. |
base |
Deprecated. Use the input-wrapper part instead. |
input-wrapper |
The component’s outer wrapper. |
input |
The internal <input> control. |
prefix |
The container that wraps the prefix. |
clear-button |
The clear button. |
password-toggle-button |
The password toggle button. |
suffix |
The container that wraps the suffix. |
character-count |
The character count. This control inherits setCustomValidity(message),
checkValidity() and reportValidity() from the form-control base class.
Because they are inherited rather than declared here, they do not appear in this component’s
generated type definitions – which is why they are easy to conclude do not exist. They do, and they
work.
|
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<ahi-icon>