index.js 1.9 KB

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