Overview
The <form-field>
element is a wrapper element that provides a label element and common layouts for form elements like <input>
, <select>
, and <textarea>
elements.
The goal of the element is to allow hand-written <form>
s to be possible with fewer elements when doing the most common form designs.
Basic Examples
The <form-field>
element is used as a helper to whittle down the boilerplate necessary to have well-functioning and well-formatted form field.
The element may include a <label>
element or several <label>
elements, depending on whether the input is a collection of inputs or not.
If the content is only a single child, that child will be rendered in a <label>
element with a span to hold the text of the field's title. The content of that span will be populated by any value provided as the label
attribute on the <form-field>
element.
If the content is multiple inputs, the inputs will be rendered in a <div class="container">
element with a span to hold the text of the field's title, which should describe the whole group. The content of that span will be populated by any value provided as the label
attribute on the <form-field>
element.
Each of the provided inputs will be rendered in their own <label>
elements, each including titles for the inputs based on the input values.
In the examples below, note the difference in HTML written with the the <form-field>
element vs with non-custom HTML elements. For only 4 fields, the difference is still apparent. But as forms grow, the benefit of using the <form-field>
element grows too.
Helpers
The <form-field>
element provides "slots" for helper elements that a field may need. Since the <form-field>
element does not use the shadowDOM, it doesn't use acutal <slot>
functionality for this feature. Instead, parts of a template are replaced dynamically with whatever content sets its slot
attribute to either prefix
or postfix
.
These slots are useful for things like requirement indicators, pre-defined values, or complex styling.
Content Without a slot
attribute
All elements that are not an expected input, or assigned a slot
attribute will be appended to the end of the form-field's container.
This kind of content is useful for error messages, or additional notes about the specific field.
Note how the the "Before" element is appended after the <input>
element.
<form-field label="Username">
<div>Before</div>
<input type="text" name="username" placeholder="Same as it ever was" />
<div>After</div>
</form-field>
prefix
slot
Setting a child element's slot
attribute to a value of prefix
will place that element directly after the input.
In the case of input groups, the prefix element will be duplicated before each input.
postfix
slot
Giving a child element the attribute
value of postfix
will place that element after the input.
In the case of input groups, the postfix element will be duplicated after each input.
Practical Example
This example implements a simple search control using the slots and additional content.
<form-field label="Search" class="search" input-selector="input[type='search']">
<input type="search" name="query" />
<select slot="prefix" class="type">
<option value="name">Name</option>
<option value="tag">Tag</option>
<option value="id">Product ID</option>
</select>
<button slot="postfix">🔍</button>
<div class="error">This is where error text would be displayed.</div>
</form-field>