index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { __spreadArray } from "tslib";
  2. import { Component, triggerEventValues, getValueFromProps, } from '../_util/simply';
  3. import { SelectorDefaultProps } from './props';
  4. import mixinValue from '../mixins/value';
  5. Component(SelectorDefaultProps, {
  6. onChange: function (e) {
  7. var _a = e.currentTarget.dataset, disabled = _a.disabled, value = _a.value;
  8. var _b = getValueFromProps(this, [
  9. 'multiple',
  10. 'options',
  11. 'maxSelectedCount',
  12. 'minSelectedCount',
  13. 'disabled',
  14. ]), multiple = _b[0], options = _b[1], maxSelectedCount = _b[2], minSelectedCount = _b[3], disabledFromProps = _b[4];
  15. if (disabled || disabledFromProps) {
  16. return;
  17. }
  18. if (multiple) {
  19. var currentValue_1 = this.getValue() || [];
  20. if (currentValue_1.indexOf(value) > -1) {
  21. if (typeof minSelectedCount === 'number' &&
  22. currentValue_1.length <= minSelectedCount) {
  23. triggerEventValues(this, 'selectMin', [value, options.find(function (v) { return v.value === value; })], e);
  24. return;
  25. }
  26. currentValue_1 = currentValue_1.filter(function (v) { return v !== value; });
  27. }
  28. else {
  29. if (typeof maxSelectedCount === 'number' &&
  30. currentValue_1.length >= maxSelectedCount) {
  31. triggerEventValues(this, 'selectMax', [value, options.find(function (v) { return v.value === value; })], e);
  32. return;
  33. }
  34. currentValue_1 = __spreadArray(__spreadArray([], currentValue_1, true), [value], false);
  35. }
  36. if (!this.isControlled()) {
  37. this.update(currentValue_1);
  38. }
  39. triggerEventValues(this, 'change', [
  40. currentValue_1,
  41. options.filter(function (v) { return currentValue_1.indexOf(v.value) > -1; }),
  42. ], e);
  43. }
  44. else {
  45. if (value === this.getValue()) {
  46. if (minSelectedCount === 1) {
  47. triggerEventValues(this, 'selectMin', [value, options.find(function (v) { return v.value === value; })], e);
  48. return;
  49. }
  50. if (!this.isControlled()) {
  51. this.update(undefined);
  52. }
  53. triggerEventValues(this, 'change', [undefined, undefined], e);
  54. }
  55. else {
  56. if (!this.isControlled()) {
  57. this.update(value);
  58. }
  59. triggerEventValues(this, 'change', [value, options.find(function (v) { return v.value === value; })], e);
  60. }
  61. }
  62. },
  63. }, null, [mixinValue()]);