index.js 3.2 KB

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