index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 { mountComponent } from '../../_util/component';
  12. import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
  13. import { useMixState } from '../../_util/hooks/useMixState';
  14. import { CheckboxGroupFunctionalProps } from './props';
  15. var CheckboxGroup = function (props) {
  16. var _a = useMixState(props.defaultValue, {
  17. value: props.value,
  18. postState: function (value) {
  19. return {
  20. valid: true,
  21. value: value || [],
  22. };
  23. },
  24. }), value = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  25. var triggerEvent = useComponentEvent(props).triggerEvent;
  26. useEvent('onChange', function (args, e) {
  27. if (props.disabled) {
  28. return;
  29. }
  30. var event;
  31. event = args;
  32. var currentValue = value;
  33. var index = event.currentTarget.dataset.index;
  34. var selectValue = props.options[index].value;
  35. if (currentValue.indexOf(selectValue) > -1) {
  36. currentValue = currentValue.filter(function (v) { return v !== selectValue; });
  37. }
  38. else {
  39. currentValue = __spreadArray(__spreadArray([], currentValue, true), [selectValue], false);
  40. }
  41. if (!isControlled) {
  42. update(currentValue);
  43. }
  44. triggerEvent('change', currentValue, e);
  45. });
  46. return {
  47. mixin: { value: value },
  48. };
  49. };
  50. mountComponent(CheckboxGroup, CheckboxGroupFunctionalProps);