Component Input

This component creates a text box for users to type in information. Use it in situations where users need to input details that can vary, such as passwords or names.

CodeUsage

Example

This is a required field.
This field is required.
Your username is your email address.
This example has a tooltip.

Props

AttrTypeDefaultDocs

autocomplete

string

'off'

This property indicates that the value of the control can be automatically completed by the browser.

disabled

boolean

false

This boolean can be used to disable the text input.

error

string

null

This property can be used to set an error. If a non-empty string is given, it will be used as the error message.

grouped

boolean

false

This boolean property is used when passing a children component to a text input as a grouped element.

help

string

''

This property can be used to set a help text.

hide-label

boolean

false

This property will visually hide the label. A label is still required to be provided for screenreader support.

input-aria-label

string

null

This property can be used to set the ARIA label associated with this input (for screen readers).

input-id

string

generateId('som-input')

This property can be used to specifically set an ID to the input field. If an ID is not provided, it will be dynamically generated.

label

string

''

This property can be used to set a label.

label-class

string

''

This property is a list of CSS classes that can be used to style the label.

max

number | string

This property sets the maximum value of the input.

maxlength

number

If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters that the user can enter.

min

number | string

This property sets the minimum value, which must not be greater than its maximum (max attribute) value.

name

string

''

This property can be used to set the form field name (used when submitting the form).

placeholder

string

''

This property can be used to set a placeholder text.

readonly

boolean

false

This boolean can be used to set the text input as readonly.

required

boolean

false

This boolean can be used to set the input as required.

required-ind

"none" | "optional" | "required"

This property is used to override the required indicator. By default, forms elements will show an optional indicator but some forms are better served showing the required indicator instead.

step

number | string

This property sets the interval between numbers in the input field.

tooltip

string

null

This property is used to set the tooltip text.

type

"email" | "number" | "password" | "search" | "tel" | "text" | "url"

'text'

This property can be used to set the type.

value

string

''

This property can be used to set the text input value.

Methods

NameReturnsDocs

setFocus

Promise will resolve after focus.

This public method sets focus to the text input element.

Events

EventDetailDocs

somBlur

any

This event is emitted when when the input field loses focus.

somChange

any

This event is emitted when the input value changes.

somFocus

any

This event is emitted when when the input field gets focus.

somInput

any

This event is emitted when when a keyboard input occurs.

Slots

NameDocs

after-input

This slot is used to place an icon or suffix at the end of the input.

before-input

This slot is used to place an icon or prefix at the start of the input.

label

Label for the The input. Alternatively, you can use the label attribute.

Anatomy

A layout of an input’s anatomy, as described below.
  1. Label: Describes the expected input.
  2. Text input: Where the user inputs data.
  3. Helper text: Provides additional context if needed.
  4. Input container: Displays the input field.

Labels

  • Each field needs a label, which should be clear, concise, and obvious to users what the field is asking for.
  • As with placeholder text, use sentence-case capitalization and consistent grammar and spelling in each label.
  • Ensure that labels are visible.

Placeholder text

Placeholder text can give users an idea of what to put into an input. It’s optional, so don’t use it as a space to put important information or use it as a replacement for a label.

  • Once the user begins entering their data into the input, placeholder text is removed.
  • Some screen readers don’t read placeholder text aloud, which is why it shouldn’t be used for important information.
  • Placeholder text should be short and informative. Use sentence-style capitalization and an active voice.
  • Don’t use placeholder text as help text because it disappears on focus, while helper text remains visible.
A properly formatted form field with helper text, a label, and helper text, as described in the caption.
An improperly formatted form field with a placeholder used instead of a label and helper text, as advised against in the caption below.
The label and helper text are placed outside the form field and are always visible to the user.
Placeholder text should not be used to replace a label or as helper text. Without labels, users cannot check their work before submitting a form.

Helper and error text

  • Use helper text to clarify requirements for filling out an input, such as formatting rules.
  • Use error text to explain mistakes made by users. Error text should be specific enough to correct errors.
  • Use sentence case, write full sentences, and use punctuation.
  • Helper text will appear underneath a field, except when an error message replaces it.
An example of helper text and error text, as described in the caption.

The helper text below the email field on the left identifies the expected input. The error text below the email field on the right explains what error a user made.

Required and optional fields

Not every input needs to be filled out. There are ways to establish when an input is optional.

  • When possible, don’t ask for unnecessary information. This prevents users from getting overwhelmed.
  • If most of the form’s fields are required, mark only the optional fields.
  • If most of the form’s fields are optional, mark only the required fields.

By default, fields not marked as required will show “(optional)” indicators. It's possible to manually override this behavior to display indicators for required fields instead. Visit the code section to learn more.

An example of fields in a form properly marked as optional, as described in the caption.
An example of fields in a form improperly marked as required, as described in the caption.
When the majority of fields are required, identify which fields are optional.
If most fields are required, don’t mark the required fields. Mark the optional fields instead.
Email fields with proper required and optional formatting, as described in the caption.

Optional and required fields need their own identifiers.

Tooltips

Add a tooltip by setting the tooltip attribute to the desired text. Use tooltips sparingly, and keep text brief and supplementary. Do not include interactive elements. The tooltip appears on hover or keyboard focus.

Your username is your email address.

Behavior

States

The different interaction states, as described in the caption.

Inputs change states as users interact with them.

  • Default: The standard state of an input.
  • Hover: When a user’s cursor moves over an input without clicking it.
  • Focus: When a user highlights an input with a keyboard or the Tab key.
  • Disabled: An unavailable action. Ensure that users aren’t confused as to why a text field is disabled.
  • Error: When a field fails validation, an error message can display instructions on how to fix it.
    • Error messages are displayed below the input area, replacing helper text until fixed.
    • Real-time validation helps streamline the process and keep data clean when the user fills out forms. Otherwise, validate the text input data when the user submits the associated form.
  • Read-only: Allows for content to be shown in a text area so that users can copy it, but not edit it.

Text overflow

If a user’s entry is too long for the input, text will flow off the right edge and be hidden by the boundary of the input. Add a character limit to prevent this.

An example of text overflow in an input.

Modifiers

Input type

Input types allow developers to specify the use or formatting requirements for an input. An input’s default type is text. To learn more about using input types, visit MDN Web Docs.

An anatomical layout of different input types, as described in the caption.

Input types include:

  1. Text: A single-line text field.
  2. Number: Accepts only numbers such as currency, measurements, and dates.
  3. URL: Accepts only hyperlink URLs.
  4. Tel: Accepts only properly formatted phone numbers
  5. Email-only: Takes properly formatted email addresses.
  6. Password: A field for a password or other sensitive information to be entered. Because users are entering private data, the data entered into these inputs are hidden.
  7. Search: An input used as a search field.

Field elements

Text prefixes show information that goes before text, such as currency symbols or buttons. Improve a form’s cohesiveness by using formatting requirements.

An input with a text prefix, as described in the caption.

The text prefix here helps the user identify the expected input.

Leading icons give users context about what input they’re expected to provide. For example, if it’s an email input, the leading icon might be an image of a letter.

A search input with a leading icon, as described in the caption.

A search icon at the far left of the input lets users know that this is a search input.

Inputs can also have buttons that perform actions, such as clearing a field, opening a popover, or toggling password visibility.

An example of a trailing button used to toggle clearing an input and password visibility as described in the caption.

A demonstration of password visibility toggled by a clear button and a password visibility toggle.

Spinners show that background actions, such as loading or data fetching, are in progress.

An example of a spinner as described in the caption.

A spinner indicates that a background action is in progress.

Interactions

Mouse

Inputs can be activated when users click on their containers.

  • Hovering over inputs changes the color and thickness of the input border.
  • Clicking on the input focuses on it so users can begin typing.
  • To interact with additional actions connected to the input, such as a password visibility toggle, users must click on the additional component.
  • Focus is lost when users click away from the input.

Keyboard focus

  • Tab: Navigates to and focuses on the input. Once the input is focused on, users can type into it. Users can also tab over to trailing buttons.
  • Enter: Triggers an action when focused on a trailing button.
  • Arrow keys: Move the cursor around inside the input.

Layout

The height of an input is predetermined and should not be changed.

Default input heights as described in the caption.

Input height is fixed and should not be altered.

The width of a field needs to match the content’s length.

  • Don’t make an input too short to fit the expected entry.
  • Give each field its own line unless they are short and related, such as the city, state, and ZIP code in an address.
  • Inputs don’t have a set smallest or largest width, but don’t make fields that are smaller than the content that they’re collecting.
  • The minimum recommended width is 120px on desktop and full width in mobile. This ensures that smaller inputs can be read and interacted with easily on touch screens.
  • The maximum recommended width for inputs is 768px.
Inputs with proper widths, as described in the caption.
Input widths sized smaller than their content, as described in the caption.
Make the minimum width the expected size of the content. Ideally, make all inputs the same width.
Don’t make an input’s width smaller than its content size.
Text input widths that are properly sized and aligned to their content and grid columns, as described in the caption.
Text input widths that are improperly sized and aligned to their content and grid columns, as described in the caption.
Make input widths proportional to the content and aligned to grid columns.
Inputs shouldn’t be wide solely to take up space. They also shouldn’t stretch into the gutter.
Get in touch

Do you have an idea, or want to help us improve this page? Reach out to us via email.

DTMB-DG-Support@michigan.gov

Need help? Visit our Support page to learn how to get assistance, troubleshoot issues, and connect with the team.