@carbon/ibm-products-web-components
Version:
Carbon for IBM Products Web Components
211 lines (207 loc) • 6.86 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import "../../globals/settings.js";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.127.0/helpers/decorate.js";
import add_select_row_default$1 from "./add-select-row.scss.js";
import { classMap } from "lit/directives/class-map.js";
import { LitElement, html, nothing } from "lit";
import { property } from "lit/decorators.js";
import { carbonElement } from "@carbon/web-components/es/globals/decorators/carbon-element.js";
import { iconLoader } from "@carbon/web-components/es/globals/internal/icon-loader.js";
import '@carbon/web-components/es-custom/components/checkbox/index.js';
import '@carbon/web-components/es-custom/components/radio-button/index.js';
import ChevronRight16 from "@carbon/icons/es/chevron--right/16";
//#region src/components/add-select/add-select-row.ts
/**
* @license
*
* Copyright IBM Corp. 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const blockClass = `c4p--add-select__next-row`;
/**
* Add Select Row component - represents a single selectable row
* @element c4p-add-select-row
* @slot default - Custom content to render after the title/subtitle section. Useful for adding badges, tags, or other metadata to the row.
* @slot icon - Optional icon slot
* @slot meta - Optional metadata slot
* @fires c4p-add-select-row-select - Fired when row is selected/deselected
* @fires c4p-add-select-row-navigate - Fired when navigating to children
*/
let CDSAddSelectRow = class CDSAddSelectRow extends LitElement {
constructor(..._args) {
super(..._args);
this.itemId = "";
this.title = "";
this.subtitle = "";
this.value = "";
this.selected = false;
this.disabled = false;
this.hasChildren = false;
this.parentId = "";
}
/**
* Whether this is part of a multi-select list (inherited from parent c4p-add-select)
* @private
*/
get _multi() {
return this.closest(`c4p-add-select`)?.multi ?? false;
}
/**
* Handle navigation to children
*/
_handleNavigate(event) {
event.stopPropagation();
if (!this.hasChildren) return;
const init = {
bubbles: true,
cancelable: true,
composed: true,
detail: {
itemId: this.itemId,
title: this.title,
parentId: this.parentId
}
};
this.dispatchEvent(new CustomEvent(this.constructor.eventNavigate, init));
}
/**
* Handle selection change from checkbox/radio button
*/
_handleSelect(event) {
if (this.disabled) {
event.preventDefault();
return;
}
const target = event.target;
this.selected = target.checked || false;
this._emitSelectionEvent();
}
/**
* Emit selection event
*/
_emitSelectionEvent() {
const init = {
bubbles: true,
cancelable: true,
composed: true,
detail: {
itemId: this.itemId,
selected: this.selected,
value: this.value
}
};
this.dispatchEvent(new CustomEvent(this.constructor.eventSelect, init));
}
render() {
const { itemId, title, subtitle, selected, disabled, hasChildren, _handleSelect: handleSelect } = this;
return html`
<div
class=${classMap({
[`${blockClass}`]: true,
[`${blockClass}--selected`]: selected,
[`${blockClass}--disabled`]: disabled
})}
role="row"
aria-selected=${selected}
tabindex="-1"
?data-has-children=${hasChildren}
>
<div class="${blockClass}__cell" role="gridcell">
<div class="${blockClass}__cell-wrapper">
${this._multi ? html`
<cds-custom-checkbox
class="${blockClass}__checkbox"
?checked=${selected}
?disabled=${disabled}
@cds-custom-checkbox-changed=${handleSelect}
label-text=${title}
?hide-label=${true}
tabindex="-1"
>
</cds-custom-checkbox>
` : html`
<cds-custom-radio-button
?checked=${selected}
?disabled=${disabled}
@cds-custom-radio-button-changed=${handleSelect}
label-text=${title}
value=${itemId}
tabindex="-1"
>
</cds-custom-radio-button>
`}
<div class="${blockClass}__content">
<slot name="icon" class="${blockClass}__icon"></slot>
<div class="${blockClass}__text">
<div class="${blockClass}__title">${title}</div>
${subtitle && html`<div class="${blockClass}__subtitle">${subtitle}</div>`}
</div>
<slot></slot>
<slot name="meta"></slot>
</div>
${hasChildren ? html`
<div
class="${blockClass}__nav-indicator"
@click=${this._handleNavigate}
role="button"
tabindex="-1"
aria-label="Navigate to children"
>
<slot name="nav-icon">
${iconLoader(ChevronRight16, { slot: "icon" })}
</slot>
</div>
` : nothing}
</div>
</div>
</div>
`;
}
/**
* The name of the custom event fired when row is selected/deselected
*/
static get eventSelect() {
return `c4p-add-select-row-select`;
}
/**
* The name of the custom event fired when navigating to children
*/
static get eventNavigate() {
return `c4p-add-select-row-navigate`;
}
static {
this.styles = add_select_row_default$1;
}
};
__decorate([property({
type: String,
attribute: "item-id"
})], CDSAddSelectRow.prototype, "itemId", void 0);
__decorate([property({ type: String })], CDSAddSelectRow.prototype, "title", void 0);
__decorate([property({ type: String })], CDSAddSelectRow.prototype, "subtitle", void 0);
__decorate([property({ type: String })], CDSAddSelectRow.prototype, "value", void 0);
__decorate([property({
type: Boolean,
reflect: true
})], CDSAddSelectRow.prototype, "selected", void 0);
__decorate([property({ type: Boolean })], CDSAddSelectRow.prototype, "disabled", void 0);
__decorate([property({
type: Boolean,
attribute: "has-children"
})], CDSAddSelectRow.prototype, "hasChildren", void 0);
__decorate([property({
type: String,
attribute: "parent-id"
})], CDSAddSelectRow.prototype, "parentId", void 0);
CDSAddSelectRow = __decorate([carbonElement(`c4p-add-select-row`)], CDSAddSelectRow);
var add_select_row_default = CDSAddSelectRow;
//#endregion
export { add_select_row_default as default };
//# sourceMappingURL=add-select-row.js.map