chunk.UBYB56LV.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {
  2. event
  3. } from "./chunk.XX234VRK.js";
  4. import {
  5. T,
  6. e,
  7. h,
  8. n,
  9. o,
  10. r
  11. } from "./chunk.5PIDMFOE.js";
  12. import {
  13. __decorateClass
  14. } from "./chunk.IHGPZX35.js";
  15. // _uyihdawu1:/Users/claviska/Projects/shoelace/src/components/form/form.scss
  16. var form_default = ":host {\n position: relative;\n box-sizing: border-box;\n}\n:host *, :host *:before, :host *:after {\n box-sizing: inherit;\n}\n\n[hidden] {\n display: none !important;\n}\n\n:host {\n display: block;\n}";
  17. // src/components/form/form.ts
  18. var SlForm = class extends h {
  19. constructor() {
  20. super(...arguments);
  21. this.novalidate = false;
  22. }
  23. connectedCallback() {
  24. super.connectedCallback();
  25. this.formControls = [
  26. {
  27. tag: "button",
  28. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null,
  29. click: (event2) => {
  30. const target = event2.target;
  31. if (target.type === "submit") {
  32. this.submit();
  33. }
  34. }
  35. },
  36. {
  37. tag: "input",
  38. serialize: (el, formData) => {
  39. if (!el.name || el.disabled) {
  40. return;
  41. }
  42. if ((el.type === "checkbox" || el.type === "radio") && !el.checked) {
  43. return;
  44. }
  45. if (el.type === "file") {
  46. [...el.files].map((file) => formData.append(el.name, file));
  47. return;
  48. }
  49. formData.append(el.name, el.value);
  50. },
  51. click: (event2) => {
  52. const target = event2.target;
  53. if (target.type === "submit") {
  54. this.submit();
  55. }
  56. },
  57. keyDown: (event2) => {
  58. const target = event2.target;
  59. if (event2.key === "Enter" && !event2.defaultPrevented && !["checkbox", "file", "radio"].includes(target.type)) {
  60. this.submit();
  61. }
  62. }
  63. },
  64. {
  65. tag: "select",
  66. serialize: (el, formData) => {
  67. if (el.name && !el.disabled) {
  68. if (el.multiple) {
  69. const selectedOptions = [...el.querySelectorAll("option:checked")];
  70. if (selectedOptions.length) {
  71. selectedOptions.map((option) => formData.append(el.name, option.value));
  72. } else {
  73. formData.append(el.name, "");
  74. }
  75. } else {
  76. formData.append(el.name, el.value);
  77. }
  78. }
  79. }
  80. },
  81. {
  82. tag: "sl-button",
  83. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null,
  84. click: (event2) => {
  85. const target = event2.target;
  86. if (target.submit) {
  87. this.submit();
  88. }
  89. }
  90. },
  91. {
  92. tag: "sl-checkbox",
  93. serialize: (el, formData) => el.name && el.checked && !el.disabled ? formData.append(el.name, el.value) : null
  94. },
  95. {
  96. tag: "sl-color-picker",
  97. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null
  98. },
  99. {
  100. tag: "sl-input",
  101. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null,
  102. keyDown: (event2) => {
  103. if (event2.key === "Enter" && !event2.defaultPrevented) {
  104. this.submit();
  105. }
  106. }
  107. },
  108. {
  109. tag: "sl-radio",
  110. serialize: (el, formData) => el.name && el.checked && !el.disabled ? formData.append(el.name, el.value) : null
  111. },
  112. {
  113. tag: "sl-range",
  114. serialize: (el, formData) => {
  115. if (el.name && !el.disabled) {
  116. formData.append(el.name, el.value + "");
  117. }
  118. }
  119. },
  120. {
  121. tag: "sl-select",
  122. serialize: (el, formData) => {
  123. if (el.name && !el.disabled) {
  124. if (el.multiple) {
  125. const selectedOptions = [...el.value];
  126. if (selectedOptions.length) {
  127. selectedOptions.map((value) => formData.append(el.name, value));
  128. } else {
  129. formData.append(el.name, "");
  130. }
  131. } else {
  132. formData.append(el.name, el.value + "");
  133. }
  134. }
  135. }
  136. },
  137. {
  138. tag: "sl-switch",
  139. serialize: (el, formData) => el.name && el.checked && !el.disabled ? formData.append(el.name, el.value) : null
  140. },
  141. {
  142. tag: "sl-textarea",
  143. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null
  144. },
  145. {
  146. tag: "textarea",
  147. serialize: (el, formData) => el.name && !el.disabled ? formData.append(el.name, el.value) : null
  148. }
  149. ];
  150. }
  151. getFormData() {
  152. const formData = new FormData();
  153. const formControls = this.getFormControls();
  154. formControls.map((el) => this.serializeElement(el, formData));
  155. return formData;
  156. }
  157. getFormControls() {
  158. const slot = this.form.querySelector("slot");
  159. const tags = this.formControls.map((control) => control.tag);
  160. return slot.assignedElements({ flatten: true }).reduce((all, el) => all.concat(el, [...el.querySelectorAll("*")]), []).filter((el) => tags.includes(el.tagName.toLowerCase()));
  161. }
  162. submit() {
  163. const formData = this.getFormData();
  164. const formControls = this.getFormControls();
  165. const formControlsThatReport = formControls.filter((el) => typeof el.reportValidity === "function");
  166. if (!this.novalidate) {
  167. for (const el of formControlsThatReport) {
  168. const isValid = el.reportValidity();
  169. if (!isValid) {
  170. return false;
  171. }
  172. }
  173. }
  174. this.slSubmit.emit({ detail: { formData, formControls } });
  175. return true;
  176. }
  177. handleClick(event2) {
  178. const target = event2.target;
  179. const tag = target.tagName.toLowerCase();
  180. for (const formControl of this.formControls) {
  181. if (formControl.tag === tag && formControl.click) {
  182. formControl.click(event2);
  183. }
  184. }
  185. }
  186. handleKeyDown(event2) {
  187. const target = event2.target;
  188. const tag = target.tagName.toLowerCase();
  189. for (const formControl of this.formControls) {
  190. if (formControl.tag === tag && formControl.keyDown) {
  191. formControl.keyDown(event2);
  192. }
  193. }
  194. }
  195. serializeElement(el, formData) {
  196. const tag = el.tagName.toLowerCase();
  197. for (const formControl of this.formControls) {
  198. if (formControl.tag === tag) {
  199. return formControl.serialize(el, formData);
  200. }
  201. }
  202. return null;
  203. }
  204. render() {
  205. return T`
  206. <div part="base" class="form" role="form" @click=${this.handleClick} @keydown=${this.handleKeyDown}>
  207. <slot></slot>
  208. </div>
  209. `;
  210. }
  211. };
  212. SlForm.styles = r(form_default);
  213. __decorateClass([
  214. o(".form")
  215. ], SlForm.prototype, "form", 2);
  216. __decorateClass([
  217. e({ type: Boolean, reflect: true })
  218. ], SlForm.prototype, "novalidate", 2);
  219. __decorateClass([
  220. event("sl-submit")
  221. ], SlForm.prototype, "slSubmit", 2);
  222. SlForm = __decorateClass([
  223. n("sl-form")
  224. ], SlForm);
  225. var form_default2 = SlForm;
  226. export {
  227. form_default2 as form_default
  228. };