This commit is contained in:
lchen committed 2026-11-23 10:41:08 +08:00
1 parent a0fe4cb7b1
commit 5cb14ffd44
11 files changed
+157 -96

No files matched your search

+44 -69
View File
@@ -1,16 +1,29 @@
import './css/dropdown.scss';
import { r as lang } from "../utility/lgres";
import { contains, nullOrEmpty } from "../utility/strings";
import { global, isPositive, throttle } from "../utility";
import { mergeOptions, global, isPositive, throttle } from "../utility";
import { createElement } from "../functions";
import { createCheckbox } from "./checkbox";
import { createIcon } from "./icon"
/** @import { DropdownOptions } from './dropdown.types' */
/** @satisfies {Partial<DropdownOptions>} */
const DEFAULT_OPTIONS = {
textKey: 'text',
valueKey: 'value',
htmlKey: 'html',
childKey: 'children',
maxLength: 500
};
/** @typedef {import('../types').MarkRequired<DropdownOptions, keyof typeof DEFAULT_OPTIONS>} ResolvedDropdownOptions */
const SymbolDropdown = Symbol.for('ui-dropdown');
const DropdownItemHeight = 30;
let r = lang;
let dropdownGlobal = global[SymbolDropdown];
let dropdownGlobal = /** @type {Record<symbol, any | undefined>} */ (/** @type {unknown} */ (global))[SymbolDropdown];
if (dropdownGlobal == null) {
// init
@@ -38,7 +51,11 @@ if (dropdownGlobal == null) {
}
}
})
global[SymbolDropdown] = dropdownGlobal;
Object.defineProperty(global, SymbolDropdown, {
value: dropdownGlobal,
writable: true,
configurable: true
});
document.addEventListener('mousedown', e => {
let parent = e.target;
@@ -124,31 +141,6 @@ function getValue(it, valuekey, textkey) {
return value;
}
/**
* 下拉列表参数对象
* @typedef DropdownOptions
* @property {string} textKey 文本关键字
* @property {string} valueKey 值关键字
* @property {string} htmlKey 源码显示的关键字
* @property {string} childKey 子集合的关键字
* @property {number} maxLength 最大输入长度
* @property {(this: Dropdown, it: DropdownItem) => string} [htmlTemplate] 模板创建函数
* @property {boolean} [multiSelect] 是否允许多选
* @property {string} [selected] 选中值
* @property {string[]} [selectedList] 选中的数组
* @property {boolean} [disabled] 是否禁用
* @property {boolean} [input] 是否支持输入
* @property {boolean} [search] 是否支持搜索
* @property {string[]} [searchKeys] 搜索的关键字数组
* @property {string} [searchPlaceholder] 搜索提示文本,默认值取语言资源 `searchHolder` "Search..."
* @property {number} [tabIndex] 焦点索引
* @property {string} [placeholder] 输入框的提示文本
* @property {boolean} [slideFixed] 是否固定为向下展开
* @property {HTMLElement} [wrapper] 父元素,默认添加到头元素之后
* @property {boolean} [ignoreAll] 是否忽略 All 选项
* @property {import('../types').GetTextCallback} [getText] 获取文本的代理函数
*/
/**
* 数据项基础类型
* @typedef DropdownItemBase
@@ -165,14 +157,8 @@ function getValue(it, valuekey, textkey) {
export class Dropdown {
_var = {
/** @type {DropdownOptions} */
options: {
textKey: 'text',
valueKey: 'value',
htmlKey: 'html',
childKey: 'children',
maxLength: 500
},
/** @type {ResolvedDropdownOptions} */
options: DEFAULT_OPTIONS,
/** @type {HTMLDivElement | undefined} */
wrapper: undefined,
/** @type {HTMLDivElement | undefined} */
@@ -210,14 +196,9 @@ export class Dropdown {
*/
constructor(options) {
if (options != null) {
this._var.options = options;
this._var.options = mergeOptions(DEFAULT_OPTIONS, options);
}
const opts = this._var.options;
opts.textKey ??= 'text';
opts.valueKey ??= 'value';
opts.htmlKey ??= 'html';
opts.childKey ??= 'children';
opts.maxLength ??= 500;
const getText = opts.getText;
if (typeof getText === 'function') {
r = getText;
@@ -398,11 +379,19 @@ export class Dropdown {
if (!Array.isArray(list)) {
return [];
}
return list.reduce((array, item) => [
...array,
(id == null ? item : { __p: String(id), __level: level, ...item }),
...reduceItems(item[childkey], item[valuekey], level + 1)
], []);
return list.reduce(
/**
* @private
* @param {DropdownItem[]} array
* @param {DropdownItem} item
* @returns {DropdownItem[]}
*/
(array, item) => [
...array,
(id == null ? item : { __p: String(id), __level: level, ...item }),
...reduceItems(item[childkey], item[valuekey], level + 1)
],
[]);
}
this._var.source = reduceItems(list);
if (this._expanded) {
@@ -521,7 +510,6 @@ export class Dropdown {
return item;
});
this._var.allChecked = false;
source.forEach(it => delete it.__checked);
if (itemlist.length === 0) {
delete this._var.selectedList;
if (this._var.label != null) {
@@ -555,22 +543,12 @@ export class Dropdown {
const source = filterSource(options.searchKeys, options.textKey, key, this.source);
this._filllist(source);
});
input.addEventListener('blur', e => {
if (!(e.target instanceof HTMLInputElement)) {
return;
}
const key = e.target.value.toLowerCase();
if (!nullOrEmpty(key)) {
const list = this._getSelectedList(this._var.currentSource);
this._selectList(list);
}
});
search.append(input, createIcon('fa-light', 'search'));
panel.appendChild(search);
}
// list
const list = createElement('div', 'ui-drop-list');
list.addEventListener('scroll', e => throttle(this._onlistscroll, 10, this, list, e.target?.scrollTop), { passive: true });
list.addEventListener('scroll', e => throttle(this._onlistscroll, 10, this, list, /** @type {Element} */(e.target)?.scrollTop), { passive: true });
if (!this.multiSelect) {
list.addEventListener('click', e => {
let li = e.target;
@@ -731,7 +709,7 @@ export class Dropdown {
const childkey = this._var.options.childKey;
const allchecked = this._var.allChecked;
const selectedlist = this.selectedList;
source.forEach((item, i) => {
source.forEach((item) => {
const val = String(getValue(item, valuekey, textkey));
if (multiselect) {
const selected = selectedlist.some(s => String(getValue(s, valuekey, textkey)) === val);
@@ -742,6 +720,8 @@ export class Dropdown {
const indeterminate = selectedlist.some(s => this._contains(String(getValue(s, valuekey, textkey)), item, valuekey, textkey, childkey));
if (indeterminate) {
item.__checked = 2;
} else {
delete item.__checked;
}
}
}
@@ -871,15 +851,14 @@ export class Dropdown {
}
/**
* @param {DropdownItem[]} [source]
* @returns {DropdownItem[]}
*/
_getSelectedList(source) {
_getSelectedList() {
const valuekey = this._var.options.valueKey;
const textkey = this._var.options.textKey;
/** @type {DropdownItem[]} */
const list = [];
source ??= this.source;
const source = this._var.currentSource ?? this.source;
source.filter(it => it.__p == null).forEach(it => {
const key = String(getValue(it, valuekey, textkey));
if (it.__checked === 1) {
@@ -1013,12 +992,8 @@ export class Dropdown {
static resolve(dom = document.body, trigger) {
const selects = dom.querySelectorAll('select');
for (let sel of selects) {
const source = [...sel.children].map(
/**
* @param {HTMLOptionElement} it
* @returns {DropdownItem}
*/
it => ({ value: it.value, text: it.innerText }));
const source = /** @type {Array<HTMLOptionElement>} */ ([...sel.children]).map(
it => /** @type {DropdownItem} */({ value: it.value, text: it.innerText }));
const drop = new Dropdown({
selected: sel.value,
disabled: sel.disabled,