main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. function Form(el, data, okCallback, cancelCallback) {
  2. let rootData = { ...data }
  3. let formBody = document.createElement('div')
  4. let okButton = document.createElement('button')
  5. okButton.innerHTML = 'OK'
  6. let cancelButton = document.createElement('button')
  7. cancelButton.innerHTML = 'Cancel'
  8. const table = document.createElement('table')
  9. const errorText = document.createElement('span')
  10. let input
  11. let inputArray = []
  12. for (const [key, value] of Object.entries(data)) {
  13. const tr = document.createElement('tr')
  14. const th = document.createElement('th')
  15. const td = document.createElement('td')
  16. key[0] === '*' ? th.innerHTML = `<span style='color:red'>${key[0]}</span> ${key.slice(1)}` : th.innerHTML = key
  17. let inputCreators = {
  18. String(key, value, oninput) {
  19. const input = document.createElement('input')
  20. input.type = (/^[*]+$/i).test(value) ? 'password' : 'text'
  21. input.placeholder = key
  22. input.value = (/^[*]+$/i).test(value) ? '' : value
  23. input.oninput = () => {
  24. key[0] === '*' && input.value === '' ? input.style.borderColor = 'red' : input.style.borderColor = ''
  25. oninput(input.value)
  26. }
  27. return input
  28. },
  29. Boolean(key, value, oninput) {
  30. const input = document.createElement('input')
  31. input.type = 'checkbox'
  32. input.checked = value
  33. input.oninput = () => oninput(input.checked)
  34. return input
  35. },
  36. Date(key, value, oninput) {
  37. const input = document.createElement('input')
  38. input.type = 'datetime-local'
  39. input.oninput = () => oninput(new Date(input.value))
  40. const offset = value.getTimezoneOffset()
  41. const now = value.getTime()
  42. const nowPlusOffset = new Date(now - offset * 60 * 1000)
  43. input.value = nowPlusOffset.toISOString().slice(0, -1)
  44. return input
  45. },
  46. }
  47. input = inputCreators[value.constructor.name](key, value, value => {
  48. if (key in this.validators && !this.validators[key](value)) {
  49. errorText.innerText = `Please, enter correct ${key}`
  50. errorText.classList.add('error');
  51. input.style.borderColor = 'red'
  52. td.append(errorText)
  53. } else {
  54. input.style.color = 'black'
  55. errorText.remove()
  56. }
  57. data[key] = value
  58. })
  59. inputArray.push(input)
  60. table.append(tr)
  61. tr.append(th)
  62. tr.append(td)
  63. td.append(input)
  64. }
  65. console.log(inputArray[3].value);
  66. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>'
  67. formBody.append(table)
  68. if (typeof okCallback === 'function') {
  69. formBody.appendChild(okButton);
  70. okButton.onclick = () => {
  71. let controlInputValid = []
  72. let i = 0
  73. for (const [key, value] of Object.entries(data)) {
  74. if (value.constructor.name === 'String') key in this.validators ? controlInputValid.push(this.validators[key](value)) : key[0] === '*' && controlInputValid.push((inputArray[i].value !== '') ? true : false)
  75. i++
  76. }
  77. controlInputValid.some(el => el === false) ? alert('Внимательно вводим поля') : alert('Проходи')
  78. }
  79. }
  80. if (typeof cancelCallback === 'function') {
  81. formBody.appendChild(cancelButton);
  82. cancelButton.onclick = () => {
  83. let i = 0
  84. for (const key in rootData) {
  85. if (inputArray[i].type === 'text') inputArray[i++].value = rootData[key]
  86. else if (inputArray[i].type === 'password') inputArray[i++].value = ''
  87. else if (inputArray[i].type === 'checkbox') inputArray[i++].checked = rootData[key]
  88. else if (inputArray[i].type === 'datetime-local') {
  89. const offset = rootData[key].getTimezoneOffset()
  90. const now = rootData[key].getTime()
  91. const nowPlusOffset = new Date(now - offset * 60 * 1000)
  92. inputArray[i++].value = nowPlusOffset.toISOString().slice(0, -1)
  93. }
  94. }
  95. }
  96. }
  97. el.appendChild(formBody)
  98. this.okCallback = okCallback
  99. this.cancelCallback = cancelCallback
  100. this.data = data
  101. this.validators = {}
  102. }
  103. let form = new Form(formContainer, {
  104. '*name': 'Anakin',
  105. surname: 'Skywalker',
  106. '*password': '******',
  107. married: true,
  108. birthday: new Date((new Date).getTime() - 86400000 * 30 * 365)
  109. }, () => console.log('ok'), () => console.log('cancel'))
  110. // form.okCallback = () => console.log('ok2')
  111. form.validators.surname = (value, key, data, input) => value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(' ') ? true : false
  112. console.log(form)