Text input

Text input is used to set a value that is a single line of text.

v0.35.2AlphaNot reviewed for accessibility
Forms frameworkThe TextField component is part of the Primer forms framework. If you're building a form, please consider using the framework instead of this standalone component.

Description

Text fields are single-line text inputs rendered as <input type="text"> in HTML.

Arguments

NameTypeDefaultDescription
size
Symbol

N/A

The size of the field. One of :large, :medium, or :small.

full_width
Boolean

N/A

When set to true, the field will take up all the horizontal space allowed by its container. Defaults to true.

name
String

N/A

Value for the HTML name attribute.

id
String

N/A

Value for the HTML id attribute.

class
String

N/A

CSS classes to include in the input's HTML class attribute. Exists for compatibility with Rails form builders.

classes
Array

N/A

CSS classes to include in the input's HTML class attribute. Combined with the :class argument. The list may contain strings, hashes, or nil values, and is automatically cleaned up by Primer's class_name helper (nils, falsy entries, and blank strings are ignored).

caption
String

N/A

A string describing the field and what sorts of input it expects. Displayed below the input.

label
String

N/A

Label text displayed above the input.

visually_hide_label
Boolean

N/A

When set to true, hides the label. Although the label will be hidden visually, it will still be visible to screen readers.

disabled
Boolean

N/A

When set to true, the input will not accept keyboard or mouse input.

hidden
Boolean

N/A

When set to true, visually hides the field.

invalid
Boolean

N/A

If set to true, the input will be rendered with a red border. Implied if validation_message is truthy. This option is set to true automatically if the model object associated with the form reports that the input is invalid via Rails validations. It is provided for cases where the form does not have an associated model. If the input is invalid as determined by Rails validations, setting invalid to false will have no effect.

validation_message
String

N/A

A string displayed between the caption and the input indicating the input's contents are invalid. This option is, by default, set to the first Rails validation message for the input (assuming the form is associated with a model object). Use validation_message to override the default or to provide a validation message in case there is no associated model object.

label_arguments
Hash

N/A

Attributes that will be passed to Rails' builder.label method. These can be HTML attributes or any of the other label options Rails supports. They will appear as HTML attributes on the <label> tag.

scope_name_to_model
Boolean

N/A

Default true. When set to false, prevents the model name from prefixing the field name. For example, if the field name is my_field, Rails will normally emit an HTML name attribute of model[my_field]. Setting scope_name_to_model to false will cause Rails to emit my_field instead.

scope_id_to_model
Boolean

N/A

Default true. When set to false, prevents the model name from prefixing the field ID. For example, if the field name is my_field, Rails will normally emit an HTML ID attribute of model_my_field. Setting scope_id_to_model to false will cause Rails to emit my_field instead.

required
Boolean

N/A

Default false. When set to true, causes an asterisk (*) to appear next to the field's label indicating it is a required field. Note that this option explicitly does not add a required HTML attribute. Doing so would enable native browser validations, which are inaccessible and inconsistent with the Primer design system.

aria
Hash

N/A

Key/value pairs that represent Aria attributes and their values. Eg. aria: { current: true } becomes aria-current="true".

data
Hash

N/A

Key/value pairs that represent data attributes and their values. Eg. data: { foo: "bar" } becomes data-foo="bar".

system_arguments
Hash

N/A

A hash of attributes passed to the underlying Rails builder methods. These options may mean something special depending on the type of input, otherwise they are emitted as HTML attributes. See the Rails documentation for more information. In addition, the usual Primer utility arguments are accepted in system arguments. For example, passing mt: 2 will add the mt-2 class to the input. See the Primer system arguments docs for details.

placeholder
String

N/A

Placeholder text.

inset
Boolean

N/A

If true, renders the input in a visually inset state.

monospace
Boolean

N/A

If true, uses a monospace font for the input field.

auto_check_src
String

N/A

When provided, makes a request to the given URL whenever the contents of the text field changes. If the server responds with a non-2xx status code, the response body is used as the validation message.

leading_visual
Hash

N/A

Renders a leading visual icon before the text field's cursor. The hash will be passed to Primer's Octicon component.

leading_spinner
Boolean

N/A

If true, a leading spinner will be included in the markup. The spinner can be shown via the showLeadingSpinner() JavaScript method, and hidden via hideLeadingSpinner(). If this argument is true, a leading visual must also be provided.

show_clear_button
Boolean

N/A

Whether or not to include a clear button inside the input that clears the input's contents when clicked.

clear_button_id
String

N/A

The HTML id attribute of the clear button.

Examples