index.js 832 B

12345678910111213141516171819202122
  1. import { mountComponent } from '../_util/component';
  2. import { useMergedState, hasValue } from '../_util/hooks/useMergedState';
  3. import { SwitchFunctionalProps } from './props';
  4. import { useEvent } from 'functional-mini/component';
  5. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  6. var Switch = function (props) {
  7. var _a = useMergedState(props.defaultChecked, {
  8. value: props.checked,
  9. }), value = _a[0], updateValue = _a[1];
  10. var triggerEvent = useComponentEvent(props).triggerEvent;
  11. useEvent('onChange', function (e) {
  12. var newValue = !value;
  13. if (!hasValue(props.checked)) {
  14. updateValue(newValue);
  15. }
  16. triggerEvent('change', newValue, e);
  17. });
  18. return {
  19. mixin: { value: value },
  20. };
  21. };
  22. mountComponent(Switch, SwitchFunctionalProps);