ranui
Version:
UI Component library based on `Web Component`
657 lines (656 loc) • 35.7 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { c as create, a as addClassToElement, r as removeClassToElement, i as isMobile, h as generateThrottle } from "./plus-BQnIzzvi.js";
import { c as createCustomError, i as isDisabled, H as HTMLElementSSR } from "./index-9tJmVuyv.js";
import "./index-Cs25ZDt6.js";
import "./index-CwCtKHVs.js";
import "./index-cO0-pAPm.js";
const f7170ee498e0dd32cbdcb63fba8f75cc = '.remove-wap-active-focus{outline:0;-webkit-tap-highlight-color:transparent}.remove-wap-active-focus:active,.remove-wap-active-focus:focus{outline:0;-webkit-tap-highlight-color:transparent}r-select{position:var(--ran-select, relative);width:var(--ran-select, 120px);height:var(--ran-select, 40px)}:host{position:var(--ran-select-host, relative);display:var(--ran-select-host, inline-block);cursor:var(--ran-select-host, pointer);height:var(--ran-select-host, 32px);outline:var(--ran-select-host, 0);-webkit-tap-highlight-color:var(--ran-select-host-highlight-color, transparent)}:host,:host(:focus),:host(:active){outline:var(--ran-select-host-focus-outline, 0);-webkit-tap-highlight-color:transparent}:host ::slotted(r-option){display:var(--ran-select-host-option-display, none)}:host([disabled]){cursor:var(--ran-select-host-disabled-cursor, not-allowed);pointer-events:var(--ran-select-host-disabled-pointer-events, all);opacity:var(--ran-select-host-disabled-opacity, .6)}:host(:not([disabled]):hover) .select{cursor:var(--ran-select-host-hover-cursor, pointer)}:host(:not([disabled],[type="text"]):hover) .selection{border:var(--ran-select-host-selection-border, 1px solid #1890ff)}:host(:not([disabled],[type="text"]):hover) .selection-search{cursor:var(--ran-select-host-selection-search-cursor, pointer)}:host(:not([disabled],[type="text"]):hover) .selection-item{cursor:var(--ran-select-host-selection-item-cursor, pointer);color:var(--ran-select-host-selection-item-color, #bfbfbf)}:host([type="text"])::part(selection){border:var(--ran-select-host-type-text-selection-border, none)}:host([type="text"])::part(icon){display:var(--ran-select-host-type-text-icon-display, none)}:host(:not([disabled]):focus) .selection{border:var(--ran-select-host-selection-focus-border, 1px solid #1890ff)}:host(:not([disabled]):focus) .selection-search{cursor:var(--ran-select-host-selection-focus-search-cursor, pointer)}:host(:not([disabled]):focus) .selection-item{cursor:var(--ran-select-host-selection-focus-item-cursor, pointer);color:var(--ran-select-host-selection-focus-item-color, #bfbfbf)}:host .selection-search{display:var(--ran-select-host-selection-search-display, none)}:host([showSearch]:not([disabled])) .selection-search{cursor:var(--ran-select-host-showSearch-selection-search-cursor, text);display:var(--ran-select-host-showSearch-selection-search-display, block)}:host([showSearch]:not([disabled])) .selection-item{cursor:var(--ran-select-host-showSearch-selection-item-cursor, pointer)}:host([showSearch]:not([disabled]):focus) .selection-search{display:var(--ran-select-host-showSearch-selection-search-display, block);cursor:var(--ran-select-host-showSearch-selection-search-cursor, text);opacity:var(--ran-select-host-showSearch-selection-search-opacity, 1)}.ran-select{width:var(--ran-select-width, 100%);height:var(--ran-select-width, 100%);font-size:var(--ran-select-font-size, 14px);box-sizing:var(--ran-select-box-sizing, border-box);margin:var(--ran-select-margin, 0);padding:var(--ran-select-padding, 0);color:var(--ran-select-color, rgba(0, 0, 0, .88));line-height:var(--ran-select-line-height, 1.57142857);list-style:var(--ran-select-list-style, none);font-family:var(--ran-select-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");position:var(--ran-select-position, relative);display:var(--ran-select-display, inline-block)}.ran-select .selection{position:var(--ran-select-selection-position, relative);background-color:var(--ran-select-selection-background-color, #ffffff);border:var(--ran-select-selection-border, 1px solid #d9d9d9);transition:var(--ran-select-selection-transition, all .2s cubic-bezier(.645, .045, .355, 1));width:var(--ran-select-selection-width, 100%);height:var(--ran-select-selection-height, 100%);padding:var(--ran-select-selection-padding, 0 11px);box-sizing:var(--ran-select-selection-box-sizing, border-box);margin:var(--ran-select-selection-margin, 0);color:var(--ran-select-selection-color, rgba(0, 0, 0, .88));font-size:var(--ran-select-selection-font-size, 14px);line-height:var(--ran-select-selection-line-height, 1.57142857);list-style:var(--ran-select-selection-list-style, none);font-family:var(--ran-select-selection-font-family, inherit);display:var(--ran-select-selection-display, flex);border-radius:var(--ran-select-selection-border-radius, 6px)}.ran-select .selection-search{position:var(--ran-select-selection-search-position, absolute);opacity:var(--ran-select-selection-search-opacity, 0);top:var(--ran-select-selection-search-top, 0);left:var(--ran-select-selection-search-left, 0);height:var(--ran-select-selection-search-height, 100%);inset-inline-start:var(--ran-select-selection-search-inset-inline-start, 11px);inset-inline-end:var(--ran-select-selection-search-inset-inline-end, 11px);margin:var(--ran-select-selection-search-margin, 0);padding:var(--ran-select-selection-search-padding, 0);background:var(--ran-select-selection-search-background, transparent);border:var(--ran-select-selection-search-border, none);outline:var(--ran-select-selection-search-outline, none);-webkit-appearance:var(--ran-select-selection-search-appearance, none);-moz-appearance:var(--ran-select-selection-search-appearance, none);appearance:var(--ran-select-selection-search-appearance, none);font-family:var(--ran-select-selection-search-font-family, inherit);cursor:var(--ran-select-selection-search-cursor, not-allowed)}.ran-select .selection-search::-webkit-search-cancel-button{display:var(--ran-select-host-selection-search-cancel-button-display, none);-webkit-appearance:var(--ran-select-host-selection-search-cancel-button-webkit-appearance, none)}.ran-select .selection-search::part(ran-input){border:var(--ran-select-host-selection-search-ran-input-border, none);padding:var(--ran-select-host-selection-search-ran-input-padding, 0);height:var(--ran-select-host-selection-search-ran-input-height, 100%);outline:var(--ran-select-host-selection-search-ran-input-outline, none)}.ran-select .selection-search::part(ran-input):active{border:var(--ran-select-host-selection-search-ran-input-active-border, none);padding:var(--ran-select-host-selection-search-ran-input-active-padding, 0);height:var(--ran-select-host-selection-search-ran-input-active-height, 100%);outline:var(--ran-select-host-selection-search-ran-input-active-outline, none);border-color:var(--ran-select-host-selection-search-ran-input-active-border-color, transparent);box-shadow:var(--ran-select-host-selection-search-ran-input-active-box-shadow, none);border-right-width:var(--ran-select-host-selection-search-ran-input-active-border-right-width, 0px)}.ran-select .selection-item{position:var(--ran-select-selection-item-position, absolute);top:var(--ran-select-selection-item-top, 0);left:var(--ran-select-selection-item-left, 12px);margin:var(--ran-select-selection-item-margin, 0);padding:var(--ran-select-selection-item-padding, 0);background:var(--ran-select-selection-item-background, transparent);border:var(--ran-select-selection-item-border, none);outline:var(--ran-select-selection-item-outline, none);-webkit-appearance:var(--ran-select-selection-item-appearance, none);-moz-appearance:var(--ran-select-selection-item-appearance, none);appearance:var(--ran-select-selection-item-appearance, none);font-family:var(--ran-select-selection-item-font-family, inherit);height:var(--ran-select-selection-item-height, 100%);-webkit-user-select:var(--ran-select-selection-item-user-select, none);user-select:var(--ran-select-selection-item-user-select, none);overflow:var(--ran-select-selection-item-overflow, hidden);white-space:var(--ran-select-selection-item-white-space, nowrap);text-overflow:var(--ran-select-selection-item-text-overflow, ellipsis);cursor:var(--ran-select-selection-item-cursor, pointer)}.ran-select .selection-select{color:var(--ran-select-selection-select-color, rgba(0, 0, 0, .25))}.ran-select .selection .icon{display:var(--ran-select-selection-icon-display, flex);align-items:var(--ran-select-selection-icon-align-items, center);color:var(--ran-select-selection-icon-color, rgba(0, 0, 0, .25));font-style:var(--ran-select-selection-icon-font-style, normal);text-align:var(--ran-select-selection-icon-text-align, center);text-transform:var(--ran-select-selection-icon-text-transform, none);text-rendering:var(--ran-select-selection-icon-text-rendering, optimizeLegibility);-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;position:var(--ran-select-selection-icon-position, absolute);top:var(--ran-select-selection-icon-top, 50%);inset-inline-start:var(--ran-select-selection-icon-inset-inline-start, auto);inset-inline-end:var(--ran-select-selection-icon-inset-inline-end, 8px);height:var(--ran-select-selection-icon-height, 12px);margin-top:var(--ran-select-selection-icon-margin-top, -3px);font-size:var(--ran-select-selection-icon-font-size, 12px);pointer-events:var(--ran-select-selection-icon-color, pointer-events)}';
function Custom$2() {
if (typeof document !== "undefined" && !customElements.get("r-option")) {
class Option extends HTMLElement {
constructor() {
super();
__publicField(this, "_option");
__publicField(this, "_optionContent");
__publicField(this, "_shadowDom");
__publicField(this, "_slot");
this._slot = document.createElement("slot");
this._option = document.createElement("div");
this._option.setAttribute("class", "ran-option");
this._optionContent = document.createElement("div");
this._optionContent.setAttribute("class", "ran-option-content");
this._optionContent.appendChild(this._slot);
this._option.appendChild(this._optionContent);
const shadowRoot = this.attachShadow({ mode: "closed" });
this._shadowDom = shadowRoot;
shadowRoot.appendChild(this._option);
}
static get observedAttributes() {
return ["disabled", "sheet", "value"];
}
get value() {
return this.getAttribute("value");
}
set value(value) {
this.setAttribute("value", value || "");
}
get sheet() {
return this.getAttribute("sheet");
}
set sheet(value) {
this.setAttribute("sheet", value || "");
}
get disabled() {
return isDisabled(this);
}
set disabled(value) {
if (!value || value === "false") {
this.removeAttribute("disabled");
} else {
this.setAttribute("disabled", "");
}
}
handlerExternalCss() {
if (this.sheet) {
try {
const sheet = new CSSStyleSheet();
sheet.insertRule(this.sheet);
this._shadowDom.adoptedStyleSheets = [sheet];
} catch (error) {
console.error(`Failed to parse the rule in CSSStyleSheet: ${this.sheet}, error: ${error}`);
}
}
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "disabled" && this._option) {
if (!newValue || newValue === "false") {
this._option.setAttribute("disabled", "");
} else {
this._option.removeAttribute("disabled");
}
}
if (name === "sheet" && this._shadowDom && oldValue !== newValue) this.handlerExternalCss();
}
}
return Option;
} else {
return createCustomError("document is undefined or r-option is exist");
}
}
Custom$2();
const less = '.remove-wap-active-focus{outline:0;-webkit-tap-highlight-color:transparent}.remove-wap-active-focus:active,.remove-wap-active-focus:focus{outline:0;-webkit-tap-highlight-color:transparent}.ranui-dropdown-option-item{position:var(--ran-dropdown-option-item-position, relative);display:var(--ran-dropdown-option-item-display, block);padding:var(--ran-dropdown-option-item-padding, 5px 12px);color:var(--ran-dropdown-option-item-color, rgba(0, 0, 0, .88));font-weight:var(--ran-dropdown-option-item-font-weight, normal);font-size:var(--ran-dropdown-option-item-font-size, 14px);transition:var(--ran-dropdown-option-item-transition, background .3s ease);border-radius:var(--ran-dropdown-option-item-border-radius, 4px);cursor:var(--ran-dropdown-option-item-cursor, pointer)}.ranui-dropdown-option-item:hover{background-color:var(--ran-dropdown-option-item-hover-background-color, rgba(0, 0, 0, .04))}.ranui-dropdown-option-item-content{text-align:var(--ran-dropdown-content-text-align, left);overflow:var(--ran-dropdown-content-overflow, hidden);white-space:var(--ran-dropdown-content-white-space, nowrap);text-overflow:var(--ran-dropdown-content-text-overflow, ellipsis);color:var(--ran-dropdown-content-color, rgba(0, 0, 0, .88));font-size:var(--ran-dropdown-content-font-size, 14px);line-height:var(--ran-dropdown-content-line-height, 1.57142857);box-sizing:var(--ran-dropdown-content-box-sizing, border-box);list-style:var(--ran-dropdown-content-list-style, none);font-family:var(--ran-dropdown-content-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");font-variant:var(--ran-dropdown-content-font-variant, initial)}.ranui-dropdown-option-active{background-color:var(--ran-dropdown-option-active-background-color, #e6f7ff);font-weight:var(--ran-dropdown-option-active-font-weight, bold)}.ranui-dropdown-option-active:hover{background-color:var(--ran-dropdown-option-active-hover-background-color, #e6f7ff)}';
class DropdownItem extends HTMLElementSSR() {
constructor() {
super();
__publicField(this, "ionDropdownItem");
__publicField(this, "_slot");
__publicField(this, "_shadowDom");
__publicField(this, "ionDropdownItemContent");
this._slot = create("slot").setAttribute("class", "slot");
this.ionDropdownItemContent = create("div").setAttribute("class", "ranui-dropdown-option-item-content").setAttribute("part", "ranui-dropdown-option-item-content").addChild(this._slot);
this.ionDropdownItem = create("div").setAttribute("class", "ranui-dropdown-option-item").setAttribute("part", "ranui-dropdown-option-item").addChild([this.ionDropdownItemContent]);
const shadowRoot = this.attachShadow({ mode: "closed" });
this._shadowDom = shadowRoot;
const style = create("style").setTextContent(less);
shadowRoot.appendChild(style.element);
shadowRoot.appendChild(this.ionDropdownItem.element);
}
static get observedAttributes() {
return ["active", "value", "title"];
}
get value() {
return this.getAttribute("value") || "";
}
set value(value) {
if (!isDisabled(this) && value) {
this.setAttribute("value", value);
} else {
this.removeAttribute("value");
}
}
get active() {
return this.getAttribute("active") || "";
}
set active(value) {
if (value) {
this.setAttribute("active", value);
} else {
this.removeAttribute("active");
}
}
get title() {
return this.getAttribute("title") || "";
}
set title(value) {
if (value) {
this.setAttribute("title", value);
} else {
this.removeAttribute("title");
}
}
connectedCallback() {
if (this.active) {
addClassToElement(this.ionDropdownItem.element, "ranui-dropdown-option-active");
}
}
attributeChangedCallback(name, _, newValue) {
if (name === "active" && newValue) {
addClassToElement(this.ionDropdownItem.element, "ranui-dropdown-option-active");
} else {
removeClassToElement(this.ionDropdownItem.element, "ranui-dropdown-option-active");
}
}
}
function Custom$1() {
if (typeof document !== "undefined" && !customElements.get("r-dropdown-item")) {
customElements.define("r-dropdown-item", DropdownItem);
return DropdownItem;
} else {
return createCustomError("document is undefined or r-dropdown-item is exist");
}
}
Custom$1();
const placementDirection = {
bottom: {
add: "ran-dropdown-down-in",
remove: "ran-dropdown-down-out"
},
top: {
add: "ran-dropdown-up-in",
remove: "ran-dropdown-up-out"
}
};
const searchThrottle = generateThrottle();
const animationTime = 300;
class Select extends HTMLElementSSR() {
constructor() {
super();
__publicField(this, "removeTimeId");
__publicField(this, "_slot");
__publicField(this, "_shadowDom");
__publicField(this, "_select");
__publicField(this, "_selection");
__publicField(this, "_search");
__publicField(this, "_icon");
__publicField(this, "_selectDropdown");
__publicField(this, "_selectionDropdown");
__publicField(this, "_selectDropDownInTimeId");
__publicField(this, "_selectDropDownOutTimeId");
__publicField(this, "_optionList");
__publicField(this, "_optionLabelMapValue");
__publicField(this, "_optionValueMapLabel");
__publicField(this, "_activeOption");
__publicField(this, "_text");
__publicField(this, "_selector");
__publicField(this, "onSearch");
/**
* @description: 移除 select dropdown
* @return {*}
*/
__publicField(this, "setSelectDropdownDisplayNone", () => {
if (this._selectDropDownOutTimeId) return;
if (this._selectionDropdown && this._selectionDropdown.style.display !== "none") {
this._selectionDropdown.setAttribute("transit", placementDirection[this.placement].remove);
this._selectDropDownOutTimeId = setTimeout(() => {
var _a;
(_a = this._selectionDropdown) == null ? void 0 : _a.style.setProperty("display", "none");
this._selectionDropdown && this._selectionDropdown.removeAttribute("transit");
clearTimeout(this._selectDropDownOutTimeId);
this._selectDropDownOutTimeId = void 0;
}, animationTime);
}
});
/**
* @description: 添加 select dropdown
* @return {*}
*/
__publicField(this, "setSelectDropdownDisplayBlock", () => {
var _a;
if (this._selectDropDownInTimeId) return;
if (this._selectionDropdown && this._selectionDropdown.style.display !== "block") {
this._selectionDropdown.setAttribute("transit", placementDirection[this.placement].add);
(_a = this._selectionDropdown) == null ? void 0 : _a.style.setProperty("display", "block");
this._selectDropDownInTimeId = setTimeout(() => {
this._selectionDropdown && this._selectionDropdown.removeAttribute("transit");
clearTimeout(this._selectDropDownInTimeId);
this._selectDropDownInTimeId = void 0;
}, animationTime);
}
});
__publicField(this, "placementPosition", () => {
if (!this._selectionDropdown || !this._selectDropdown) return;
const rect = this.getBoundingClientRect();
const { top, left, bottom, width } = rect;
const root = document.getElementById(this.getPopupContainerId);
this._selectionDropdown.style.setProperty("position", `absolute`);
this._selectionDropdown.style.setProperty("--ran-x", `${top + window.scrollX}`);
this._selectionDropdown.style.setProperty("--ran-y", `${left + window.scrollY}`);
let selectTop = bottom + window.scrollY;
let selectLeft = left + window.scrollX;
this._selectionDropdown.style.setProperty("width", `${width}px`);
if (this.placement === "top") {
selectTop = top + window.scrollY - this._selectionDropdown.clientHeight;
}
if (this.getPopupContainerId && root) {
const rootRect = root.getBoundingClientRect();
selectLeft = left - rootRect.left;
if (this.placement === "top") {
selectTop = top - root.getBoundingClientRect().top - this._selectionDropdown.clientHeight;
} else {
selectTop = root.getBoundingClientRect().height;
}
}
this._selectionDropdown.style.setProperty("inset", `${selectTop}px auto auto ${selectLeft}px`);
});
/**
* @description: 设置下拉框
* @return {*}
*/
__publicField(this, "selectMouseDown", (e) => {
e.stopPropagation();
if (isDisabled(this)) return;
this.removeDropDownTimeId(e);
this.setSelectDropdownDisplayNone();
this.setSelectDropdownDisplayBlock();
this.placementPosition();
});
__publicField(this, "removeDropDownTimeId", (e) => {
e.stopPropagation();
this._search.setAttribute("value", "");
if (this.trigger.includes("hover") && !isMobile()) {
clearTimeout(this.removeTimeId);
this.removeTimeId = void 0;
}
});
/**
* @description: 焦点移除的情况,需要移除 select 下拉框
* @return {*}
*/
__publicField(this, "selectBlur", (e) => {
e.stopPropagation();
if (this.removeTimeId) {
this.removeDropDownTimeId(e);
}
this.removeTimeId = setTimeout(() => {
this.removeDropDownTimeId(e);
this.setSelectDropdownDisplayNone();
}, 300);
});
/**
* @description: 选中一个选项的情况
* @param {MouseEvent} e
* @return {*}
*/
__publicField(this, "clickOption", (e) => {
e.stopPropagation();
const element = e.target;
const label = element.innerHTML;
const value = this._optionLabelMapValue.get(label);
if (value) {
this.setAttribute("value", value);
this._text.innerHTML = label;
this._text.setAttribute("title", label);
this._search.setAttribute("placeholder", label);
}
const rect = this.getBoundingClientRect();
const { height } = rect;
this._text.style.setProperty("line-height", `${height}px`);
if (this._activeOption) {
this._activeOption.removeAttribute("active");
}
this._activeOption = element;
if (this._activeOption) {
this._activeOption.setAttribute("active", value || "");
}
this.setSelectDropdownDisplayNone();
this.dispatchEvent(new CustomEvent("change", { detail: { value, label } }));
this.removeDropDownTimeId(e);
});
/**
* @description: 初始化创建选项下拉框
* @return {*}
*/
__publicField(this, "createOption", () => {
if (!this._selectDropdown) {
const container = document.getElementById(this.getPopupContainerId) || document.body;
this._selectDropdown = document.createElement("div");
this._selectDropdown.style.setProperty("-webkit-tap-highlight-color", "transparent");
this._selectDropdown.style.setProperty("outline", "0");
this._selectDropdown.addEventListener("click", this.clickOption);
this._selectionDropdown = document.createElement("r-dropdown");
if (this.dropdownclass) {
this._selectionDropdown.setAttribute("class", this.dropdownclass);
}
if (this.trigger.includes("hover") && !isMobile()) {
this._selectDropdown.addEventListener("mouseleave", this.selectBlur);
this._selectDropdown.addEventListener("mouseenter", this.removeDropDownTimeId);
}
this._selectDropdown.appendChild(this._selectionDropdown);
this._selectionDropdown.style.setProperty("display", "none");
container.appendChild(this._selectDropdown);
}
});
/**
* @description: 移除选项下拉框
* @return {*}
*/
__publicField(this, "removeSelectDropdown", () => {
try {
if (this._selectDropdown) {
const container = document.getElementById(this.getPopupContainerId) || document.body;
container.removeChild(this._selectDropdown);
}
} catch (error) {
console.error("removeSelectDropdown error", error);
}
});
/**
* @description: 当 select 中有 option 元素的时候,给 dropdown 添加元素
* @return {*}
*/
__publicField(this, "addOptionToSlot", () => {
const slots = this._slot.assignedElements();
slots.forEach((item) => {
var _a;
if (item.tagName !== "R-OPTION") return;
const label = item.innerHTML;
const value = item.getAttribute("value") || "";
(_a = this._optionList) == null ? void 0 : _a.push({ label, value });
if (this._optionLabelMapValue.get(label)) {
console.warn(`${label} is repeat option`);
}
if (this._optionValueMapLabel.get(value)) {
console.warn(`${value} is repeat option`);
}
this._optionLabelMapValue.set(label, value);
this._optionValueMapLabel.set(value, label);
});
this.createSelectDropdownContent(this._optionList);
});
__publicField(this, "createSelectDropdownContent", (options = []) => {
var _a, _b;
if (options.length === 0) {
(_a = this._selectDropdown) == null ? void 0 : _a.style.setProperty("display", "none");
} else {
(_b = this._selectDropdown) == null ? void 0 : _b.style.setProperty("display", "block");
}
options.forEach((item) => {
if (this._selectionDropdown) {
const { label, value } = item;
const selectOptionItem = document.createElement("r-dropdown-item");
const defaultValue = this.getAttribute("defaultValue") || this.getAttribute("value");
if (defaultValue === value) {
selectOptionItem.setAttribute("active", value);
this._activeOption = selectOptionItem;
} else {
selectOptionItem.removeAttribute("active");
}
selectOptionItem.innerHTML = `${label}`;
selectOptionItem.setAttribute("value", `${value}`);
selectOptionItem.setAttribute("title", `${label}`);
this._selectionDropdown.appendChild(selectOptionItem);
}
});
this.setDefaultValue();
});
__publicField(this, "setDefaultValue", () => {
const defaultValue = this.getAttribute("defaultValue") || this.getAttribute("value");
if (!defaultValue) return;
const label = this._optionValueMapLabel.get(defaultValue);
if (!label) return;
this.setAttribute("value", defaultValue);
const rect = this.getBoundingClientRect();
const { height } = rect;
this._text.style.setProperty("line-height", `${height}px`);
this._text.innerHTML = label;
this._text.setAttribute("title", label);
});
__publicField(this, "changeSearch", (e) => {
const value = e.detail.value || "";
this.dispatchEvent(
new CustomEvent("search", {
detail: { value }
})
);
if (this._selectionDropdown) {
this._selectionDropdown.innerHTML = "";
}
if (value.length > 0) {
const options = this._optionList.map((item) => {
const { label } = item;
if (`${label}`.toLowerCase().includes(value)) {
return { label, value: item.value };
}
return void 0;
}).filter((item) => item);
this.createSelectDropdownContent(options);
} else {
this.createSelectDropdownContent(this._optionList);
}
});
__publicField(this, "setShowSearch", () => {
this.onSearch = searchThrottle(this.changeSearch);
this.onSearch && this._search.addEventListener("change", this.onSearch);
this.onSearch && this._search.addEventListener("click", this.onSearch);
});
__publicField(this, "removeShowSearch", () => {
this.onSearch && this._search.removeEventListener("change", this.onSearch);
this.onSearch && this._search.removeEventListener("click", this.onSearch);
});
__publicField(this, "listenSlotChange", () => {
this._slot.addEventListener("slotchange", this.addOptionToSlot);
});
__publicField(this, "removeListenSlotChange", () => {
this._slot.removeEventListener("slotchange", this.addOptionToSlot);
});
__publicField(this, "listenActionEvent", () => {
this.removeEventListener("mouseenter", this.selectMouseDown);
this.removeEventListener("mouseleave", this.selectBlur);
this.removeEventListener("click", this.selectMouseDown);
this.removeEventListener("blur", this.selectBlur);
if (this.trigger.includes("hover") && !isMobile()) {
this.addEventListener("mouseenter", this.selectMouseDown);
this.addEventListener("mouseleave", this.selectBlur);
}
if (this.trigger.includes("click")) {
this.addEventListener("click", this.selectMouseDown);
this.addEventListener("blur", this.selectBlur);
}
});
__publicField(this, "clickRemoveSelect", (e) => {
e.stopPropagation();
this.setSelectDropdownDisplayNone();
});
this._slot = document.createElement("slot");
this._select = document.createElement("div");
this._select.setAttribute("class", "ran-select");
this._select.setAttribute("part", "select");
this._selection = document.createElement("div");
this._selection.setAttribute("class", "selection");
this._selection.setAttribute("part", "selection");
this._selector = document.createElement("div");
this._search = document.createElement("r-input");
this._search.setAttribute("class", "selection-search");
this._search.setAttribute("part", "search");
this._search.setAttribute("type", "search");
this._search.setAttribute("autocomplete", "off");
this._text = document.createElement("span");
this._text.setAttribute("class", "selection-item");
this._text.setAttribute("part", "selection-item");
this._icon = document.createElement("r-icon");
this._icon.setAttribute("class", "icon");
this._icon.setAttribute("part", "icon");
this._icon.setAttribute("name", "arrow-down");
this._icon.setAttribute("color", "#d9d9d9");
this._icon.setAttribute("size", "16");
this._selector.appendChild(this._text);
this._selector.appendChild(this._search);
this._selection.appendChild(this._icon);
this._selection.appendChild(this._selector);
this._slot.setAttribute("class", "slot");
this._select.appendChild(this._selection);
this._select.appendChild(this._slot);
this._optionList = [];
this._optionLabelMapValue = /* @__PURE__ */ new Map();
this._optionValueMapLabel = /* @__PURE__ */ new Map();
const shadowRoot = this.attachShadow({ mode: "closed" });
const F7170EE498E0DD32CBDCB63FBA8F75CC = document.createElement("style");
F7170EE498E0DD32CBDCB63FBA8F75CC.textContent = f7170ee498e0dd32cbdcb63fba8f75cc;
shadowRoot.appendChild(F7170EE498E0DD32CBDCB63FBA8F75CC);
this._shadowDom = shadowRoot;
this._shadowDom.appendChild(this._select);
}
static get observedAttributes() {
return [
"disabled",
"sheet",
"clear",
"type",
"defaultValue",
"showSearch",
"placement",
// 弹窗的方向
"getPopupContainerId",
// 挂载的节点
"dropdownclass",
// 弹窗的类名
"trigger"
// 触发下拉框的行为,click 还是 hover,hover 在 isMobile 移动端无效
];
}
get value() {
return this.getAttribute("value") || "";
}
set value(value) {
if (!isDisabled(this) && value) {
this.setAttribute("value", value);
} else {
this.removeAttribute("value");
}
}
get defaultValue() {
return this.getAttribute("defaultValue") || "";
}
set defaultValue(value) {
this.setAttribute("defaultValue", value || "");
}
get showSearch() {
return this.getAttribute("showSearch") || "";
}
set showSearch(value) {
this.setAttribute("showSearch", value || "");
}
get type() {
return this.getAttribute("type") || "";
}
set type(value) {
this.setAttribute("type", value || "");
}
get placement() {
return this.getAttribute("placement") || "bottom";
}
set placement(value) {
this.setAttribute("placement", value || "");
}
get sheet() {
return this.getAttribute("sheet") || "";
}
set sheet(value) {
this.setAttribute("sheet", value || "");
}
get getPopupContainerId() {
return this.getAttribute("getPopupContainerId") || "";
}
set getPopupContainerId(value) {
this.setAttribute("getPopupContainerId", value || "");
}
get dropdownclass() {
return this.getAttribute("dropdownclass") || "";
}
set dropdownclass(value) {
this.setAttribute("dropdownclass", value || "");
}
get trigger() {
return this.getAttribute("trigger") || "click";
}
set trigger(value) {
this.setAttribute("trigger", value || "");
}
get disabled() {
return isDisabled(this);
}
set disabled(value) {
if (!value || value === "false") {
this.removeAttribute("disabled");
this._selection.removeAttribute("disabled");
} else {
this.setAttribute("disabled", "");
this._selection.setAttribute("disabled", "");
}
}
handlerExternalCss() {
if (this.sheet) {
try {
const sheet = new CSSStyleSheet();
sheet.insertRule(this.sheet);
this._shadowDom.adoptedStyleSheets = [sheet];
} catch (error) {
console.error(`Failed to parse the rule in CSSStyleSheet: ${this.sheet}, error: ${error}`);
}
}
}
connectedCallback() {
this.handlerExternalCss();
this.createOption();
this.listenActionEvent();
this.listenSlotChange();
this.setShowSearch();
document.addEventListener("click", this.clickRemoveSelect);
}
disconnectCallback() {
var _a;
this.removeEventListener("mouseenter", this.selectMouseDown);
this.removeEventListener("mouseleave", this.selectBlur);
this.removeEventListener("click", this.selectMouseDown);
this.removeEventListener("blur", this.selectBlur);
this.removeSelectDropdown();
(_a = this._selectDropdown) == null ? void 0 : _a.removeEventListener("click", this.clickOption);
this.removeListenSlotChange();
document.removeEventListener("click", this.clickRemoveSelect);
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "disabled" && this._select) {
if (!newValue || newValue === "false") {
this._select.setAttribute("disabled", "");
this._selection.setAttribute("disabled", "");
} else {
this._select.removeAttribute("disabled");
this._selection.removeAttribute("disabled");
}
}
if (name === "sheet" && this._shadowDom && oldValue !== newValue) {
this.handlerExternalCss();
}
}
}
function Custom() {
if (typeof document !== "undefined" && !customElements.get("r-select")) {
customElements.define("r-select", Select);
return Select;
} else {
return createCustomError("document is undefined or r-select is exist");
}
}
const index = Custom();
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
Select,
default: index
}, Symbol.toStringTag, { value: "Module" }));
export {
Select as S,
index as a,
index$1 as i
};