@postnord/web-components
Version:
PostNord Web Components
592 lines (591 loc) • 23.3 kB
JavaScript
/*!
* Built with Stencil
* By PostNord.
*/
import { uuidv4 } from "../../../globals/helpers";
import { h, Host, Mixin } from "@stencil/core";
import { animateHeightFactory } from "../../../globals/mixins/index";
/**
* The `pn-radio-button` is built with a native `input[type="radio"]` in the background.
* This means you can use native events to listen to the changes.
*
* @nativeChange Use the `change` event to listen when the radio state is being changed.
*
* @slot - The default slot for adding custom HTML under the label/helpertext. {@since v7.17.0}
* @slot helpertext - Use this slot to add custom HTML inside of the helpertext element.
* Only recommended if you need a link without the `tile` prop. {@since v7.17.0}
* @slot content - When using the `tile` and `expand` props, this slot will be revealed when the input is checked. {@since v7.17.0}
*/
export class PnRadioButton extends Mixin(animateHeightFactory) {
constructor() {
super();
}
id = `pn-radio-${uuidv4()}`;
idHelpertext = `${this.id}-helpertext`;
contentArea;
radioInput;
hostElement;
/** The radio label */
label;
/** This adds an optional helpertext under the label. */
helpertext;
/** This will be emitted on change and input events. @category Native attributes */
value;
/** The name of the radio group. @category Native attributes */
name;
/** Programmatically check the input. @category Native attributes */
checked = false;
/** Set the radio as required. @category Native attributes */
required = false;
/** Disable the radio. @category Native attributes */
disabled = false;
/**
* Turn the radio into a clickable tile. A border and padding is added.
*
* **Do not** use interactive elements (links/buttons) inside of the slots when using this prop.
* An exception is made when using the `tile` + `expand` props together,
* which allows you to use interactive elements.
*
* @category Features
*/
tile = false;
/**
* Allow the radio to control the slot area "content".
* When checked, the area is visible, when unchecked, the area is hidden.
*
* The prop `tile` must be used at the same time.
* @see {@link tile}
* @since v7.17.0
* @category Features
*/
expand = false;
/**
* Add an icon on the right of your radio tile. The `tile` prop must be `true` for the icon to work.
* @see {@link tile}
* @category Features
*/
icon = null;
/**
* If set to true, color scheme will turn red, indicating that there is an issue or incorrect input related the radio.
* @category Features
*/
invalid = false;
/** A unique HTML ID for the radio. @since v7.25.0 @category HTML attributes */
pnId;
/**
* Provide the label via an aria attribute.
* We strongly recommend you use the `label` prop instead.
* @since v7.25.0
* @category HTML attributes
*/
pnAriaLabel;
/**
* Provide the label from another element via its ID.
* We strongly recommend you use the `label` prop instead.
* @since v7.25.0
* @category HTML attributes
*/
pnAriaLabelledby;
/**
* A unique HTML ID for the radio.
* @deprecated Use `pn-id` instead.
* @category HTML attributes
*/
radioid;
handleId() {
this.idHelpertext = `${this.getId()}-helpertext`;
}
handleChecked(checked) {
this.checked = checked ?? this.radioInput.checked;
if (!this.displayContent())
return;
if (this.checked) {
this.openDropdown(this.contentArea);
this.uncheckOtherRadios();
}
else {
this.closeDropdown(this.contentArea);
}
}
handleChange(e) {
const target = e.target;
const { name } = target;
const isSameRadioGroup = name === this.name;
const isSameRadio = target === this.radioInput && target.checked;
/**
* Since content can be nested inside of this component,
* double check that the event is coming from this specific radio.
*/
if (isSameRadioGroup)
this.checked = isSameRadio;
}
/**
* This is the same as the native `change` event, but without bubbling.
* This is useful when you want to listen to the event directly on the `pn-radio-button` element without
* having to worry about catching events from nested pn-radio-buttons.
*
* @since v7.25.0
**/
pnRadioChange;
componentDidLoad() {
requestAnimationFrame(() => this.displayContent() && this.checked && this.openDropdown(this.contentArea));
}
getId() {
return this.pnId || this.radioid || this.id;
}
uncheckOtherRadios() {
if (!this.name)
return;
const selector = `pn-radio-button[name="${this.name}"]`;
const list = document.querySelectorAll(selector);
const radios = Array.from(list);
radios.forEach(radio => radio !== this.hostElement && (radio.checked = false));
}
isInvalid() {
return this.invalid && !this.disabled;
}
displayText() {
return this.displayLabel() || this.displayHelpertext();
}
displayLabel() {
return !!this.label;
}
displayHelpertext() {
return !!(this.helpertext || this.hostElement.querySelector('[slot="helpertext"]')?.textContent);
}
displayIcon() {
return this.tile && !!this.icon;
}
displayContent() {
return this.tile && this.expand;
}
getAriaLabel() {
return !this.displayLabel() && !this.pnAriaLabelledby ? this.pnAriaLabel : null;
}
getAriaLabelledby() {
return !this.displayLabel() && !this.pnAriaLabel ? this.pnAriaLabelledby : null;
}
render() {
return (h(Host, { key: '4cc4729921eef5f9076bb2b3ce409d105c37e7fc' }, h("input", { key: '5081a5a1be4ebd3bfeca8559c3973b60e6257f6c', type: "radio", id: this.getId(), class: "pn-radio-input", value: this.value, name: this.name, disabled: this.disabled, checked: this.checked, required: this.required, "aria-label": this.getAriaLabel(), "aria-labelledby": this.getAriaLabelledby(), "aria-invalid": this.isInvalid()?.toString(), "aria-describedby": this.displayHelpertext() ? this.idHelpertext : null, "data-tile": this.tile, "data-expand": this.expand, onChange: event => this.pnRadioChange.emit(event), ref: el => (this.radioInput = el) }), h("div", { key: 'a933fc7fe850449df7c3c94ee387fa882b905fac', class: "pn-radio", "data-tile": this.tile, "data-expand": this.expand, "data-invalid": this.isInvalid() }, h("div", { key: '83f27a39013bd0674d1155aced4258da5c2cba58', class: "pn-radio-button" }, h("div", { key: 'fab4f7fbe57a2e9ae9c7cae444c2d603b23b6acb', class: "pn-radio-outer" }, h("div", { key: 'd672da19315841a7e1a46ada320739d8ced84a93', class: "pn-radio-inner" }))), h("div", { key: 'e863aa9e7396ccead7fed2227bbee45ca96c2187', class: "pn-radio-content", hidden: !this.displayText() }, h("label", { key: '221ea43c549af80b6b8c54fc611b0b48fe709f17', htmlFor: this.getId(), class: "pn-radio-label", hidden: !this.displayLabel() }, this.label), h("p", { key: '9a1cc75ef224d5d6830c35ee20742a14ef2268ba', id: this.idHelpertext, class: "pn-radio-helpertext", hidden: !this.displayHelpertext() }, this.helpertext, h("slot", { key: 'fb77032b6ce781fbc45bc9a5914b7c64b6cb3fd5', name: "helpertext" })), h("slot", { key: '73d298a9ef742031d83de42b737f5a6289ec692d' })), this.displayIcon() && h("pn-icon", { key: '8850771543bb69709196805cb46096bd706d32f7', icon: this.icon, color: "gray900" }), h("div", { key: '9ac6ed23cc447f4f7ed1bd0ab5311a211fc2f25d', class: "pn-radio-container", "data-open": this.checked, hidden: !this.displayContent(), style: { height: '0px' }, ref: el => (this.contentArea = el) }, h("div", { key: 'b952ea9f31572a6d5933f3ea3342283aaf9901e4', class: "pn-radio-area" }, h("slot", { key: '41e31f73b12b07ae647ccf89abcac486e5820592', name: "content" }))))));
}
static get is() { return "pn-radio-button"; }
static get originalStyleUrls() {
return {
"$": ["pn-radio-button.scss"]
};
}
static get styleUrls() {
return {
"$": ["pn-radio-button.css"]
};
}
static get properties() {
return {
"label": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The radio label"
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "label"
},
"helpertext": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "This adds an optional helpertext under the label."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "helpertext"
},
"value": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": true,
"optional": false,
"docs": {
"tags": [{
"name": "category",
"text": "Native attributes"
}],
"text": "This will be emitted on change and input events."
},
"getter": false,
"setter": false,
"reflect": true,
"attribute": "value"
},
"name": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Native attributes"
}],
"text": "The name of the radio group."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "name"
},
"checked": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Native attributes"
}],
"text": "Programmatically check the input."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "checked",
"defaultValue": "false"
},
"required": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Native attributes"
}],
"text": "Set the radio as required."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "required",
"defaultValue": "false"
},
"disabled": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Native attributes"
}],
"text": "Disable the radio."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "disabled",
"defaultValue": "false"
},
"tile": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Features"
}],
"text": "Turn the radio into a clickable tile. A border and padding is added.\n\n**Do not** use interactive elements (links/buttons) inside of the slots when using this prop.\nAn exception is made when using the `tile` + `expand` props together,\nwhich allows you to use interactive elements."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "tile",
"defaultValue": "false"
},
"expand": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "see",
"text": "{@link tile }"
}, {
"name": "since",
"text": "v7.17.0"
}, {
"name": "category",
"text": "Features"
}],
"text": "Allow the radio to control the slot area \"content\".\nWhen checked, the area is visible, when unchecked, the area is hidden.\n\nThe prop `tile` must be used at the same time."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "expand",
"defaultValue": "false"
},
"icon": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "see",
"text": "{@link tile }"
}, {
"name": "category",
"text": "Features"
}],
"text": "Add an icon on the right of your radio tile. The `tile` prop must be `true` for the icon to work."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "icon",
"defaultValue": "null"
},
"invalid": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "category",
"text": "Features"
}],
"text": "If set to true, color scheme will turn red, indicating that there is an issue or incorrect input related the radio."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "invalid",
"defaultValue": "false"
},
"pnId": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "since",
"text": "v7.25.0"
}, {
"name": "category",
"text": "HTML attributes"
}],
"text": "A unique HTML ID for the radio."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "pn-id"
},
"pnAriaLabel": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "since",
"text": "v7.25.0"
}, {
"name": "category",
"text": "HTML attributes"
}],
"text": "Provide the label via an aria attribute.\nWe strongly recommend you use the `label` prop instead."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "pn-aria-label"
},
"pnAriaLabelledby": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "since",
"text": "v7.25.0"
}, {
"name": "category",
"text": "HTML attributes"
}],
"text": "Provide the label from another element via its ID.\nWe strongly recommend you use the `label` prop instead."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "pn-aria-labelledby"
},
"radioid": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [{
"name": "deprecated",
"text": "Use `pn-id` instead."
}, {
"name": "category",
"text": "HTML attributes"
}],
"text": "A unique HTML ID for the radio."
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "radioid"
}
};
}
static get events() {
return [{
"method": "pnRadioChange",
"name": "pnRadioChange",
"bubbles": false,
"cancelable": true,
"composed": true,
"docs": {
"tags": [{
"name": "since",
"text": "v7.25.0"
}],
"text": "This is the same as the native `change` event, but without bubbling.\nThis is useful when you want to listen to the event directly on the `pn-radio-button` element without\nhaving to worry about catching events from nested pn-radio-buttons."
},
"complexType": {
"original": "Event",
"resolved": "Event",
"references": {
"Event": {
"location": "import",
"path": "@stencil/core",
"id": "node_modules::Event",
"referenceLocation": "Event"
}
}
}
}];
}
static get elementRef() { return "hostElement"; }
static get watchers() {
return [{
"propName": "radioid",
"methodName": "handleId"
}, {
"propName": "pnId",
"methodName": "handleId",
"handlerOptions": {
"immediate": true
}
}, {
"propName": "checked",
"methodName": "handleChecked"
}];
}
static get listeners() {
return [{
"name": "change",
"method": "handleChange",
"target": "window",
"capture": false,
"passive": false
}];
}
}