Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitosis does not convert optional callbacks in angular to use @Output and event emitter. #918

Open
1 of 11 tasks
cadamsdev opened this issue Nov 16, 2022 · 2 comments
Open
1 of 11 tasks
Labels
bug Something isn't working

Comments

@cadamsdev
Copy link
Contributor

cadamsdev commented Nov 16, 2022

I am interested in helping provide a fix!

Yes

Which generators are impacted?

  • All
  • Angular
  • HTML
  • Qwik
  • React
  • React-Native
  • Solid
  • Stencil
  • Svelte
  • Vue
  • Web components

Reproduction case

https://mitosis.builder.io/?outputTab=IYOw5grgNsBOQ%3D%3D%3D&code=KYDwDg9gTgLgBAE2AMwIYFcA29noHYDGMAlhHnALICeAwhALaR7B4wAUYUEYAzgJRwA3gCg4cXIRJk4AC1R4EmYDUzECAawBC6GDDJsBIsWM7ceAOjIqIPYAH5zBgNxxRcAL7C3UYDHRRyNjcxAB4AIx09citVDQBeQQM4OIA%2BWXlFZVitSP0%2BdxSCbLh6YBCAegjdMhS3PidhTyA%3D%3D%3D

Expected Behaviour

mitosis

export default function MyComponent(props) {
  function handleClickButton() {
    props.onClose?.(); 
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

should be converted into this angular code

export class MyComponent {
  @Output() onClose = new EventEmitter();

  handleClickButton = function handleClickButton() {
    this.onClose.emit();
  };
}

Actual Behaviour

mitosis

export default function MyComponent(props) {
  function handleClickButton() {
    // do something here
    // angular output should convert this into this.onClose.emit();
    props.onClose?.(); 
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

angular

export class MyComponent {
  @Input() onClose: any;

  handleClickButton = function handleClickButton() {
    // do something here
    // angular output should convert this into this.onClose.emit();
    this.onClose?.();
  };
}

Additional Information

Angular output
see here

Stackblitz
see here

@cadamsdev cadamsdev added the bug Something isn't working label Nov 16, 2022
@cadamsdev cadamsdev changed the title Mitosis not converting callbacks into @Output events in angular Mitosis does not convert callbacks in angular to use @Output and event emitter. Nov 16, 2022
@kingzez
Copy link
Contributor

kingzez commented Nov 17, 2022

export default function MyComponent(props) {
  function handleClickButton() {
    props.onClose?.();   //  change to `props.onClose()` works
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

@cadamsdev
Copy link
Contributor Author

cadamsdev commented Nov 17, 2022

export default function MyComponent(props) {
  function handleClickButton() {
    props.onClose?.();   //  change to `props.onClose()` works
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

That does work, but what happens if the callback is optional? If I do this I'll have lint issues.

We'd have to do something like this (which works)

export default function MyComponent(props) {
  function handleClickButton() {
    if (props.onClose) {
      props.onClose(); 
    }
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

see fiddle

Though mitosis should also support this way as well.

export default function MyComponent(props) {
  function handleClickButton() {
      props.onClose?.(); 
  }

  return (
    <button onClick={() => handleClickButton()}>click me</button>
  );
}

@cadamsdev cadamsdev reopened this Nov 17, 2022
@cadamsdev cadamsdev changed the title Mitosis does not convert callbacks in angular to use @Output and event emitter. Mitosis does not convert optional callbacks in angular to use @Output and event emitter. Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants