index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  2. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  3. if (ar || !(i in from)) {
  4. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  5. ar[i] = from[i];
  6. }
  7. }
  8. return to.concat(ar || Array.prototype.slice.call(from));
  9. };
  10. import { useEvent } from 'functional-mini/component';
  11. import '../_util/assert-component2';
  12. import { mountComponent } from '../_util/component';
  13. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  14. import { useMixState } from '../_util/hooks/useMixState';
  15. import { SelectorFunctionalProps } from './props';
  16. var Selector = function (props) {
  17. var _a = useMixState(props.defaultValue, {
  18. value: props.value,
  19. }), selectorValue = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  20. var triggerEventValues = useComponentEvent(props).triggerEventValues;
  21. useEvent('onChange', function (e) {
  22. var _a = e.currentTarget.dataset, disabled = _a.disabled, value = _a.value;
  23. var multiple = props.multiple, options = props.options, maxSelectedCount = props.maxSelectedCount, minSelectedCount = props.minSelectedCount;
  24. if (disabled || props.disabled) {
  25. return;
  26. }
  27. if (multiple) {
  28. var currentValue_1 = selectorValue || [];
  29. if (currentValue_1.indexOf(value) > -1) {
  30. if (typeof minSelectedCount === 'number' &&
  31. currentValue_1.length <= minSelectedCount) {
  32. triggerEventValues('selectMin', [value, options.find(function (v) { return v.value === value; })], e);
  33. return;
  34. }
  35. currentValue_1 = currentValue_1.filter(function (v) { return v !== value; });
  36. }
  37. else {
  38. if (typeof maxSelectedCount === 'number' &&
  39. currentValue_1.length >= maxSelectedCount) {
  40. triggerEventValues('selectMax', [value, options.find(function (v) { return v.value === value; })], e);
  41. return;
  42. }
  43. currentValue_1 = __spreadArray(__spreadArray([], currentValue_1, true), [value], false);
  44. }
  45. if (!isControlled) {
  46. update(currentValue_1);
  47. }
  48. triggerEventValues('change', [
  49. currentValue_1,
  50. options.filter(function (v) { return currentValue_1.indexOf(v.value) > -1; }),
  51. ], e);
  52. }
  53. else {
  54. if (value === selectorValue) {
  55. if (minSelectedCount === 1) {
  56. triggerEventValues('selectMin', [value, options.find(function (v) { return v.value === value; })], e);
  57. return;
  58. }
  59. if (!isControlled) {
  60. update(undefined);
  61. }
  62. triggerEventValues('change', [undefined, undefined], e);
  63. }
  64. else {
  65. if (!isControlled) {
  66. update(value);
  67. }
  68. triggerEventValues('change', [value, options.find(function (v) { return v.value === value; })], e);
  69. }
  70. }
  71. });
  72. return {
  73. mixin: {
  74. value: selectorValue,
  75. },
  76. };
  77. };
  78. mountComponent(Selector, SelectorFunctionalProps);