add jsdoc comments

This commit is contained in:
lchen committed 2026-11-18 10:14:21 +08:00
1 parent 6de354ad47
commit 8c1205bc13
24 files changed
+1228 -425

No files matched your search

+47 -15
View File
@@ -1,8 +1,20 @@
import './css/checkbox.scss';
import { createElement } from "../functions";
import { createIcon } from "./icon";
import { isPositive, nullOrEmpty } from '../utility';
function fillCheckbox(container, type = 'fa-regular', label, tabindex = -1, charactor = 'check', title) {
/** @typedef {import('../types').CheckboxOptions} CheckboxOptions */
/**
* @private
* @param {HTMLElement} container
* @param {string} [type="fa-regular"]
* @param {string | HTMLElement} [label]
* @param {number} [tabindex]
* @param {string} [title]
* @param {string} [charactor]
*/
function fillCheckbox(container, type = 'fa-regular', label, tabindex = -1, title, charactor = 'check') {
const checkIcon = createIcon(type, charactor);
checkIcon.classList.add('ui-check-icon');
const indeterminateIcon = createIcon(type, 'minus');
@@ -24,7 +36,7 @@ function fillCheckbox(container, type = 'fa-regular', label, tabindex = -1, char
}
}, checkIcon, indeterminateIcon)
);
if (label instanceof Element) {
if (label instanceof HTMLElement) {
container.appendChild(label);
} else if (label != null && String(label).length > 0) {
container.appendChild(
@@ -38,11 +50,18 @@ function fillCheckbox(container, type = 'fa-regular', label, tabindex = -1, char
}
}
/**
* 创建一个单选框
* @param {CheckboxOptions} opts
* @returns {HTMLLabelElement}
*/
export function createRadiobox(opts = {}) {
const container = createElement('label', 'ui-check-wrapper ui-radio-wrapper',
createElement('input', input => {
input.setAttribute('type', 'radio');
input.name = opts.name;
if (!nullOrEmpty(opts.name)) {
input.name = opts.name;
}
if (opts.checked === true) {
input.checked = true;
}
@@ -61,10 +80,15 @@ export function createRadiobox(opts = {}) {
if (opts.className) {
container.classList.add(opts.className);
}
fillCheckbox(container, opts.type, opts.label, opts.tabIndex, 'circle', opts.title);
fillCheckbox(container, opts.type, opts.label, opts.tabIndex, opts.title, 'circle');
return container;
}
/**
* 创建一个复选框
* @param {CheckboxOptions} opts
* @returns {HTMLLabelElement}
*/
export function createCheckbox(opts = {}) {
const container = createElement('label', opts.switch ? 'ui-switch' : 'ui-check-wrapper',
createElement('input', input => {
@@ -112,7 +136,7 @@ export function createCheckbox(opts = {}) {
} else if (opts.checkedNode != null && opts.uncheckedNode != null) {
container.classList.add('ui-check-image-wrapper');
let height = opts.imageHeight;
if (isNaN(height) || height <= 0) {
if (!isPositive(height)) {
height = 14;
}
opts.checkedNode.classList.add('checked');
@@ -120,16 +144,22 @@ export function createCheckbox(opts = {}) {
opts.uncheckedNode.classList.add('unchecked');
container.appendChild(opts.uncheckedNode);
} else {
fillCheckbox(container, opts.type, opts.label, opts.tabIndex, undefined, opts.title);
fillCheckbox(container, opts.type, opts.label, opts.tabIndex, opts.title);
}
return container;
}
/**
* 解析容器元素内符合条件的子元素为复选框元素
* @param {HTMLElement} container
* @param {boolean} [legacy]
* @returns {HTMLElement}
*/
export function resolveCheckbox(container = document.body, legacy) {
if (legacy) {
const checks = container.querySelectorAll('input[type="checkbox"]');
for (let chk of checks) {
if (chk.parentElement.classList.contains('ui-check-wrapper')) {
if (!(chk instanceof HTMLInputElement) || chk.parentElement?.classList.contains('ui-check-wrapper')) {
// skip
continue;
}
@@ -140,7 +170,7 @@ export function resolveCheckbox(container = document.body, legacy) {
}
if (label == null) {
const e = chk.nextElementSibling;
if (e != null) {
if (e instanceof HTMLElement) {
if (e.tagName === 'LABEL') {
label = e;
} else if (e.tagName === 'SPAN' && e.dataset.lgid != null) {
@@ -151,7 +181,7 @@ export function resolveCheckbox(container = document.body, legacy) {
}
if (label == null) {
const e = chk.previousElementSibling;
if (e != null) {
if (e instanceof HTMLElement) {
if (e.tagName === 'LABEL') {
label = e;
} else if (text == null && e.tagName === 'SPAN' && e.dataset.lgid != null) {
@@ -160,9 +190,9 @@ export function resolveCheckbox(container = document.body, legacy) {
}
}
}
if (label == null) {
if (!(label instanceof HTMLElement)) {
label = createElement('label');
chk.parentElement.insertBefore(label, chk);
chk.parentElement?.insertBefore(label, chk);
} else {
text = label.innerText;
}
@@ -172,12 +202,15 @@ export function resolveCheckbox(container = document.body, legacy) {
label.className = 'ui-check-wrapper';
}
label.replaceChildren();
fillCheckbox(label, 'fa-regular', text, chk.tabIndex, undefined, label.title);
fillCheckbox(label, 'fa-regular', text, chk.tabIndex, label.title);
label.insertBefore(chk, label.firstChild);
}
}
const boxes = container.querySelectorAll('label[data-checkbox]');
for (let box of boxes) {
if (!(box instanceof HTMLLabelElement)) {
continue;
}
if (!box.classList.contains('ui-check-wrapper')) {
box.classList.add('ui-check-wrapper');
}
@@ -189,8 +222,7 @@ export function resolveCheckbox(container = document.body, legacy) {
fillCheckbox(box,
box.dataset.type,
box.dataset.label,
box.dataset.tabIndex,
undefined,
Number(box.dataset.tabIndex),
box.title);
box.removeAttribute('title');
box.removeAttribute('data-type');
@@ -198,7 +230,7 @@ export function resolveCheckbox(container = document.body, legacy) {
}
const input = createElement('input');
const id = box.dataset.id;
if (id?.length > 0) {
if (!nullOrEmpty(id)) {
input.id = id;
}
if (box.dataset.checked != null) {