Skip to main content

RadioButton

A configurable RadioButton component used to gather user input where only one choice can be selected from a series of options.

Import

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

Code

<DhlRadioButton>Example Text</DhlRadioButton>

Interactive Demo

Migrating from DUIL 1.0

  • Rename onChange to changeEvent
  • Rename disabled to isDisabled
  • Add formnovalidate

Readme

Usage

Dhl-radio-button

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-radio-button
name="dhl-radio-button"
value="value1"
>Radio Button 1</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value2"
>Radio Button 2</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value3"
>Radio Button 3</dhl-radio-button
>

<hr />

<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");
form.addEventListener("submit", async (e) => await e.target.checkValidity());
</script>

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

<form novalidate>
<dhl-radio-button
name="dhl-radio-button"
value="value1"
required
>Radio Button 1</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value2"
>Radio Button 2</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value3"
>Radio Button 3</dhl-radio-button
>

<hr />

<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");
form.addEventListener("submit", async (e) => {
e.preventDefault();
console.log(Object.fromEntries(new FormData(form)));
return await e.target.checkValidity();
});
</script>

usage with validation (required) and custom validation message

<form novalidate>
<dhl-radio-button
name="dhl-radio-button"
value="value1"
required
>Radio Button 1</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value2"
>Radio Button 2</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value3"
>Radio Button 3</dhl-radio-button
>

<hr />

<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
</form>

<script>
const form = document.querySelector("form");
const radioButtons = document.querySelectorAll("dhl-radio-button");
const validationMessageInvalid = "This field is invalid";
const validationMessageValid = "This field is valid";

form.addEventListener("submit", async (e) => {
e.preventDefault();
const isValid = await e.target.checkValidity();
radioButtons.forEach(async (radioButton) => {
const elementValid = await radioButton.checkValidity();

radioButton.validation = {
type: elementValid ? Variants.valid : Variants.invalid,
message: elementValid
? validationMessageValid
: validationMessageInvalid,
};
});
return isValid;
});
</script>

usage with custom events

<form novalidate>
<dhl-radio-button
name="dhl-radio-button"
value="value1"
required
>Radio Button 1</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value2"
>Radio Button 2</dhl-radio-button
>
<dhl-radio-button
name="dhl-radio-button"
value="value3"
>Radio Button 3</dhl-radio-button
>

<hr />

<dhl-button
type="reset"
variant="outline"
>reset</dhl-button
>
<dhl-button type="submit">submit</dhl-button>
</form>

<script>
const form = document.querySelector("form");
const radioButtons = document.querySelectorAll("dhl-radio-button");

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

radioButtons.forEach((radioButton) => {
radioButton.addEventListener("dhlChange", async (e) => {
const isValid = await radioButton.checkValidity();
console.log(radioButton, "isValid", isValid);
return isValid;
});
});
</script>

Properties

PropertyAttributeDescriptionTypeDefault
changeEvent--[DEPRECATED] Use dhlChange event instead.

An optional onChange callback handler.
(event: MouseEvent) => voidundefined
checkedcheckedAn optional prop used to describe either a toggled or un-toggled state of the component.booleannull
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
dataAriaInvaliddata-aria-invalidAn optional prop used to indicate that the form value of the component does not conform to the expected format.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
dataIdidAn optional prop. Gives a valid HTML ID attribute value for the component.stringdhl-radio-button-${getRandomString()}
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
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
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
renderLabelAsDivrender-label-as-divAn optional prop used to prevent a11y multiple label warning, setting renderLabelAsDiv to true presents the inner label as a "div" as opposed to a "label". Ensure that if set true, another label is used for the component - or the ariaLabel/ariaDescribedby prop is used.booleanundefined
requiredrequiredAn optional prop to flag the component as required within a form context.booleanundefined
sizesizeAn optional size prop for the component."md" \| "sm"DHL_RADIO_BUTTON.SIZE.MD
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

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; }>

Methods

checkValidity() => Promise<boolean>

Checks the validity of the custom 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 component.

Returns

Type: Promise<string>

A promise that resolves to a string representing the validation message.

reportValidity() => Promise<boolean>

Reports the validity of the custom 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 component.

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
"unnamed"unnamed children slot for text that accompanies the DhlRadioButton

Dependencies

Depends on

Graph


Built by DHL User Interface Library Team!