zidan-select 2.2.4 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * ZidanForm Select-only v2.2.4
2
+ * ZidanForm Select-only v2.3.1
3
3
  * Zero-dependency select / combobox / tags enhancement. Every other control family is omitted.
4
4
  * Pairs with plugin-select-core.css and plugin-select-theme.css.
5
5
  *
@@ -29,7 +29,7 @@
29
29
  })(typeof window !== 'undefined' ? window : this, function (global) {
30
30
  'use strict';
31
31
 
32
- var VERSION = '2.2.4';
32
+ var VERSION = '2.3.1';
33
33
 
34
34
  // Loading the file twice (classic script + module wrapper, or two bundles)
35
35
  // must not replace the classes and orphan the live instances.
@@ -93,6 +93,24 @@
93
93
  return Array.isArray(value) ? value.slice() : [value];
94
94
  }
95
95
 
96
+ function isThenable(value) {
97
+ return !!(value && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function');
98
+ }
99
+
100
+ /* true when the element renders right-to-left, following the nearest dir attribute */
101
+ function isRtl(element) {
102
+ if (!element) return false;
103
+ var host = element.closest ? element.closest('[dir]') : null;
104
+ var dir = host ? host.getAttribute('dir') : '';
105
+ if (!dir && doc && doc.documentElement) dir = doc.documentElement.getAttribute('dir') || '';
106
+ if (dir) return String(dir).toLowerCase() === 'rtl';
107
+ try {
108
+ return !!(global.getComputedStyle && global.getComputedStyle(element).direction === 'rtl');
109
+ } catch (err) {
110
+ return false;
111
+ }
112
+ }
113
+
96
114
  function isElement(value) {
97
115
  return !!(value && typeof value === 'object' && value.nodeType === 1);
98
116
  }
@@ -288,9 +306,9 @@
288
306
  }
289
307
 
290
308
  function registerIcon(name, markup) {
291
- if (!name || typeof markup !== 'string') return ZidanSelect;
292
- ICON_REGISTRY[String(name)] = markup;
293
- return ZidanSelect;
309
+ if (typeof ZidanSelect !== 'undefined' && (!name || typeof markup !== 'string')) return ZidanSelect;
310
+ if (name && typeof markup === 'string') ICON_REGISTRY[String(name)] = markup;
311
+ return typeof ZidanSelect !== 'undefined' ? ZidanSelect : ZidanForm;
294
312
  }
295
313
 
296
314
  /* ---------------- field options (JS options + data-* attributes) ---------------- */
@@ -348,6 +366,7 @@
348
366
  fileType: 'This file type is not allowed.',
349
367
  fileSize: 'This file is too large.',
350
368
  fileCount: 'Too many files.',
369
+ pending: 'Checking…',
351
370
  generic: 'Please check this field.'
352
371
  };
353
372
 
@@ -375,6 +394,20 @@
375
394
  return fillTemplate(map[key] || FIELD_MESSAGES[key] || map.generic, vars);
376
395
  }
377
396
 
397
+ var NATIVE_VALIDITY_FLAGS = ['valueMissing', 'typeMismatch', 'patternMismatch', 'rangeUnderflow',
398
+ 'rangeOverflow', 'stepMismatch', 'tooShort', 'tooLong', 'badInput', 'fileMissing'];
399
+
400
+ /* customError is the library mirroring its own message onto the input, so re-reading it
401
+ would turn a pending/custom check into a phantom native constraint failure */
402
+ function hasNativeValidityError(input) {
403
+ var v = input && input.validity;
404
+ if (!v) return false;
405
+ for (var i = 0; i < NATIVE_VALIDITY_FLAGS.length; i += 1) {
406
+ if (v[NATIVE_VALIDITY_FLAGS[i]]) return true;
407
+ }
408
+ return false;
409
+ }
410
+
378
411
  /* first non-empty value of any of `names`, walking up the tree */
379
412
  function inheritedAttr(node, names) {
380
413
  var current = node;
@@ -393,6 +426,24 @@
393
426
  return list.indexOf(candidate) === -1 ? fallback : candidate;
394
427
  }
395
428
 
429
+ /* rangeTicks: JS array, JSON array, comma list or a tick count */
430
+ function parseRangeTicks(value) {
431
+ if (Array.isArray(value)) return value.slice();
432
+ if (typeof value === 'string') {
433
+ var raw = value.trim();
434
+ if (raw.charAt(0) === '[') {
435
+ try {
436
+ var parsed = JSON.parse(raw);
437
+ if (Array.isArray(parsed)) return parsed;
438
+ } catch (err) { /* fall back to the tick count */ }
439
+ } else if (raw.indexOf(',') !== -1) {
440
+ return raw.split(',').map(function (part) { return Number(part.trim()); })
441
+ .filter(function (number) { return isFinite(number); });
442
+ }
443
+ }
444
+ return value;
445
+ }
446
+
396
447
  /* theme/framework/layout resolution shared by every field and the select */
397
448
  function resolveScope(node, options) {
398
449
  var opts = options || {};
@@ -459,6 +510,10 @@
459
510
  minChars: 0,
460
511
  allowFreeText: false,
461
512
  searchValues: true,
513
+ fetchPageSize: 0, // >0 enables paged fetchOptions({items, hasMore})
514
+ autoLoadMore: true,
515
+ cacheResults: true,
516
+ loadMoreText: 'Load more',
462
517
  /* tags */
463
518
  maxTags: Infinity,
464
519
  minTagLength: 1,
@@ -487,6 +542,7 @@
487
542
  onClose: null,
488
543
  onTagAdd: null,
489
544
  onTagRemove: null,
545
+ onLoadMore: null,
490
546
  onError: null
491
547
  };
492
548
 
@@ -526,6 +582,14 @@
526
582
  this.rendered = [];
527
583
  this.listeners = new Map();
528
584
  this.asyncController = null;
585
+ this.asyncItems = null;
586
+ this.asyncQuery = '';
587
+ this.asyncPage = 0;
588
+ this.hasMore = false;
589
+ this.fetchingMore = false;
590
+ this.loadMoreError = null;
591
+ this._fetchCache = Object.create(null);
592
+ this._cacheKeys = [];
529
593
  this.timer = null;
530
594
  this.savedInputText = '';
531
595
  this.disabled_ = !!this.opts.disabled;
@@ -602,6 +666,10 @@
602
666
  merged.validation = pick('validation', d.zidanValidation || null);
603
667
  merged.options = pick('options', null);
604
668
  merged.suggestions = pick('suggestions', null);
669
+ merged.fetchPageSize = pick('fetchPageSize', d.zidanFetchPageSize ? parseInt(d.zidanFetchPageSize, 10) || 0 : DEFAULTS.fetchPageSize);
670
+ merged.autoLoadMore = pick('autoLoadMore', readBoolAttr(d.zidanAutoLoadMore, DEFAULTS.autoLoadMore));
671
+ merged.cacheResults = pick('cacheResults', readBoolAttr(d.zidanCacheResults, DEFAULTS.cacheResults));
672
+ merged.loadMoreText = pick('loadMoreText', d.zidanLoadMoreText || DEFAULTS.loadMoreText);
605
673
 
606
674
  // carry over every explicitly-passed option (callbacks, custom classes, ...)
607
675
  Object.keys(options).forEach(function (key) {
@@ -748,6 +816,7 @@
748
816
  rootEl.setAttribute('data-zs-framework', this.framework);
749
817
  rootEl.setAttribute('data-zs-mode', this.mode);
750
818
  rootEl.setAttribute('data-zs-scrollbar', opts.scrollbar || 'default');
819
+ if (isRtl(this.el)) rootEl.setAttribute('dir', 'rtl');
751
820
  rootEl.classList.add('zs-root--' + this.mode);
752
821
  if (this.mode === 'tags') rootEl.classList.add('zs-root--multiple');
753
822
  if (opts.multiple && this.mode === 'select') rootEl.classList.add('zs-root--multiple');
@@ -776,6 +845,9 @@
776
845
  input.setAttribute('aria-controls', this.id + '-listbox');
777
846
  input.setAttribute('aria-autocomplete', (opts.searchable || this.mode !== 'select') ? 'list' : 'none');
778
847
  input.setAttribute('aria-haspopup', 'listbox');
848
+ /* a plain select is not type-to-filter: keep its text field read-only so
849
+ typing cannot filter or hide the options */
850
+ if (this.mode === 'select' && !opts.searchable) input.readOnly = true;
779
851
  if (opts.ariaLabel || opts.label) input.setAttribute('aria-label', opts.ariaLabel || opts.label);
780
852
  if (opts.disabled) input.disabled = true;
781
853
  this._applyCustomClass(input, 'input');
@@ -785,6 +857,16 @@
785
857
  labelEl = el('label', 'zs-label', { for: input.id });
786
858
  labelEl.textContent = opts.label;
787
859
  if (this.layout !== 'floating-label') rootEl.classList.add('zs-root--has-static-label');
860
+
861
+ // Hide the author's <label>: this label replaces it, and showing both
862
+ // would print the same text twice above the control.
863
+ var authorLabel = this.el.labels && this.el.labels[0] ? this.el.labels[0] : null;
864
+ if (authorLabel && authorLabel.closest && authorLabel.closest('.zs-root')) authorLabel = null;
865
+ this._authorLabel = authorLabel || null;
866
+ if (authorLabel) {
867
+ authorLabel.hidden = true;
868
+ authorLabel.setAttribute('data-zs-hidden-label', 'true');
869
+ }
788
870
  }
789
871
 
790
872
  var clearBtn = el('button', 'zs-clear', {
@@ -813,9 +895,7 @@
813
895
  var dropdown = el('div', 'zs-dropdown');
814
896
  dropdown.id = this.id + '-listbox';
815
897
  dropdown.setAttribute('role', 'listbox');
816
- if (this.mode !== 'tags') {
817
- dropdown.setAttribute('aria-multiselectable', String(!!(opts.multiple || this.mode === 'tags')));
818
- }
898
+ dropdown.setAttribute('aria-multiselectable', String(!!(opts.multiple || this.mode === 'tags')));
819
899
  this._applyCustomClass(dropdown, 'dropdown');
820
900
 
821
901
  var live = el('div', 'zs-live', { 'aria-live': 'polite', 'aria-atomic': 'true' });
@@ -893,6 +973,22 @@
893
973
  };
894
974
  this.form.addEventListener('reset', this.onFormReset);
895
975
  }
976
+
977
+ /* `required` is a JS-only option as well as a native attribute. A native
978
+ select can carry it directly; a mount-mode div needs a submit guard. */
979
+ if (opts.required && this.native && this.native.tagName === 'SELECT' && !this.native.required) {
980
+ this.native.required = true;
981
+ this._nativeRequiredAdded = true;
982
+ } else if (opts.required && !this.native && this.form) {
983
+ this.onFormSubmit = function (event) {
984
+ if (self.value_.length) return;
985
+ event.preventDefault();
986
+ self.setValidation('invalid');
987
+ if (self.input && self.input.focus) self.input.focus();
988
+ self.announce('Please select an option.');
989
+ };
990
+ this.form.addEventListener('submit', this.onFormSubmit);
991
+ }
896
992
  return this.syncEl;
897
993
  };
898
994
 
@@ -993,7 +1089,9 @@
993
1089
 
994
1090
  if (!this.opts.multiple && this.mode !== 'tags') next = next.slice(0, 1);
995
1091
  this.value_ = next;
996
- if (next.length && this.opts.validation === 'invalid') this.opts.validation = null;
1092
+ /* a preset/manual invalid state is only cleared by a real value change, not
1093
+ by the initial value applied during construction */
1094
+ if (next.length && this.opts.validation === 'invalid' && !cfg.silent) this.opts.validation = null;
997
1095
  this._render();
998
1096
  this._syncNative();
999
1097
  if (!cfg.silent) {
@@ -1183,8 +1281,11 @@
1183
1281
  var query = this.query.trim();
1184
1282
  var source = this._sourceItems();
1185
1283
  var isCaseSensitive = !!this.opts.caseSensitive;
1284
+ var remote = typeof this.opts.fetchOptions === 'function' && !!this.asyncItems;
1186
1285
 
1187
1286
  if (!query) return source.slice();
1287
+ // the server already filtered this page; only an explicit filterOption re-checks it
1288
+ if (remote && typeof this.opts.filterOption !== 'function') return source.slice();
1188
1289
 
1189
1290
  return source.filter(function (item) {
1190
1291
  if (typeof self.opts.filterOption === 'function') {
@@ -1282,6 +1383,20 @@
1282
1383
  self.dropdown.appendChild(option);
1283
1384
  });
1284
1385
  }
1386
+
1387
+ if (this.hasMore) {
1388
+ var more = el('button', 'zs-more', { type: 'button' });
1389
+ if (this.fetchingMore) {
1390
+ more.appendChild(el('span', 'zs-spinner', { 'aria-hidden': 'true' }));
1391
+ more.appendChild(el('span', null, null, this.opts.loadingText || 'Loading…'));
1392
+ more.disabled = true;
1393
+ more.setAttribute('aria-busy', 'true');
1394
+ } else {
1395
+ more.textContent = this.loadMoreError || this.opts.loadMoreText;
1396
+ if (this.loadMoreError) more.classList.add('zs-more--error');
1397
+ }
1398
+ this.dropdown.appendChild(more);
1399
+ }
1285
1400
  }
1286
1401
 
1287
1402
  this.dropdown.scrollTop = scrollTop;
@@ -1335,6 +1450,8 @@
1335
1450
  this.opened = true;
1336
1451
  this.query = '';
1337
1452
  this.activeIndex = -1;
1453
+ this.loadMoreError = null;
1454
+ this.fetchingMore = false;
1338
1455
  if (this.mode === 'combobox' || (this.mode === 'select' && this.opts.searchable)) {
1339
1456
  this.savedInputText = this.input.value;
1340
1457
  }
@@ -1346,7 +1463,7 @@
1346
1463
  this.emit('open', { instance: this });
1347
1464
  this.call('onOpen', { instance: this });
1348
1465
 
1349
- if (typeof this.opts.fetchOptions === 'function' && !this.asyncItems) {
1466
+ if (typeof this.opts.fetchOptions === 'function' && (!this.asyncItems || this.asyncQuery !== '')) {
1350
1467
  this._fetch('');
1351
1468
  } else if (this.mode !== 'tags') {
1352
1469
  var first = this._firstEnabledIndex();
@@ -1374,6 +1491,15 @@
1374
1491
  this.dropdown.removeAttribute('data-zs-visible');
1375
1492
  this.activeIndex = -1;
1376
1493
  this.query = '';
1494
+ if (typeof this.opts.fetchOptions === 'function') {
1495
+ if (this.asyncController) {
1496
+ this.asyncController.abort();
1497
+ this.asyncController = null;
1498
+ }
1499
+ this.loading = false;
1500
+ this.fetchingMore = false;
1501
+ this.loadMoreError = null;
1502
+ }
1377
1503
  this._render();
1378
1504
  this.emit('close', { instance: this });
1379
1505
  this.call('onClose', { instance: this });
@@ -1443,54 +1569,205 @@
1443
1569
 
1444
1570
  /* ---------------- async loading ---------------- */
1445
1571
 
1446
- ZidanSelect.prototype._fetch = function (query) {
1572
+ ZidanSelect.prototype._cacheKey = function (query, page) {
1573
+ return query + '\u0000' + page;
1574
+ };
1575
+
1576
+ ZidanSelect.prototype._cacheGet = function (query, page) {
1577
+ if (!this.opts.cacheResults) return null;
1578
+ var key = this._cacheKey(query, page);
1579
+ var entry = this._fetchCache[key];
1580
+ if (!entry) return null;
1581
+ var index = this._cacheKeys.indexOf(key);
1582
+ if (index !== -1) {
1583
+ this._cacheKeys.splice(index, 1);
1584
+ this._cacheKeys.push(key);
1585
+ }
1586
+ return entry;
1587
+ };
1588
+
1589
+ ZidanSelect.prototype._cacheSet = function (query, page, entry) {
1590
+ if (!this.opts.cacheResults) return;
1591
+ var key = this._cacheKey(query, page);
1592
+ this._fetchCache[key] = entry;
1593
+ var index = this._cacheKeys.indexOf(key);
1594
+ if (index !== -1) this._cacheKeys.splice(index, 1);
1595
+ this._cacheKeys.push(key);
1596
+ var limit = ZidanSelect.cacheLimit || 50;
1597
+ while (this._cacheKeys.length > limit) {
1598
+ delete this._fetchCache[this._cacheKeys.shift()];
1599
+ }
1600
+ };
1601
+
1602
+ ZidanSelect.prototype.clearCache = function () {
1603
+ this._fetchCache = Object.create(null);
1604
+ this._cacheKeys = [];
1605
+ return this;
1606
+ };
1607
+
1608
+ ZidanSelect.prototype._readFetchPayload = function (result, page, pageSize) {
1609
+ var rows = result;
1610
+ var hasMore = false;
1611
+ if (result && !Array.isArray(result) && typeof result === 'object') {
1612
+ rows = result.items || result.options || result.results || [];
1613
+ if (result.hasMore !== undefined) {
1614
+ hasMore = !!result.hasMore;
1615
+ } else if (result.total !== undefined && pageSize > 0) {
1616
+ hasMore = (page + 1) * pageSize < Number(result.total);
1617
+ } else if (pageSize > 0) {
1618
+ hasMore = toArray(rows).length >= pageSize;
1619
+ }
1620
+ } else if (pageSize > 0) {
1621
+ hasMore = toArray(rows).length >= pageSize;
1622
+ }
1623
+ return { items: this._normalizeItems(rows), hasMore: hasMore };
1624
+ };
1625
+
1626
+ ZidanSelect.prototype._applyCachedPage = function (query, page, append) {
1627
+ var cached = this._cacheGet(query, page);
1628
+ if (!cached) return false;
1629
+ this.loading = false;
1630
+ this.fetchingMore = false;
1631
+ this.loadMoreError = null;
1632
+ this.error = null;
1633
+ this.asyncQuery = query;
1634
+ this.asyncPage = page;
1635
+ this.asyncItems = append ? (this.asyncItems || []).concat(cached.items) : cached.items.slice();
1636
+ this.hasMore = !!cached.hasMore;
1637
+ if (!append) this.activeIndex = -1;
1638
+ this._renderDropdown();
1639
+ return true;
1640
+ };
1641
+
1642
+ /* page 0 replaces the result set; later pages append (remote pagination) */
1643
+ ZidanSelect.prototype._fetch = function (query, page, append) {
1447
1644
  var self = this;
1448
- if (typeof this.opts.fetchOptions !== 'function') return;
1645
+ if (typeof this.opts.fetchOptions !== 'function') return Promise.resolve([]);
1449
1646
  query = query === undefined || query === null ? '' : String(query);
1647
+ page = page === undefined || page === null ? 0 : parseInt(page, 10) || 0;
1648
+ append = !!append;
1649
+ if (append && (this.loading || this.fetchingMore || !this.hasMore)) return Promise.resolve([]);
1650
+
1651
+ var cached = this._cacheGet(query, page);
1652
+ if (cached && (append ? this.hasMore : true)) {
1653
+ if (this.asyncController) this.asyncController.abort();
1654
+ this.asyncController = null;
1655
+ this._applyCachedPage(query, page, append);
1656
+ return Promise.resolve(this._visibleItems());
1657
+ }
1450
1658
 
1451
1659
  if (this.asyncController) this.asyncController.abort();
1452
1660
  var controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
1453
1661
  this.asyncController = controller;
1454
- this.loading = true;
1662
+ this.loading = !append;
1663
+ this.fetchingMore = append;
1455
1664
  this.error = null;
1665
+ this.loadMoreError = null;
1666
+ if (!append) {
1667
+ this.hasMore = false;
1668
+ this.asyncPage = 0;
1669
+ this.announce(this.opts.loadingText);
1670
+ }
1456
1671
  this._renderDropdown();
1457
- this.announce(this.opts.loadingText);
1458
1672
 
1673
+ var settleFetch;
1674
+ var fetchPromise = new Promise(function (resolve) { settleFetch = resolve; });
1675
+ if (controller && controller.signal.addEventListener) {
1676
+ controller.signal.addEventListener('abort', function () { settleFetch([]); });
1677
+ }
1678
+
1679
+ if (append) {
1680
+ this.emit('load-more', { page: page, query: query, instance: this });
1681
+ this.call('onLoadMore', { page: page, query: query, instance: this });
1682
+ }
1683
+
1684
+ var pageSize = this.opts.fetchPageSize > 0 ? this.opts.fetchPageSize : 0;
1459
1685
  var done = function (result) {
1686
+ if (controller && controller.signal.aborted) return;
1460
1687
  self.loading = false;
1461
- self.asyncItems = self._normalizeItems(result || []);
1462
- self.activeIndex = -1;
1688
+ self.fetchingMore = false;
1689
+ self.loadMoreError = null;
1690
+ self.error = null;
1691
+ var payload = self._readFetchPayload(result, page, pageSize);
1692
+ self.asyncQuery = query;
1693
+ self.asyncPage = page;
1694
+ self.asyncItems = append ? (self.asyncItems || []).concat(payload.items) : payload.items;
1695
+ self.hasMore = payload.hasMore;
1696
+ self._cacheSet(query, page, { items: payload.items, hasMore: payload.hasMore });
1697
+ if (!append) self.activeIndex = -1;
1463
1698
  self._renderDropdown();
1464
1699
  var count = self._visibleItems().length;
1465
1700
  self.announce(count + ' result' + (count === 1 ? '' : 's'));
1701
+ settleFetch(self._visibleItems());
1466
1702
  };
1467
1703
  var fail = function (err) {
1704
+ if (err && err.name === 'AbortError') {
1705
+ settleFetch([]);
1706
+ return;
1707
+ }
1708
+ if (controller && controller.signal.aborted) {
1709
+ settleFetch([]);
1710
+ return;
1711
+ }
1712
+ var message = (err && err.message) || 'Unable to load options.';
1468
1713
  self.loading = false;
1469
- self.error = { message: (err && err.message) || 'Unable to load options.' };
1470
- self.emit('error', { error: err, message: self.error.message, instance: self });
1471
- self.call('onError', { error: err, message: self.error.message, instance: self });
1714
+ self.fetchingMore = false;
1715
+ if (append) self.loadMoreError = message;
1716
+ else self.error = { message: message };
1717
+ self.emit('error', { error: err, message: message, instance: self });
1718
+ self.call('onError', { error: err, message: message, instance: self });
1472
1719
  self._renderDropdown();
1720
+ settleFetch([]);
1473
1721
  };
1474
1722
 
1475
1723
  try {
1476
- var result = this.opts.fetchOptions(query, controller ? controller.signal : undefined);
1724
+ var result = this.opts.fetchOptions(query, controller ? controller.signal : undefined, {
1725
+ page: page,
1726
+ pageSize: pageSize,
1727
+ append: append,
1728
+ instance: this
1729
+ });
1477
1730
  if (result && typeof result.then === 'function') {
1478
- result.then(function (data) {
1479
- if (controller && controller.signal.aborted) return;
1480
- done(data);
1481
- }).catch(function (err) {
1482
- if (err && err.name === 'AbortError') return;
1483
- if (controller && controller.signal.aborted) return;
1484
- fail(err);
1485
- });
1731
+ result.then(done).catch(fail);
1486
1732
  } else {
1487
1733
  done(result);
1488
1734
  }
1489
1735
  } catch (err) {
1490
- if (err && err.name === 'AbortError') return;
1491
1736
  fail(err);
1492
1737
  }
1493
- void self;
1738
+ return fetchPromise;
1739
+ };
1740
+
1741
+ ZidanSelect.prototype.loadMore = function () {
1742
+ if (typeof this.opts.fetchOptions !== 'function') return this;
1743
+ if (this.loading || this.fetchingMore || !this.hasMore) return this;
1744
+ return this._fetch(this.asyncQuery || this.query || '', this.asyncPage + 1, true);
1745
+ };
1746
+
1747
+ ZidanSelect.prototype.hasMoreResults = function () {
1748
+ return !!this.hasMore;
1749
+ };
1750
+
1751
+ ZidanSelect.prototype.getAsyncState = function () {
1752
+ return {
1753
+ query: this.asyncQuery,
1754
+ page: this.asyncPage,
1755
+ hasMore: !!this.hasMore,
1756
+ loading: !!this.loading,
1757
+ fetchingMore: !!this.fetchingMore,
1758
+ error: this.error ? this.error.message : null
1759
+ };
1760
+ };
1761
+
1762
+ ZidanSelect.prototype.refreshOptions = function () {
1763
+ this.clearCache();
1764
+ if (typeof this.opts.fetchOptions !== 'function') return this;
1765
+ this.asyncItems = null;
1766
+ this.asyncQuery = '';
1767
+ this.asyncPage = 0;
1768
+ this.hasMore = false;
1769
+ if (this.opened) return this._fetch(this.query, 0, false);
1770
+ return this;
1494
1771
  };
1495
1772
 
1496
1773
  /* ---------------- tags ---------------- */
@@ -1527,6 +1804,22 @@
1527
1804
  return false;
1528
1805
  }
1529
1806
 
1807
+ /* allowFreeText: false limits tags to the supplied option/suggestion list */
1808
+ if (this.mode === 'tags' && !opts.allowFreeText) {
1809
+ var pool = this.suggestions && this.suggestions.length ? this.suggestions : this.items;
1810
+ var needle = opts.caseSensitive ? raw : raw.toLowerCase();
1811
+ var listed = false;
1812
+ for (var s = 0; s < pool.length; s += 1) {
1813
+ var itemValue = opts.caseSensitive ? String(pool[s].value) : String(pool[s].value).toLowerCase();
1814
+ var itemLabel = opts.caseSensitive ? String(pool[s].label) : String(pool[s].label).toLowerCase();
1815
+ if (itemValue === needle || itemLabel === needle) { listed = true; break; }
1816
+ }
1817
+ if (!listed) {
1818
+ if (!cfg.silent) this._tagError('"' + raw + '" is not in the list.');
1819
+ return false;
1820
+ }
1821
+ }
1822
+
1530
1823
  var formatted = raw;
1531
1824
  if (typeof opts.formatTag === 'function') {
1532
1825
  var formattedResult = opts.formatTag(raw, { instance: this, tags: this.getTags() });
@@ -1603,6 +1896,14 @@
1603
1896
  var key = event.key;
1604
1897
  var items = this._visibleItems();
1605
1898
 
1899
+ /* Space opens a closed combobox/listbox (WAI-ARIA), unless the user is
1900
+ already typing a search or tag query in an open control. */
1901
+ if ((key === ' ' || key === 'Spacebar') && !this.opened) {
1902
+ event.preventDefault();
1903
+ this.open();
1904
+ return;
1905
+ }
1906
+
1606
1907
  if (key === 'ArrowDown' || key === 'ArrowUp') {
1607
1908
  event.preventDefault();
1608
1909
  if (!this.opened) {
@@ -1738,6 +2039,8 @@
1738
2039
 
1739
2040
  this.onInput = function () {
1740
2041
  var value = self.input.value;
2042
+ /* plain selects are not searchable: typing must not filter the list */
2043
+ if (self.mode === 'select' && !self.opts.searchable) return;
1741
2044
  self.query = value;
1742
2045
  if (self.opened) {
1743
2046
  if (typeof self.opts.fetchOptions === 'function' && self.query.length >= (self.opts.minChars || 0)) {
@@ -1847,6 +2150,13 @@
1847
2150
  };
1848
2151
 
1849
2152
  this.onDropdownClick = function (event) {
2153
+ var more = event.target.closest ? event.target.closest('.zs-more') : null;
2154
+ if (more) {
2155
+ event.preventDefault();
2156
+ event.stopPropagation();
2157
+ self.loadMore();
2158
+ return;
2159
+ }
1850
2160
  var option = event.target.closest ? event.target.closest('[data-zs-option]') : null;
1851
2161
  if (!option) return;
1852
2162
  event.preventDefault();
@@ -1870,6 +2180,13 @@
1870
2180
  if (self.opened) self._position();
1871
2181
  };
1872
2182
 
2183
+ this.onDropdownScroll = function () {
2184
+ if (!self.opts.autoLoadMore || !self.opened || !self.hasMore) return;
2185
+ if (self.loading || self.fetchingMore) return;
2186
+ var node = self.dropdown;
2187
+ if (node.scrollTop + node.clientHeight >= node.scrollHeight - 24) self.loadMore();
2188
+ };
2189
+
1873
2190
  this.debouncedFetch = debounce(function (query) { self._fetch(query); }, this.opts.debounce);
1874
2191
 
1875
2192
  if (this.el && this.el.addEventListener) {
@@ -1890,6 +2207,7 @@
1890
2207
  this.clearBtn.addEventListener('click', this.onClearClick);
1891
2208
  this.dropdown.addEventListener('mousedown', this.onDropdownMouseDown);
1892
2209
  this.dropdown.addEventListener('click', this.onDropdownClick);
2210
+ this.dropdown.addEventListener('scroll', this.onDropdownScroll);
1893
2211
  doc.addEventListener('pointerdown', this.onDocumentPointerDown, true);
1894
2212
  global.addEventListener('resize', this.onWindowResize);
1895
2213
  };
@@ -1977,6 +2295,10 @@
1977
2295
  }
1978
2296
  }
1979
2297
  this.asyncItems = null;
2298
+ this.asyncPage = 0;
2299
+ this.hasMore = false;
2300
+ this.loadMoreError = null;
2301
+ this.clearCache();
1980
2302
  this.activeIndex = -1;
1981
2303
  this._render();
1982
2304
  this._syncNative();
@@ -2109,8 +2431,14 @@
2109
2431
 
2110
2432
  if (this.debouncedFetch) this.debouncedFetch.cancel();
2111
2433
  if (this.asyncController) this.asyncController.abort();
2434
+ this.clearCache();
2112
2435
  if (this.resizeObserver) this.resizeObserver.disconnect();
2113
2436
  if (this.form && this.onFormReset) this.form.removeEventListener('reset', this.onFormReset);
2437
+ if (this.form && this.onFormSubmit) this.form.removeEventListener('submit', this.onFormSubmit);
2438
+ if (this._nativeRequiredAdded && this.native) {
2439
+ this.native.required = false;
2440
+ this._nativeRequiredAdded = false;
2441
+ }
2114
2442
 
2115
2443
  doc.removeEventListener('pointerdown', this.onDocumentPointerDown, true);
2116
2444
  global.removeEventListener('resize', this.onWindowResize);
@@ -2120,6 +2448,12 @@
2120
2448
 
2121
2449
  if (this.root && this.root.parentNode) this.root.parentNode.removeChild(this.root);
2122
2450
 
2451
+ if (this._authorLabel) {
2452
+ this._authorLabel.hidden = false;
2453
+ this._authorLabel.removeAttribute('data-zs-hidden-label');
2454
+ this._authorLabel = null;
2455
+ }
2456
+
2123
2457
  if (this.native) {
2124
2458
  this.native.hidden = false;
2125
2459
  this.native.classList.remove('zs-native');
@@ -2207,7 +2541,11 @@
2207
2541
  // (output, counter, clearable, …) work without a data attribute
2208
2542
  var kind = fieldKindFor(node) || inferredTextKind(node);
2209
2543
  if (!kind) return;
2210
- if (kind === 'radio' && !node.hasAttribute('data-zidan-radio')) {
2544
+ if (kind === 'radio' || kind === 'radio-group') {
2545
+ if (/^INPUT$/i.test(node.tagName)) {
2546
+ node = node.closest ? node.closest('fieldset, [data-zidan-radio-group]') : null;
2547
+ if (!node) return;
2548
+ }
2211
2549
  kind = 'radio-group';
2212
2550
  }
2213
2551
  var instance = createField(node, kind, options || {});
@@ -2222,7 +2560,13 @@
2222
2560
  if (!isElement(node) || node.__zidanField) return;
2223
2561
  var kind = fieldKindFor(node);
2224
2562
  if (!kind) return;
2225
- if (kind === 'radio') kind = 'radio-group';
2563
+ if (kind === 'radio' || kind === 'radio-group') {
2564
+ if (/^INPUT$/i.test(node.tagName)) {
2565
+ node = node.closest ? node.closest('fieldset, [data-zidan-radio-group]') : null;
2566
+ if (!node) return;
2567
+ }
2568
+ kind = 'radio-group';
2569
+ }
2226
2570
  var instance = createField(node, kind, options || {});
2227
2571
  if (instance) instances.push(instance);
2228
2572
  });
@@ -2327,6 +2671,7 @@
2327
2671
  ZidanSelect.frameworks = FRAMEWORKS.slice();
2328
2672
  ZidanSelect.scrollbars = SCROLLBARS.slice();
2329
2673
  ZidanSelect.defaults = DEFAULTS;
2674
+ ZidanSelect.cacheLimit = 50;
2330
2675
  ZidanSelect.get = function (target) {
2331
2676
  var element = typeof target === 'string' ? doc.querySelector(target) : target;
2332
2677
  if (!isElement(element)) return null;