Skip to main content

InputField

A configurable InputField component used to gather user input through a text field.

Import

// 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"

Code

<DhlInputField
variant={{
label: 'Label',
placeholder: 'Placeholder',
type: Variants.static
}}
></DhlInputField>

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

PropertyAttributeDescriptionTypeDefault
autoCompleteauto-completeAn optional prop used to set the autocomplete value. It takes any valid value that can be used for the autocomplete attribute of an HTMLInputElement.stringundefined
blurEvent--[DEPRECATED] Use dhlBlur event instead.

An optional onBlur callback handler.
(event: FocusEvent) => voidundefined
dataAriaActivedescendantdata-aria-activedescendantAn optional prop used to identify the currently active element within a composite widget context.stringundefined
dataAriaAutoCompletedata-aria-auto-completeAn optional prop used to indicate whether inputting text could trigger display of one or more predictions of the user's intended value.stringundefined
dataAriaDescribedbydata-aria-describedbyAn optional prop defining the list of reference IDs (separated by spaces), recommended when you want to an error message on your field.stringundefined
dataAriaExpandeddata-aria-expandedAn optional prop used for assistive technology support - used to mark expandable and collapsible regions.stringundefined
dataAriaHasPopupdata-aria-has-popupAn optional prop that lets the screen reader know that this component has a popup.stringundefined
dataAriaLabeldata-aria-labelAn 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.stringundefined
dataAriaOwnsdata-aria-ownsAn 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.stringundefined
dataClassNamedata-class-nameAn optional class name prop for the component.stringundefined
dataIddata-idAn optional prop. Gives a valid HTML ID attribute value for the component.stringdhl-input-field-${getRandomString()}
dataRoledata-roleAn optional prop defining the role attribute of the component.stringundefined
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
dataTrackingdata-trackingAn optional data tracking prop for the component.stringundefined
disableValidationdisable-validationAn optional prop to disable the validation's visual feedback. Validation is still applied and validity state is still set to the form element.booleanfalse
focusEvent--[DEPRECATED] Use dhlFocus event instead.

An optional onFocus callback handler.
(event: FocusEvent) => voidundefined
formNoValidateform-no-validateAn optional prop used to set native formnovalidate attribute. This bypasses form control validation for form submission for the types image and submit.booleanundefined
inputEvent--[DEPRECATED] Use dhlInput event instead.

An optional onInput callback handler.
(event: InputEvent) => voidundefined
isDisableddisabledAn optional flag to define if the component is disabled.booleanfalse
keyDownEvent--[DEPRECATED] Use dhlKeyDown event instead.

An optional onKeyDown callback handler.
(event: KeyboardEvent) => voidundefined
listlistAn optional attribute that specifies a datalist for the input field.stringundefined
maxmaxAn optional prop describing the maximum value that can be entered in the input field. Only works with type="number", type="range", type="date".stringundefined
minminAn optional prop describing the minimum value that can be entered in the input field. Only works with type="number", type="range", type="date".stringundefined
namenameAn 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.stringundefined
patternpatternAn optional prop describing the regular expression pattern that the input value must match.stringundefined
readonlyreadonlyAn optional prop to flag the component as readonly within a form context.booleanfalse
requiredrequiredAn optional prop to flag the component as required within a form context.booleanundefined
searchIconsearch-iconAn 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.booleanundefined
srcsrcAn optional prop to give the source of the input element, only used for type="image".stringundefined
stepstepAn 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".stringundefined
typetypeAn 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
valuevalueAn 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

EventDescriptionType
dhlBlurEvent emitted when the input field loses focus.CustomEvent<{ value: string; }>
dhlChangeEvent emitted when the input field changes value.CustomEvent<{ value: string; }>
dhlFocusEvent emitted when the input field receives focus.CustomEvent<{ value: string; }>
dhlInputEvent emitted when the input value changes.CustomEvent<{ value: string; }>
dhlKeyDownEvent emitted when a key is pressed down on the input field.CustomEvent<KeyboardEvent>
dhlKeyUpEvent 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

NameTypeDescription
validityValidityState- The validity state to set.
validationMessagestring- 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

SlotDescription
"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 to blurEvent
  • Rename disabled to isDisabled
  • Rename onChange to inputEvent
  • rightIcon now supplied via slot name right-icon
  • Remove prop ariaExpanded
  • Remove isBlock
  • Add formnovalidate
  • Add dataAriaActivedescendant
  • Add dataAriaAutoComplete
  • Add dataAriaExpanded
  • Add autoComplete
  • Add dataAriaOwns
  • Add focusEvent
  • Add keyDownEvent
  • Add dataRole