index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { __spreadArray } from "tslib";
  2. import { useEvent } from 'functional-mini/component';
  3. import { mountComponent } from '../../_util/component';
  4. import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
  5. import { useMixState } from '../../_util/hooks/useMixState';
  6. import { CheckboxGroupFunctionalProps } from './props';
  7. var CheckboxGroup = function (props) {
  8. var _a = useMixState(props.defaultValue, {
  9. value: props.value,
  10. postState: function (value) {
  11. return {
  12. valid: true,
  13. value: value || [],
  14. };
  15. },
  16. }), value = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  17. var triggerEvent = useComponentEvent(props).triggerEvent;
  18. useEvent('onChange', function (args, e) {
  19. if (props.disabled) {
  20. return;
  21. }
  22. var event;
  23. event = e;
  24. var currentValue = value;
  25. var index = event.currentTarget.dataset.index;
  26. var selectValue = props.options[index].value;
  27. if (currentValue.indexOf(selectValue) > -1) {
  28. currentValue = currentValue.filter(function (v) { return v !== selectValue; });
  29. }
  30. else {
  31. currentValue = __spreadArray(__spreadArray([], currentValue, true), [selectValue], false);
  32. }
  33. if (!isControlled) {
  34. update(currentValue);
  35. }
  36. triggerEvent('change', currentValue, e);
  37. });
  38. return {
  39. mixin: { value: value },
  40. };
  41. };
  42. mountComponent(CheckboxGroup, CheckboxGroupFunctionalProps);