index.js 947 B

12345678910111213141516171819202122232425
  1. import { RadioGroupFunctionalProps } from './props';
  2. import { mountComponent } from '../../_util/component';
  3. import { useMixState } from '../../_util/hooks/useMixState';
  4. import { useEvent } from 'functional-mini/component';
  5. import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
  6. var RadioGroup = function (props) {
  7. var _a = useMixState(props.defaultValue, {
  8. value: props.value,
  9. }), value = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  10. var triggerEvent = useComponentEvent(props).triggerEvent;
  11. useEvent('onChange', function (_, e) {
  12. var index = e.currentTarget.dataset.index;
  13. var value = props.options[index].value;
  14. if (!isControlled) {
  15. update(value);
  16. }
  17. triggerEvent('change', value, e);
  18. });
  19. return {
  20. mixin: {
  21. value: value,
  22. },
  23. };
  24. };
  25. mountComponent(RadioGroup, RadioGroupFunctionalProps);