Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

174 righe
5.8 KiB

  1. var jsDaisyUI = {
  2. // Function to generate a DaisyUI 5-based form as a JSON structure from a JSON schema
  3. schemaToForm: function (params) {
  4. if (!params.schema || !params.schema.properties) {
  5. return {
  6. html: [
  7. {
  8. tag: 'p',
  9. attr: {},
  10. text: 'schemaToForm Error: Invalid JSON schema provided.'
  11. }
  12. ]
  13. };
  14. }
  15. const formElements = [];
  16. // Iterate through schema properties
  17. for (const [key, prop] of Object.entries(params.schema.properties)) {
  18. const label = prop.title || key;
  19. const required = params.schema.required && params.schema.required.includes(key) ? 'required' : '';
  20. const inputId = `input-${key}`; // Unique ID for accessibility
  21. const hiddenId = (key == 'id' && jsonApp && jsonApp.json.data.settings && !jsonApp.json.data.settings.debug) ? ' hidden' : '';
  22. // Create fieldset for each field
  23. const fieldsetChildren = [
  24. {
  25. tag: 'label',
  26. attr: {
  27. class: 'label text-primary capitalize' + hiddenId,
  28. for: inputId
  29. },
  30. html: [
  31. {
  32. tag: 'span',
  33. attr: {},
  34. text: label
  35. }
  36. ]
  37. }
  38. ];
  39. // Handle different input types
  40. let inputElement;
  41. switch (prop.type) {
  42. case 'string':
  43. inputElement = {
  44. tag: 'input',
  45. attr: {
  46. id: inputId,
  47. type: 'text',
  48. name: key,
  49. placeholder: prop.description || ' ',
  50. class: 'w-100 input input-primary' + hiddenId,
  51. ...(required && { required: '' })
  52. }
  53. };
  54. break;
  55. case 'number':
  56. case 'integer':
  57. inputElement = {
  58. tag: 'input',
  59. attr: {
  60. id: inputId,
  61. type: 'number',
  62. name: key,
  63. placeholder: prop.description || ' ',
  64. class: 'w-100 input input-primary',
  65. ...(required && { required: '' })
  66. }
  67. };
  68. break;
  69. case 'boolean':
  70. inputElement = {
  71. tag: 'input',
  72. attr: {
  73. id: inputId,
  74. type: 'checkbox',
  75. name: key,
  76. class: 'toggle',
  77. ...(required && { required: '' })
  78. }
  79. };
  80. break;
  81. case 'array':
  82. inputElement = {
  83. tag: 'textarea',
  84. attr: {
  85. id: inputId,
  86. name: key,
  87. placeholder: prop.description || 'Enter values separated by commas',
  88. class: 'w-100 textarea input-primary',
  89. ...(required && { required: '' })
  90. }
  91. };
  92. break;
  93. case 'object':
  94. inputElement = {
  95. tag: 'textarea',
  96. attr: {
  97. id: inputId,
  98. name: key,
  99. placeholder: prop.description || 'Enter JSON object',
  100. class: 'w-100 textarea input-primary',
  101. ...(required && { required: '' })
  102. }
  103. };
  104. break;
  105. default:
  106. inputElement = {
  107. tag: 'input',
  108. attr: {
  109. id: inputId,
  110. type: 'text',
  111. name: key,
  112. placeholder: prop.description || ' ',
  113. class: 'w-100 input input-primary',
  114. ...(required && { required: '' })
  115. }
  116. };
  117. }
  118. // Add input element to fieldset
  119. fieldsetChildren.push(inputElement);
  120. // Add fieldset to form elements
  121. formElements.push({
  122. tag: 'fieldset',
  123. attr: { class: 'fieldset text-center' },
  124. html: fieldsetChildren
  125. });
  126. }
  127. // Add submit button
  128. formElements.push({
  129. tag: 'div',
  130. attr: { class: 'w-100 mt-6 text-center' },
  131. html: [
  132. {
  133. tag: 'button',
  134. attr: {
  135. type: 'submit',
  136. class: 'btn btn-outline btn-primary'
  137. },
  138. text: 'Submit'
  139. }
  140. ]
  141. });
  142. // Return the full form structure
  143. return {
  144. html: [
  145. {
  146. tag: 'form',
  147. attr: { class: 'space-y-4' },
  148. html: formElements
  149. }
  150. ]
  151. };
  152. },
  153. createForm: function (params) {
  154. let appForm = jsDaisyUI.schemaToForm(params);
  155. appForm.selector = params.selector;
  156. jsonApp.do(appForm);
  157. }
  158. }