ui: keep policy controls close to their labels

This commit is contained in:
AttilaNet
2026-11-13 08:08:12 +08:00
parent 1b1fa96984
commit 2cb50839b5
16 changed files with 444 additions and 58 deletions
+18 -12
View File
@@ -55,10 +55,13 @@ export function control(field, {get, set, onError = () => {}}) {
const items = optionsList(field.options).map(option => {
const check = el('input', {type: 'checkbox', 'aria-label': option.label});
check.addEventListener('change', () => commit(items.filter(item => item.check.checked).map(item => item.option.value)));
root.append(el('label', {}, check, option.label));
root.append(el('label', {class: 'multi-select-option'}, check, el('span', {class: 'field-text', text: option.label})));
return {check, option};
});
sync = () => items.forEach(({check, option}) => { check.checked = (value() || []).includes(option.value); });
sync = () => items.forEach(({check, option}) => {
check.checked = (value() || []).includes(option.value);
check.disabled = Boolean(field.disabled || field.readonly || option.disabled);
});
} else if (['id_counts', 'counts', 'reserves'].includes(type)) {
root = el('div', {class: 'counts-editor', role: 'group', 'aria-label': label});
const lines = el('div', {class: 'counts-lines'});
@@ -152,19 +155,22 @@ export class PolicyForm {
groups.set(group, grid);
}
const entry = control(field, this.context);
entry.root.classList.add('field-input');
const boolean = ['boolean', 'bool', 'checkbox'].includes(field.type);
const wide = ['id_counts', 'counts', 'reserves', 'multiselect'].includes(field.type);
const row = el('label', {class: `field${boolean ? ' checkbox-field' : ''}${wide ? ' field-wide' : ''}`});
if (entry.root.tagName === 'DIV') {
const fieldRow = el('div', {class: `field${wide ? ' field-wide' : ''}`}, el('span', {class: 'field-label', text: field.label}), entry.root);
groups.get(group).append(fieldRow);
entry.row = fieldRow;
const multi = ['multiselect', 'multi_select'].includes(field.type);
const wide = multi || ['id_counts', 'counts', 'reserves'].includes(field.type);
const label = field.label || field.path.at(-1) || '设置';
entry.root.classList.add(boolean ? 'field-checkbox' : 'field-input');
let row;
if (boolean) {
row = el('div', {class: 'field field-boolean checkbox-field'},
el('label', {class: 'field-label'}, entry.root, el('span', {class: 'field-text', text: label})));
} else {
row.append(el('span', {class: 'field-label', text: field.label}), entry.root);
groups.get(group).append(row);
entry.row = row;
row = el(entry.root.tagName === 'DIV' ? 'div' : 'label',
{class: `field field-stacked${multi ? ' field-multiselect' : ''}${wide ? ' field-wide' : ''}`},
el('span', {class: 'field-label', text: label}), entry.root);
}
groups.get(group).append(row);
entry.row = row;
this.controls.push(entry);
});
this.signature = signature;