TextAreaField
A configurable TextArea component used to gather create multi-line text input.
Import
- React
- Angular
- Vue.js
// 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"
If the DUIL has been installed, you can use the web component directly:
<dhl-text-area-field></dhl-text-area-field>
// with @dhl-official/vue-library:
import { DhlTextAreaField } from "@dhl-official/vue-library"
// with @dhl-official/ui-libraries/vue-library:
import { DhlTextAreaField } from "@dhl-official/ui-libraries/vue-library"
Code
- React
- Angular
- Vue.js
<DhlTextAreaField></DhlTextAreaField>
<dhl-text-area-field></dhl-text-area-field>
<dhl-text-area-field></dhl-text-area-field>
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
Property | Attribute | Description | Type | Default |
---|---|---|---|---|
blurEvent | -- | [DEPRECATED] Use dhlBlur event instead.An optional onBlur callback handler. | (event?: FocusEvent) => void | 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 |
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 |
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-text-area-field-${getRandomString()} |
dataTestid | data-testid | An optional prop. The test id attached to the component as a data-testid attribute. | string | undefined |
hint | hint | An optional hint prop for used to display a hint below the component. | string | undefined |
inputEvent | -- | [DEPRECATED] Use dhlInput event instead.An optional onInput callback handler. | (event?: InputEvent) => void | undefined |
isDisabled | is-disabled | An optional flag to define if the component is disabled. | boolean | 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 |
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 |
rows | rows | An optional prop describing the number of text rows to be made available by the component. | number | 5 |
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 | undefined |
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
Event | Description | Type |
---|---|---|
dhlBlur | Event emitted when the component loses focus. | CustomEvent<{ value: string; }> |
dhlChange | Event emitted when the component changes value. | CustomEvent<{ value: string; }> |
dhlFocus | Event emitted when the component receives focus. | CustomEvent<{ value: string; }> |
dhlInput | Event emitted when the component value changes. | CustomEvent<{ value: string; }> |
dhlKeyDown | Event emitted when a key is pressed down on the component. | CustomEvent<KeyboardEvent> |
dhlKeyUp | Event 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
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.
Dependencies
Depends on
Graph
Built by DHL User Interface Library Team!
Migrating from DUIL 1.0
- Rename
disabled
toisDisabled
- Remove
isBlock