index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 { mountComponent } from '../_util/component';
  11. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  12. import { useHandleCustomEvent } from '../_util/hooks/useHandleCustomEvent';
  13. import { useMixState } from '../_util/hooks/useMixState';
  14. import { ChecklistFunctionalProps, } from './props';
  15. var Checkbox = function (props) {
  16. var _a = useMixState(props.defaultValue, {
  17. value: props.value,
  18. postState: function (val) {
  19. var value = val || [];
  20. return {
  21. valid: true,
  22. value: value,
  23. };
  24. },
  25. }), state = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  26. var triggerEventValues = useComponentEvent(props).triggerEventValues;
  27. useHandleCustomEvent('onChange', function (item) {
  28. var multiple = props.multiple, options = props.options;
  29. var value = item.value;
  30. if (multiple) {
  31. var currentValue_1 = state;
  32. if (currentValue_1.indexOf(value) > -1) {
  33. currentValue_1 = currentValue_1.filter(function (v) { return v !== value; });
  34. }
  35. else {
  36. currentValue_1 = __spreadArray(__spreadArray([], currentValue_1, true), [value], false);
  37. }
  38. if (!isControlled) {
  39. update(currentValue_1);
  40. }
  41. triggerEventValues('change', [
  42. currentValue_1,
  43. options.filter(function (v) { return currentValue_1.indexOf(v.value) > -1; }),
  44. ]);
  45. }
  46. else {
  47. if (!isControlled) {
  48. update(value);
  49. }
  50. triggerEventValues('change', [
  51. value,
  52. options.find(function (v) { return v.value === value; }),
  53. ]);
  54. }
  55. });
  56. return {
  57. mixin: {
  58. value: state,
  59. },
  60. };
  61. };
  62. mountComponent(Checkbox, ChecklistFunctionalProps);