Source

fields/Button.js

  1. /**
  2. * @category Fields
  3. * @class
  4. * Simple widget for a button element.
  5. */
  6. class Button {
  7. constructor(props) {
  8. Object.assign(
  9. this,
  10. {
  11. id: null,
  12. vapp: document.querySelector(".lt-vapp"),
  13. onClick: null,
  14. },
  15. props
  16. );
  17. this.el = this.vapp.querySelector(this.id);
  18. if (!this.el) {
  19. console.error(`The button id '${this.id}' does not exists.`);
  20. return null;
  21. }
  22. this.el.addEventListener("click", (evt) => {
  23. evt.stopPropagation();
  24. evt.preventDefault();
  25. this.click(evt);
  26. });
  27. return this;
  28. }
  29. /**
  30. * Trigger onClick callback.
  31. * @param {Event} evt - Event fired.
  32. * @memberof Button
  33. */
  34. click(evt = null) {
  35. if (this.onClick) this.onClick(evt);
  36. }
  37. }
  38. export default Button;