js.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function Form(el, data, okCallback, cancelCallback) {
  2. let formBody = document.createElement("div");
  3. let okButton = document.createElement("button");
  4. okButton.innerHTML = "OK";
  5. let cancelButton = document.createElement("button");
  6. cancelButton.innerHTML = "Cancel";
  7. for (var key in data) {
  8. var inputs = document.createElement("input");
  9. var p = document.createElement('p')
  10. p.innerText = key
  11. inputs.value = data[key];
  12. inputs.oninput = () => {
  13. // data = inputs.value
  14. console.log(inputs.value);
  15. };
  16. el.appendChild(p)
  17. el.appendChild(inputs);
  18. }
  19. if (typeof okCallback === "function") {
  20. formBody.appendChild(okButton);
  21. okButton.onclick = (e) => {
  22. console.log(this);
  23. this.okCallback(e);
  24. };
  25. }
  26. if (typeof cancelCallback === "function") {
  27. formBody.appendChild(cancelButton);
  28. cancelButton.onclick = cancelCallback;
  29. }
  30. el.appendChild(formBody);
  31. this.okCallback = okCallback;
  32. this.cancelCallback = cancelCallback;
  33. this.data = data;
  34. this.validators = {};
  35. }
  36. let form = new Form(
  37. formContainer,
  38. {
  39. name: "Anakin",
  40. surname: "Skywalker",
  41. married: true,
  42. birthday: new Date(new Date().getTime() - 86400000 * 30 * 365),
  43. },
  44. () => console.log("ok"),
  45. () => console.log("cancel")
  46. );
  47. form.okCallback = () => console.log("ok2");
  48. form.validators.surname = (value, key, data, input) =>
  49. value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(" ")
  50. ? true
  51. : "Wrong name";
  52. console.log(form);