InputField
A configurable InputField component used to gather user input through a text field.
Import
- React
- Angular
- Vue.js
// with @dhl-official/react-library:
import { DhlInputField } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlInputField } from "@dhl-official/ui-libraries/react-library"
If the DUIL has been installed, you can use the web component directly:
<dhl-input-field></dhl-input-field>
// with @dhl-official/vue-library:
import { DhlInputField } from "@dhl-official/vue-library"
// with @dhl-official/ui-libraries/vue-library:
import { DhlInputField } from "@dhl-official/ui-libraries/vue-library"
Code
- React
- Angular
- Vue.js
<DhlInputField
variant={{
label: 'Label',
placeholder: 'Placeholder',
type: Variants.static
}}
></DhlInputField>
<dhl-input-field
[variant]="{
label: 'Label',
placeholder: 'Placeholder',
type: Variants.static
}"
></dhl-input-field>
<dhl-input-field
:variant.prop="{
label: 'Label',
placeholder: 'Placeholder',
type: Variants.static
}"
></dhl-input-field>
Interactive Demo
Readme
Usage
Dhl-input-field
Snippets of code in HTML and JavaScript to show some of the use cases for the component.
The code is not meant to be executed, but to be used as a reference for the usage of the component.
Angular, React and Vue usages are not included in this documentation, but can be easily derived from the html and javascript code.
default usage
<form novalidate>
<dhl-input-field name="dhl-input-field-custom-element"></dhl-input-field>
<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
</form>
<script type="module">
const form = document.querySelector("form");
const input = document.querySelector("dhl-input-field");
input.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};
form.addEventListener("submit", async (e) => {
e.preventDefault();
console.log(Object.fromEntries(new FormData(form)));
return await form.checkValidity();
});
</script>
usage with validation (required) and (browser) default validation message
<form novalidate>
<dhl-input-field
name="dhl-input-field-custom-element"
required
>
</dhl-input-field>
<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
</form>
<script type="module">
const form = document.querySelector("form");
const input = document.querySelector("dhl-input-field");
input.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};
form.addEventListener("submit", async (e) => {
e.preventDefault();
console.log(Object.fromEntries(new FormData(form)));
return await form.checkValidity();
});
</script>
usage with validation (required) and custom validation message
<form novalidate>
<dhl-input-field
name="dhl-input-field-custom-element"
required
>
</dhl-input-field>
<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
</form>
<script type="module">
const form = document.querySelector("form");
const input = document.querySelector("dhl-input-field");
input.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};
const validationMessageInvalid = "This field is invalid";
const validationMessageValid = "This field valid";
form.addEventListener("submit", async (e) => {
const isValid = await e.target.checkValidity();
if (isValid) {
input.validation = {
type: "valid",
message: validationMessageValid,
};
} else {
input.validation = {
type: "invalid",
message: validationMessageInvalid,
};
}
return isValid;
});
</script>
usage with custom events
<form novalidate>
<dhl-input-field
name="dhl-input-field-custom-element"
required
>
</dhl-input-field>
<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
<dhl-text></dhl-text>
</form>
<script type="module">
const form = document.querySelector("form");
const input = document.querySelector("dhl-input-field");
const text = document.querySelector("dhl-text");
input.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};
const validationMessageInvalid = "This field is invalid";
const validationMessageValid = "This field valid";
input.addEventListener("dhlBlur", async (e) => {
const isValid = await e.target.checkValidity();
if (isValid) {
input.validation = {
type: "valid",
message: validationMessageValid,
};
} else {
input.validation = {
type: "invalid",
message: validationMessageInvalid,
};
}
});
input.addEventListener("dhlChange", async (e) => {
text.innerHTML = e.target.value;
});
form.addEventListener("submit", async (e) => {
e.preventDefault();
console.log(Object.fromEntries(new FormData(form)));
return await form.checkValidity();
});
</script>
Properties
Property | Attribute | Description | Type | Default |
---|---|---|---|---|
autoComplete | auto-complete | An optional prop used to set the autocomplete value. It takes any valid value that can be used for the autocomplete attribute of an HTMLInputElement. | string | undefined |
blurEvent | -- | [DEPRECATED] Use dhlBlur event instead.An optional onBlur callback handler. | (event: FocusEvent) => void | undefined |
dataAriaActivedescendant | data-aria-activedescendant | An optional prop used to identify the currently active element within a composite widget context. | string | undefined |
dataAriaAutoComplete | data-aria-auto-complete | An optional prop used to indicate whether inputting text could trigger display of one or more predictions of the user's intended value. | string | undefined |
dataAriaDescribedby | data-aria-describedby | An optional prop defining the list of reference IDs (separated by spaces), recommended when you want to an error message on your field. | string | undefined |
dataAriaExpanded | data-aria-expanded | An optional prop used for assistive technology support - used to mark expandable and collapsible regions. | string | undefined |
dataAriaHasPopup | data-aria-has-popup | An optional prop that lets the screen reader know that this component has a popup. | string | undefined |
dataAriaLabel | data-aria-label | An optional prop defining the text read by the screen reader to represent the component; use this if you need different text to be read from label. | string | undefined |
dataAriaOwns | data-aria-owns | An optional attribute to identify an element (or elements) to define a visual, functional, or contextual relationship between a parent and it's child when it otherwise cannot be in the DOM hierarchy. | string | undefined |
dataClassName | data-class-name | An optional class name prop for the component. | string | undefined |
dataId | data-id | An optional prop. Gives a valid HTML ID attribute value for the component. | string | dhl-input-field-${getRandomString()} |
dataRole | data-role | An optional prop defining the role attribute of the component. | string | undefined |
dataTestid | data-testid | An optional prop. The test id attached to the component as a data-testid attribute. | string | undefined |
dataTracking | data-tracking | An optional data tracking prop for the component. | string | undefined |
disableValidation | disable-validation | An optional prop to disable the validation's visual feedback. Validation is still applied and validity state is still set to the form element. | boolean | false |
focusEvent | -- | [DEPRECATED] Use dhlFocus event instead.An optional onFocus callback handler. | (event: FocusEvent) => void | undefined |
formNoValidate | form-no-validate | An optional prop used to set native formnovalidate attribute. This bypasses form control validation for form submission for the types image and submit. | boolean | undefined |
inputEvent | -- | [DEPRECATED] Use dhlInput event instead.An optional onInput callback handler. | (event: InputEvent) => void | undefined |
isDisabled | disabled | An optional flag to define if the component is disabled. | boolean | false |
keyDownEvent | -- | [DEPRECATED] Use dhlKeyDown event instead.An optional onKeyDown callback handler. | (event: KeyboardEvent) => void | undefined |
list | list | An optional attribute that specifies a datalist for the input field. | string | undefined |
max | max | An optional prop describing the maximum value that can be entered in the input field. Only works with type="number", type="range", type="date". | string | undefined |
min | min | An optional prop describing the minimum value that can be entered in the input field. Only works with type="number", type="range", type="date". | string | undefined |
name | name | An optional value to be set to the element HTML name attribute. It takes any valid value that can be used for the name attribute of an HTMLInputElement. | string | undefined |
pattern | pattern | An optional prop describing the regular expression pattern that the input value must match. | string | undefined |
readonly | readonly | An optional prop to flag the component as readonly within a form context. | boolean | false |
required | required | An optional prop to flag the component as required within a form context. | boolean | undefined |
searchIcon | search-icon | An optional prop flag to define if the search icon is displayed. By setting the type prop to search this prop is automatically set to true . | boolean | undefined |
src | src | An optional prop to give the source of the input element, only used for type="image". | string | undefined |
step | step | An optional prop describing the step value for the input field. It works with type="number", type="range", type="date", type="datetime", type="datetime-local", type="month", type="time", type="week". | string | undefined |
type | type | An optional prop describing the type of the component. It takes any valid value that can be used for the type attribute of an HTMLInputElement. | string | "text" |
validation | -- | An optional object to set-up a custom components validation state. Required Fields: type | { type: Variants.valid \| Variants.invalid \| Variants.note; message?: string; } | undefined |
value | value | An optional prop defining the value of the component which is taken when a form is submitted. | string | "" |
variant (required) | -- | A REQUIRED object to set-up a custom components variant state. It can be used to set a custom label, a custom placeholder text, enable or disable label animation (via the mandatory type field). | { label?: string; placeholder?: string; type: Variants.animated \| Variants.static; } | undefined |
Events
Event | Description | Type |
---|---|---|
dhlBlur | Event emitted when the input field loses focus. | CustomEvent<{ value: string; }> |
dhlChange | Event emitted when the input field changes value. | CustomEvent<{ value: string; }> |
dhlFocus | Event emitted when the input field receives focus. | CustomEvent<{ value: string; }> |
dhlInput | Event emitted when the input value changes. | CustomEvent<{ value: string; }> |
dhlKeyDown | Event emitted when a key is pressed down on the input field. | CustomEvent<KeyboardEvent> |
dhlKeyUp | Event emitted when a key is released on the input field. | CustomEvent<KeyboardEvent> |
Methods
checkValidity() => Promise<boolean>
Checks the validity of the input field.
Returns
Type: Promise<boolean>
A promise that resolves to true if the input field is valid, otherwise false.
getInputElement() => Promise<HTMLInputElement>
Retrieves the input element asynchronously.
Returns
Type: Promise<HTMLInputElement>
A promise that resolves to the input element.
getValidationMessage() => Promise<string>
Retrieves the validation message for the input field.
Returns
Type: Promise<string>
A promise that resolves to a string representing the validation message.
reportValidity() => Promise<boolean>
Reports the validity of the input field.
Returns
Type: Promise<boolean>
A promise that resolves to a boolean indicating whether the input field is valid.
setValidity(validity: ValidityState, validationMessage?: string) => Promise<void>
Sets the validity state of the input field.
Parameters
Name | Type | Description |
---|---|---|
validity | ValidityState | - The validity state to set. |
validationMessage | string | - An optional validation message to set. |
Returns
Type: Promise<void>
A Promise that resolves when the validity state is set.
willValidate() => Promise<boolean>
Returns a promise that resolves to true if the element will successfully validate, or false otherwise.
Returns
Type: Promise<boolean>
A promise that resolves to a boolean value indicating whether the element will validate.
Slots
Slot | Description |
---|---|
"right-icon" | slot intended for a DhlIcon to be displayed within DhlInputField |
Dependencies
Used by
Depends on
Graph
Built by DHL User Interface Library Team!
Migrating from DUIL 1.0
- Rename
onBlur
toblurEvent
- Rename
disabled
toisDisabled
- Rename
onChange
toinputEvent
rightIcon
now supplied via slot nameright-icon
- Remove prop
ariaExpanded
- Remove
isBlock
- Add
formnovalidate
- Add
dataAriaActivedescendant
- Add
dataAriaAutoComplete
- Add
dataAriaExpanded
- Add
autoComplete
- Add
dataAriaOwns
- Add
focusEvent
- Add
keyDownEvent
- Add
dataRole