Skip to main content

TextAreaField

A configurable TextArea component used to gather create multi-line text input.

Import

// with @dhl-official/react-library:
import { DhlTextAreaField } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlTextAreaField } from "@dhl-official/ui-libraries/react-library"

Code

<DhlTextAreaField></DhlTextAreaField>

Interactive Demo

Readme

Usage

Dhl-text-area-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-text-area-field
name="dhl-text-area-field-custom-element"
></dhl-text-area-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 textArea = document.querySelector("dhl-text-area-field");

textArea.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};

form.addEventListener("submit", async (e) => {
e.preventDefault();
const isValid = await e.target.checkValidity();
console.log(Object.fromEntries(new FormData(form)));
return isValid;
});
</script>

usage with validation (required) and (browser) default validation message

<form novalidate>
<dhl-text-area-field
name="dhl-text-area-field-custom-element"
required
>
</dhl-text-area-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 textArea = document.querySelector("dhl-text-area-field");

textArea.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};

form.addEventListener("submit", async (e) => {
e.preventDefault();
const isValid = await e.target.checkValidity();
console.log(Object.fromEntries(new FormData(form)));
return isValid;
});
</script>

usage with validation (required) and custom validation message

<form novalidate>
<dhl-text-area-field
name="dhl-text-area-field-custom-element"
required
>
</dhl-text-area-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 textArea = document.querySelector("dhl-text-area-field");

textArea.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};

const validationMessageInvalid = "This field is invalid";
const validationMessageValid = "This field valid";

form.addEventListener("submit", async (e) => {
e.preventDefault();
const isValid = await e.target.checkValidity();
if (isValid) {
textArea.validation = {
type: "valid",
message: validationMessageValid,
};
} else {
textArea.validation = {
type: "invalid",
message: validationMessageInvalid,
};
}
return isValid;
});
</script>

usage with custom events

<form novalidate>
<dhl-text-area-field
name="dhl-text-area-field-custom-element"
required
>
</dhl-text-area-field>
<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
<hr />
<dhl-text></dhl-text>
</form>

<script type="module">
const form = document.querySelector("form");
const textArea = document.querySelector("dhl-text-area-field");
const text = document.querySelector("dhl-text");

textArea.variant = {
label: "Label",
placeholder: "Placeholder",
type: "animated",
};

const validationMessageInvalid = "This field is invalid";
const validationMessageValid = "This field valid";

textArea.addEventListener("dhlBlur", async (e) => {
e.preventDefault();
console.log(e.type, e.target.value);
const isValid = await e.target.checkValidity();
if (isValid) {
textArea.validation = {
type: "valid",
message: validationMessageValid,
};
} else {
textArea.validation = {
type: "invalid",
message: validationMessageInvalid,
};
}
});

textArea.addEventListener("dhlChange", async (e) => {
text.innerHTML = e.target.value;
});

form.addEventListener("submit", async (e) => {
e.preventDefault();
const isValid = await e.target.checkValidity();
console.log(Object.fromEntries(new FormData(form)));
return isValid;
});

form.addEventListener("reset", (e) => {
text.innerHTML = "";
});
</script>

Properties

PropertyAttributeDescriptionTypeDefault
blurEvent--[DEPRECATED] Use dhlBlur event instead.

An optional onBlur callback handler.
(event?: FocusEvent) => voidundefined
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
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
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-text-area-field-${getRandomString()}
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
hinthintAn optional hint prop for used to display a hint below the component.stringundefined
inputEvent--[DEPRECATED] Use dhlInput event instead.

An optional onInput callback handler.
(event?: InputEvent) => voidundefined
isDisabledis-disabledAn optional flag to define if the component is disabled.booleanundefined
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
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
rowsrowsAn optional prop describing the number of text rows to be made available by the component.number5
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.stringundefined
variant--An optional 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 component loses focus.CustomEvent<{ value: string; }>
dhlChangeEvent emitted when the component changes value.CustomEvent<{ value: string; }>
dhlFocusEvent emitted when the component receives focus.CustomEvent<{ value: string; }>
dhlInputEvent emitted when the component value changes.CustomEvent<{ value: string; }>
dhlKeyDownEvent emitted when a key is pressed down on the component.CustomEvent<KeyboardEvent>
dhlKeyUpEvent emitted when a key is released on the component. *CustomEvent<KeyboardEvent>

Methods

checkValidity() => Promise<boolean>

Checks the validity of the component.

Returns

Type: Promise<boolean>

A promise that resolves to true if the component is valid, otherwise false.

getTextareaElement() => Promise<HTMLTextAreaElement>

Retrieves the text area element asynchronously.

Returns

Type: Promise<HTMLTextAreaElement>

A promise that resolves to the text area 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 component.

Returns

Type: Promise<boolean>

A promise that resolves to a boolean indicating whether the component is valid.

setValidity(validity: ValidityState, validationMessage?: string) => Promise<void>

Sets the validity state of the text area.

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.

Dependencies

Depends on

Graph


Built by DHL User Interface Library Team!

Migrating from DUIL 1.0

  • Rename disabled to isDisabled
  • Remove isBlock