slim-select 2.3.0 → 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.
@@ -6,6 +6,7 @@ export default class Select {
6
6
 
7
7
  // Mutation observer fields
8
8
  public onValueChange?: (value: string[]) => void
9
+ public onClassChange?: (classes: string[]) => void
9
10
  public onDisabledChange?: (disabled: boolean) => void
10
11
  public onOptionsChange?: (data: DataArrayPartial) => void
11
12
 
@@ -88,6 +89,7 @@ export default class Select {
88
89
  return
89
90
  }
90
91
 
92
+ let classChanged = false
91
93
  let disabledChanged = false
92
94
  let optgroupOptionChanged = false
93
95
 
@@ -99,6 +101,11 @@ export default class Select {
99
101
  if (m.attributeName === 'disabled') {
100
102
  disabledChanged = true
101
103
  }
104
+
105
+ // Check if class has changed
106
+ if (m.attributeName === 'class') {
107
+ classChanged = true
108
+ }
102
109
  }
103
110
 
104
111
  // Check if its an optgroup or option
@@ -107,6 +114,11 @@ export default class Select {
107
114
  }
108
115
  }
109
116
 
117
+ // If class has changed then call the class change function
118
+ if (classChanged && this.onClassChange) {
119
+ this.onClassChange(this.select.className.split(' '))
120
+ }
121
+
110
122
  // If disabled has changed then call the disabled change function
111
123
  if (disabledChanged && this.onDisabledChange) {
112
124
  this.changeListen(false)
@@ -241,9 +253,9 @@ export default class Select {
241
253
  // Stop listening to changes
242
254
  this.changeListen(false)
243
255
 
244
- // Update id
256
+ // Update id, only if the id isnt already set
245
257
  if (id) {
246
- this.select.id = id
258
+ this.select.dataset.id = id
247
259
  }
248
260
 
249
261
  // Update style
@@ -310,7 +322,7 @@ export default class Select {
310
322
  public createOption(info: Option): HTMLOptionElement {
311
323
  const optionEl = document.createElement('option')
312
324
  optionEl.id = info.id
313
- optionEl.value = info.value // !== '' ? info.value : info.text
325
+ optionEl.value = info.value
314
326
  optionEl.innerHTML = info.text
315
327
  if (info.html !== '') {
316
328
  optionEl.setAttribute('data-html', info.html)
@@ -356,7 +368,10 @@ export default class Select {
356
368
  this.observer = null
357
369
  }
358
370
 
359
- // show the original select
371
+ // Remove dataset id from original select
372
+ delete this.select.dataset.id
373
+
374
+ // Show the original select
360
375
  this.showUI()
361
376
  }
362
377
  }
@@ -33,7 +33,7 @@ export default class SlimSelect {
33
33
  getData(): DataArray;
34
34
  setData(data: DataArrayPartial): void;
35
35
  getSelected(): string[];
36
- setSelected(value: string | string[]): void;
36
+ setSelected(value: string | string[], runAfterChange?: boolean): void;
37
37
  addOption(option: OptionOptional): void;
38
38
  open(): void;
39
39
  close(): void;
@@ -4,7 +4,7 @@ export interface Callbacks {
4
4
  open: () => void;
5
5
  close: () => void;
6
6
  addable?: (value: string) => Promise<OptionOptional | string> | OptionOptional | string;
7
- setSelected: (value: string[]) => void;
7
+ setSelected: (value: string[], runAfterChange: boolean) => void;
8
8
  addOption: (option: Option) => void;
9
9
  search: (search: string) => void;
10
10
  beforeChange?: (newVal: Option[], oldVal: Option[]) => boolean | void;
@@ -90,6 +90,7 @@ export default class Render {
90
90
  disable(): void;
91
91
  open(): void;
92
92
  close(): void;
93
+ updateClassStyles(): void;
93
94
  mainDiv(): Main;
94
95
  mainFocus(trigger: boolean): void;
95
96
  placeholder(): HTMLDivElement;
@@ -2,6 +2,7 @@ import { DataArray, DataArrayPartial, Optgroup, OptgroupOptional, Option } from
2
2
  export default class Select {
3
3
  select: HTMLSelectElement;
4
4
  onValueChange?: (value: string[]) => void;
5
+ onClassChange?: (classes: string[]) => void;
5
6
  onDisabledChange?: (disabled: boolean) => void;
6
7
  onOptionsChange?: (data: DataArrayPartial) => void;
7
8
  listen: boolean;
@@ -345,6 +345,7 @@ class Render {
345
345
  this.callbacks = callbacks;
346
346
  this.main = this.mainDiv();
347
347
  this.content = this.contentDiv();
348
+ this.updateClassStyles();
348
349
  this.settings.contentLocation.appendChild(this.content.main);
349
350
  }
350
351
  enable() {
@@ -359,8 +360,6 @@ class Render {
359
360
  this.main.arrow.path.setAttribute('d', this.classes.arrowOpen);
360
361
  this.main.main.classList.add(this.settings.openPosition === 'up' ? this.classes.openAbove : this.classes.openBelow);
361
362
  this.moveContent();
362
- this.renderOptions(this.store.getData());
363
- this.moveContent();
364
363
  const selectedOptions = this.store.getSelectedOptions();
365
364
  if (selectedOptions.length) {
366
365
  const selectedId = selectedOptions[selectedOptions.length - 1].id;
@@ -377,20 +376,32 @@ class Render {
377
376
  this.content.main.classList.remove(this.classes.openBelow);
378
377
  this.main.arrow.path.setAttribute('d', this.classes.arrowClose);
379
378
  }
380
- mainDiv() {
381
- const main = document.createElement('div');
382
- main.tabIndex = 0;
383
- main.style.cssText = this.settings.style !== '' ? this.settings.style : '';
384
- main.className = '';
385
- main.classList.add(this.settings.id);
386
- main.classList.add(this.classes.main);
387
- if (this.settings.class) {
379
+ updateClassStyles() {
380
+ this.main.main.className = '';
381
+ this.main.main.removeAttribute('style');
382
+ this.content.main.className = '';
383
+ this.content.main.removeAttribute('style');
384
+ this.main.main.classList.add(this.classes.main);
385
+ this.content.main.classList.add(this.classes.content);
386
+ if (this.settings.style !== '') {
387
+ this.content.main.style.cssText = this.settings.style;
388
+ }
389
+ if (this.settings.class.length) {
388
390
  for (const c of this.settings.class) {
389
391
  if (c.trim() !== '') {
390
- main.classList.add(c.trim());
392
+ this.main.main.classList.add(c.trim());
393
+ this.content.main.classList.add(c.trim());
391
394
  }
392
395
  }
393
396
  }
397
+ if (this.settings.contentPosition === 'relative') {
398
+ this.content.main.classList.add('ss-' + this.settings.contentPosition);
399
+ }
400
+ }
401
+ mainDiv() {
402
+ const main = document.createElement('div');
403
+ main.dataset.id = this.settings.id;
404
+ main.tabIndex = 0;
394
405
  main.onfocus = () => {
395
406
  if (this.settings.triggerFocus) {
396
407
  this.callbacks.open();
@@ -443,7 +454,7 @@ class Render {
443
454
  shouldDelete = this.callbacks.beforeChange(after, before) === true;
444
455
  }
445
456
  if (shouldDelete) {
446
- this.callbacks.setSelected(['']);
457
+ this.callbacks.setSelected([''], false);
447
458
  if (this.settings.closeOnSelect) {
448
459
  this.callbacks.close();
449
460
  }
@@ -642,7 +653,7 @@ class Render {
642
653
  selectedValues.push(o.value);
643
654
  }
644
655
  }
645
- this.callbacks.setSelected(selectedValues);
656
+ this.callbacks.setSelected(selectedValues, false);
646
657
  if (this.settings.closeOnSelect) {
647
658
  this.callbacks.close();
648
659
  }
@@ -663,21 +674,7 @@ class Render {
663
674
  }
664
675
  contentDiv() {
665
676
  const main = document.createElement('div');
666
- main.classList.add(this.classes.content);
667
677
  main.dataset.id = this.settings.id;
668
- if (this.settings.style !== '') {
669
- main.style.cssText = this.settings.style;
670
- }
671
- if (this.settings.contentPosition === 'relative') {
672
- main.classList.add('ss-' + this.settings.contentPosition);
673
- }
674
- if (this.settings.class.length) {
675
- for (const c of this.settings.class) {
676
- if (c.trim() !== '') {
677
- main.classList.add(c.trim());
678
- }
679
- }
680
- }
681
678
  const search = this.searchDiv();
682
679
  main.appendChild(search.main);
683
680
  const list = this.listDiv();
@@ -789,10 +786,10 @@ class Render {
789
786
  if (this.settings.isMultiple) {
790
787
  let values = this.store.getSelected();
791
788
  values.push(newOption.value);
792
- this.callbacks.setSelected(values);
789
+ this.callbacks.setSelected(values, true);
793
790
  }
794
791
  else {
795
- this.callbacks.setSelected([newOption.value]);
792
+ this.callbacks.setSelected([newOption.value], true);
796
793
  }
797
794
  this.callbacks.search('');
798
795
  if (this.settings.closeOnSelect) {
@@ -974,12 +971,12 @@ class Render {
974
971
  }
975
972
  return true;
976
973
  });
977
- this.callbacks.setSelected(newSelected);
974
+ this.callbacks.setSelected(newSelected, true);
978
975
  return;
979
976
  }
980
977
  else {
981
978
  const newSelected = currentSelected.concat(d.options.map((o) => o.value));
982
- this.callbacks.setSelected(newSelected);
979
+ this.callbacks.setSelected(newSelected, true);
983
980
  }
984
981
  });
985
982
  optgroupActions.appendChild(selectAll);
@@ -1063,6 +1060,9 @@ class Render {
1063
1060
  if (this.settings.showOptionTooltips && optionEl.textContent) {
1064
1061
  optionEl.setAttribute('title', optionEl.textContent);
1065
1062
  }
1063
+ if (!option.display) {
1064
+ optionEl.classList.add(this.classes.hide);
1065
+ }
1066
1066
  if (option.disabled) {
1067
1067
  optionEl.classList.add(this.classes.disabled);
1068
1068
  }
@@ -1122,7 +1122,7 @@ class Render {
1122
1122
  if (!this.store.getOptionByID(elementID)) {
1123
1123
  this.callbacks.addOption(option);
1124
1124
  }
1125
- this.callbacks.setSelected(after.map((o) => o.value));
1125
+ this.callbacks.setSelected(after.map((o) => o.value), false);
1126
1126
  if (this.settings.closeOnSelect) {
1127
1127
  this.callbacks.close();
1128
1128
  }
@@ -1258,6 +1258,7 @@ class Select {
1258
1258
  if (!this.listen) {
1259
1259
  return;
1260
1260
  }
1261
+ let classChanged = false;
1261
1262
  let disabledChanged = false;
1262
1263
  let optgroupOptionChanged = false;
1263
1264
  for (const m of mutations) {
@@ -1265,11 +1266,17 @@ class Select {
1265
1266
  if (m.attributeName === 'disabled') {
1266
1267
  disabledChanged = true;
1267
1268
  }
1269
+ if (m.attributeName === 'class') {
1270
+ classChanged = true;
1271
+ }
1268
1272
  }
1269
1273
  if (m.target.nodeName === 'OPTGROUP' || m.target.nodeName === 'OPTION') {
1270
1274
  optgroupOptionChanged = true;
1271
1275
  }
1272
1276
  }
1277
+ if (classChanged && this.onClassChange) {
1278
+ this.onClassChange(this.select.className.split(' '));
1279
+ }
1273
1280
  if (disabledChanged && this.onDisabledChange) {
1274
1281
  this.changeListen(false);
1275
1282
  this.onDisabledChange(this.select.disabled);
@@ -1374,7 +1381,7 @@ class Select {
1374
1381
  updateSelect(id, style, classes) {
1375
1382
  this.changeListen(false);
1376
1383
  if (id) {
1377
- this.select.id = id;
1384
+ this.select.dataset.id = id;
1378
1385
  }
1379
1386
  if (style) {
1380
1387
  this.select.style.cssText = style;
@@ -1462,6 +1469,7 @@ class Select {
1462
1469
  this.observer.disconnect();
1463
1470
  this.observer = null;
1464
1471
  }
1472
+ delete this.select.dataset.id;
1465
1473
  this.showUI();
1466
1474
  }
1467
1475
  }
@@ -1534,6 +1542,10 @@ class SlimSelect {
1534
1542
  this.select.onValueChange = (values) => {
1535
1543
  this.setSelected(values);
1536
1544
  };
1545
+ this.select.onClassChange = (classes) => {
1546
+ this.settings.class = classes;
1547
+ this.render.updateClassStyles();
1548
+ };
1537
1549
  this.select.onDisabledChange = (disabled) => {
1538
1550
  if (disabled) {
1539
1551
  this.disable();
@@ -1612,7 +1624,7 @@ class SlimSelect {
1612
1624
  getSelected() {
1613
1625
  return this.store.getSelected();
1614
1626
  }
1615
- setSelected(value) {
1627
+ setSelected(value, runAfterChange = true) {
1616
1628
  const selected = this.store.getSelected();
1617
1629
  this.store.setSelectedBy('value', Array.isArray(value) ? value : [value]);
1618
1630
  const data = this.store.getData();
@@ -1624,7 +1636,7 @@ class SlimSelect {
1624
1636
  else {
1625
1637
  this.render.renderOptions(data);
1626
1638
  }
1627
- if (this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1639
+ if (runAfterChange && this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1628
1640
  this.events.afterChange(this.store.getSelectedOptions());
1629
1641
  }
1630
1642
  }
@@ -1671,7 +1683,9 @@ class SlimSelect {
1671
1683
  this.events.beforeClose();
1672
1684
  }
1673
1685
  this.render.close();
1674
- this.search('');
1686
+ if (this.render.content.search.input.value !== '') {
1687
+ this.search('');
1688
+ }
1675
1689
  this.render.mainFocus(false);
1676
1690
  setTimeout(() => {
1677
1691
  if (this.events.afterClose) {
@@ -346,6 +346,7 @@ var SlimSelectVue = (function (vue) {
346
346
  this.callbacks = callbacks;
347
347
  this.main = this.mainDiv();
348
348
  this.content = this.contentDiv();
349
+ this.updateClassStyles();
349
350
  this.settings.contentLocation.appendChild(this.content.main);
350
351
  }
351
352
  enable() {
@@ -360,8 +361,6 @@ var SlimSelectVue = (function (vue) {
360
361
  this.main.arrow.path.setAttribute('d', this.classes.arrowOpen);
361
362
  this.main.main.classList.add(this.settings.openPosition === 'up' ? this.classes.openAbove : this.classes.openBelow);
362
363
  this.moveContent();
363
- this.renderOptions(this.store.getData());
364
- this.moveContent();
365
364
  const selectedOptions = this.store.getSelectedOptions();
366
365
  if (selectedOptions.length) {
367
366
  const selectedId = selectedOptions[selectedOptions.length - 1].id;
@@ -378,20 +377,32 @@ var SlimSelectVue = (function (vue) {
378
377
  this.content.main.classList.remove(this.classes.openBelow);
379
378
  this.main.arrow.path.setAttribute('d', this.classes.arrowClose);
380
379
  }
381
- mainDiv() {
382
- const main = document.createElement('div');
383
- main.tabIndex = 0;
384
- main.style.cssText = this.settings.style !== '' ? this.settings.style : '';
385
- main.className = '';
386
- main.classList.add(this.settings.id);
387
- main.classList.add(this.classes.main);
388
- if (this.settings.class) {
380
+ updateClassStyles() {
381
+ this.main.main.className = '';
382
+ this.main.main.removeAttribute('style');
383
+ this.content.main.className = '';
384
+ this.content.main.removeAttribute('style');
385
+ this.main.main.classList.add(this.classes.main);
386
+ this.content.main.classList.add(this.classes.content);
387
+ if (this.settings.style !== '') {
388
+ this.content.main.style.cssText = this.settings.style;
389
+ }
390
+ if (this.settings.class.length) {
389
391
  for (const c of this.settings.class) {
390
392
  if (c.trim() !== '') {
391
- main.classList.add(c.trim());
393
+ this.main.main.classList.add(c.trim());
394
+ this.content.main.classList.add(c.trim());
392
395
  }
393
396
  }
394
397
  }
398
+ if (this.settings.contentPosition === 'relative') {
399
+ this.content.main.classList.add('ss-' + this.settings.contentPosition);
400
+ }
401
+ }
402
+ mainDiv() {
403
+ const main = document.createElement('div');
404
+ main.dataset.id = this.settings.id;
405
+ main.tabIndex = 0;
395
406
  main.onfocus = () => {
396
407
  if (this.settings.triggerFocus) {
397
408
  this.callbacks.open();
@@ -444,7 +455,7 @@ var SlimSelectVue = (function (vue) {
444
455
  shouldDelete = this.callbacks.beforeChange(after, before) === true;
445
456
  }
446
457
  if (shouldDelete) {
447
- this.callbacks.setSelected(['']);
458
+ this.callbacks.setSelected([''], false);
448
459
  if (this.settings.closeOnSelect) {
449
460
  this.callbacks.close();
450
461
  }
@@ -643,7 +654,7 @@ var SlimSelectVue = (function (vue) {
643
654
  selectedValues.push(o.value);
644
655
  }
645
656
  }
646
- this.callbacks.setSelected(selectedValues);
657
+ this.callbacks.setSelected(selectedValues, false);
647
658
  if (this.settings.closeOnSelect) {
648
659
  this.callbacks.close();
649
660
  }
@@ -664,21 +675,7 @@ var SlimSelectVue = (function (vue) {
664
675
  }
665
676
  contentDiv() {
666
677
  const main = document.createElement('div');
667
- main.classList.add(this.classes.content);
668
678
  main.dataset.id = this.settings.id;
669
- if (this.settings.style !== '') {
670
- main.style.cssText = this.settings.style;
671
- }
672
- if (this.settings.contentPosition === 'relative') {
673
- main.classList.add('ss-' + this.settings.contentPosition);
674
- }
675
- if (this.settings.class.length) {
676
- for (const c of this.settings.class) {
677
- if (c.trim() !== '') {
678
- main.classList.add(c.trim());
679
- }
680
- }
681
- }
682
679
  const search = this.searchDiv();
683
680
  main.appendChild(search.main);
684
681
  const list = this.listDiv();
@@ -790,10 +787,10 @@ var SlimSelectVue = (function (vue) {
790
787
  if (this.settings.isMultiple) {
791
788
  let values = this.store.getSelected();
792
789
  values.push(newOption.value);
793
- this.callbacks.setSelected(values);
790
+ this.callbacks.setSelected(values, true);
794
791
  }
795
792
  else {
796
- this.callbacks.setSelected([newOption.value]);
793
+ this.callbacks.setSelected([newOption.value], true);
797
794
  }
798
795
  this.callbacks.search('');
799
796
  if (this.settings.closeOnSelect) {
@@ -975,12 +972,12 @@ var SlimSelectVue = (function (vue) {
975
972
  }
976
973
  return true;
977
974
  });
978
- this.callbacks.setSelected(newSelected);
975
+ this.callbacks.setSelected(newSelected, true);
979
976
  return;
980
977
  }
981
978
  else {
982
979
  const newSelected = currentSelected.concat(d.options.map((o) => o.value));
983
- this.callbacks.setSelected(newSelected);
980
+ this.callbacks.setSelected(newSelected, true);
984
981
  }
985
982
  });
986
983
  optgroupActions.appendChild(selectAll);
@@ -1064,6 +1061,9 @@ var SlimSelectVue = (function (vue) {
1064
1061
  if (this.settings.showOptionTooltips && optionEl.textContent) {
1065
1062
  optionEl.setAttribute('title', optionEl.textContent);
1066
1063
  }
1064
+ if (!option.display) {
1065
+ optionEl.classList.add(this.classes.hide);
1066
+ }
1067
1067
  if (option.disabled) {
1068
1068
  optionEl.classList.add(this.classes.disabled);
1069
1069
  }
@@ -1123,7 +1123,7 @@ var SlimSelectVue = (function (vue) {
1123
1123
  if (!this.store.getOptionByID(elementID)) {
1124
1124
  this.callbacks.addOption(option);
1125
1125
  }
1126
- this.callbacks.setSelected(after.map((o) => o.value));
1126
+ this.callbacks.setSelected(after.map((o) => o.value), false);
1127
1127
  if (this.settings.closeOnSelect) {
1128
1128
  this.callbacks.close();
1129
1129
  }
@@ -1259,6 +1259,7 @@ var SlimSelectVue = (function (vue) {
1259
1259
  if (!this.listen) {
1260
1260
  return;
1261
1261
  }
1262
+ let classChanged = false;
1262
1263
  let disabledChanged = false;
1263
1264
  let optgroupOptionChanged = false;
1264
1265
  for (const m of mutations) {
@@ -1266,11 +1267,17 @@ var SlimSelectVue = (function (vue) {
1266
1267
  if (m.attributeName === 'disabled') {
1267
1268
  disabledChanged = true;
1268
1269
  }
1270
+ if (m.attributeName === 'class') {
1271
+ classChanged = true;
1272
+ }
1269
1273
  }
1270
1274
  if (m.target.nodeName === 'OPTGROUP' || m.target.nodeName === 'OPTION') {
1271
1275
  optgroupOptionChanged = true;
1272
1276
  }
1273
1277
  }
1278
+ if (classChanged && this.onClassChange) {
1279
+ this.onClassChange(this.select.className.split(' '));
1280
+ }
1274
1281
  if (disabledChanged && this.onDisabledChange) {
1275
1282
  this.changeListen(false);
1276
1283
  this.onDisabledChange(this.select.disabled);
@@ -1375,7 +1382,7 @@ var SlimSelectVue = (function (vue) {
1375
1382
  updateSelect(id, style, classes) {
1376
1383
  this.changeListen(false);
1377
1384
  if (id) {
1378
- this.select.id = id;
1385
+ this.select.dataset.id = id;
1379
1386
  }
1380
1387
  if (style) {
1381
1388
  this.select.style.cssText = style;
@@ -1463,6 +1470,7 @@ var SlimSelectVue = (function (vue) {
1463
1470
  this.observer.disconnect();
1464
1471
  this.observer = null;
1465
1472
  }
1473
+ delete this.select.dataset.id;
1466
1474
  this.showUI();
1467
1475
  }
1468
1476
  }
@@ -1535,6 +1543,10 @@ var SlimSelectVue = (function (vue) {
1535
1543
  this.select.onValueChange = (values) => {
1536
1544
  this.setSelected(values);
1537
1545
  };
1546
+ this.select.onClassChange = (classes) => {
1547
+ this.settings.class = classes;
1548
+ this.render.updateClassStyles();
1549
+ };
1538
1550
  this.select.onDisabledChange = (disabled) => {
1539
1551
  if (disabled) {
1540
1552
  this.disable();
@@ -1613,7 +1625,7 @@ var SlimSelectVue = (function (vue) {
1613
1625
  getSelected() {
1614
1626
  return this.store.getSelected();
1615
1627
  }
1616
- setSelected(value) {
1628
+ setSelected(value, runAfterChange = true) {
1617
1629
  const selected = this.store.getSelected();
1618
1630
  this.store.setSelectedBy('value', Array.isArray(value) ? value : [value]);
1619
1631
  const data = this.store.getData();
@@ -1625,7 +1637,7 @@ var SlimSelectVue = (function (vue) {
1625
1637
  else {
1626
1638
  this.render.renderOptions(data);
1627
1639
  }
1628
- if (this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1640
+ if (runAfterChange && this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1629
1641
  this.events.afterChange(this.store.getSelectedOptions());
1630
1642
  }
1631
1643
  }
@@ -1672,7 +1684,9 @@ var SlimSelectVue = (function (vue) {
1672
1684
  this.events.beforeClose();
1673
1685
  }
1674
1686
  this.render.close();
1675
- this.search('');
1687
+ if (this.render.content.search.input.value !== '') {
1688
+ this.search('');
1689
+ }
1676
1690
  this.render.mainFocus(false);
1677
1691
  setTimeout(() => {
1678
1692
  if (this.events.afterClose) {