ui: allow multi-select

This commit is contained in:
undefined committed 2021-04-12 23:15:25 +08:00
1 parent a2edf18737
commit f21d1b0327
3 files changed
+21 -22

No files matched your search

+8 -7
View File
@@ -50,18 +50,19 @@ module.exports = {
rules: { rules: {
// FIXME A bug with eslint-parser // FIXME A bug with eslint-parser
'template-curly-spacing': 'off', 'template-curly-spacing': 'off',
indent: 'off',
'comma-dangle': [ 'comma-dangle': [
'error', 'error',
'always-multiline', 'always-multiline',
], ],
indent: [ // indent: [
'error', // 'error',
2, // 2,
{ // {
SwitchCase: 0, // SwitchCase: 0,
}, // },
], // ],
'max-len': ['error', 150], 'max-len': ['error', 150],
quotes: 'warn', quotes: 'warn',
'class-methods-use-this': 'off', 'class-methods-use-this': 'off',
@@ -22,6 +22,7 @@ export default class AutoComplete extends DOMAttachedObject {
clearDefaultValue: true, clearDefaultValue: true,
position: 'bottom left', position: 'bottom left',
classes: '', classes: '',
multi: false,
...options, ...options,
}; };
this.clear(this.options.clearDefaultValue); this.clear(this.options.clearDefaultValue);
@@ -46,9 +47,7 @@ export default class AutoComplete extends DOMAttachedObject {
} }
clear(clearValue = true) { clear(clearValue = true) {
if (clearValue) { if (clearValue) this.$dom.val('');
this.$dom.val('');
}
this._value = null; this._value = null;
this.lastText = null; this.lastText = null;
} }
@@ -80,19 +79,14 @@ export default class AutoComplete extends DOMAttachedObject {
} }
onKeyUp(ev) { onKeyUp(ev) {
if (ev.which === 27) { if (ev.which === 27 /* ESC */) {
// ESC
this.close(); this.close();
return; return;
} }
if (this.$dom.val() === this.lastText) { if (this.$dom.val() === this.lastText) return;
return;
}
this.lastText = this.$dom.val(); this.lastText = this.$dom.val();
this.updateOpenState(); this.updateOpenState();
if (this.isOpen) { if (this.isOpen) this.renderList();
this.renderList();
}
} }
onMenuClick(ev) { onMenuClick(ev) {
@@ -109,7 +103,8 @@ export default class AutoComplete extends DOMAttachedObject {
this._value = item; this._value = item;
this.$dom.trigger('vjAutoCompleteSelect', item); this.$dom.trigger('vjAutoCompleteSelect', item);
if (text != null) { if (text != null) {
this.$dom.val(text); if (!this.options.multi) this.$dom.val(text);
else this.$dom.val(`${this.$dom.val() + text}, `);
} }
this.close(); this.close();
} }
@@ -141,7 +136,11 @@ export default class AutoComplete extends DOMAttachedObject {
} }
async renderList() { async renderList() {
const val = this.$dom.val(); let val = this.$dom.val();
if (this.options.multi) {
const elems = val.split(',').map((i) => i.trim());
val = elems[elems.length - 1];
}
const items = await this.getItems(val); const items = await this.getItems(val);
const html = this.getHtml(items); const html = this.getHtml(items);
this.currentItems = items; this.currentItems = items;
@@ -2,8 +2,7 @@ import { NamedPage } from 'vj/misc/Page';
import ProblemSelectAutoComplete from 'vj/components/autocomplete/ProblemSelectAutoComplete'; import ProblemSelectAutoComplete from 'vj/components/autocomplete/ProblemSelectAutoComplete';
const page = new NamedPage(['contest_edit', 'contest_create'], () => { const page = new NamedPage(['contest_edit', 'contest_create'], () => {
console.log($('[name="pids"]')); ProblemSelectAutoComplete.getOrConstruct($('[name="pids"]'), { multi: true });
ProblemSelectAutoComplete.getOrConstruct($('[name="pids"]'));
}); });
export default page; export default page;