lucos_search_component 1.0.53 → 1.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5946,6 +5946,37 @@ class LucosSearchComponent extends HTMLSpanElement {
5946
5946
  shadow.append(selector.nextElementSibling);
5947
5947
  }
5948
5948
  }
5949
+ connectedCallback() {
5950
+ const form = this.closest('form');
5951
+ if (!form) return;
5952
+ this._form = form;
5953
+ this._formdataHandler = (event) => {
5954
+ const selector = this.querySelector('select');
5955
+ if (!selector || !selector.name) return;
5956
+ const ts = selector.tomselect;
5957
+ if (!ts) return;
5958
+ const name = selector.name;
5959
+ const values = ts.getValue();
5960
+ const valueArray = Array.isArray(values) ? values : (values ? [values] : []);
5961
+ // Remove the native select values so consumers only receive the structured pairs
5962
+ event.formData.delete(name);
5963
+ valueArray.forEach((id, idx) => {
5964
+ const option = ts.options[id];
5965
+ if (option) {
5966
+ event.formData.append(`${name}[${idx}][uri]`, id);
5967
+ event.formData.append(`${name}[${idx}][name]`, option.pref_label);
5968
+ }
5969
+ });
5970
+ };
5971
+ form.addEventListener('formdata', this._formdataHandler);
5972
+ }
5973
+ disconnectedCallback() {
5974
+ if (this._form && this._formdataHandler) {
5975
+ this._form.removeEventListener('formdata', this._formdataHandler);
5976
+ this._form = null;
5977
+ this._formdataHandler = null;
5978
+ }
5979
+ }
5949
5980
  get noLangOption() {
5950
5981
  const label = this.getAttribute("data-no-lang");
5951
5982
  if (!label) return null;
@@ -46,6 +46,24 @@
46
46
  <label for="lang3">Common languages option</label><span is="lucos-lang" data-api-key="${KEY_LUCOS_ARACHNE}" data-no-lang="No Language" data-common="en,ga,zxx">
47
47
  <select id="lang3"></select>
48
48
  </span>
49
+ <h1>Form submission</h1>
50
+ <form id="test-form" onsubmit="handleSubmit(event)">
51
+ <label for="tags">Tags:</label><span is="lucos-search" data-api-key="${KEY_LUCOS_ARACHNE}"><select id="tags" name="tags" multiple></select></span>
52
+ <button type="submit">Submit</button>
53
+ </form>
54
+ <pre id="form-output"></pre>
55
+ <script>
56
+ function handleSubmit(event) {
57
+ event.preventDefault();
58
+ const data = new FormData(event.target);
59
+ const output = {};
60
+ for (const [key, value] of data.entries()) {
61
+ if (!output[key]) output[key] = [];
62
+ output[key].push(value);
63
+ }
64
+ document.getElementById('form-output').textContent = JSON.stringify(output, null, 2);
65
+ }
66
+ </script>
49
67
  <script src="./built.js"></script>
50
68
  </body>
51
69
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucos_search_component",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "description": "Web Components for searching lucOS data",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -303,6 +303,37 @@ class LucosSearchComponent extends HTMLSpanElement {
303
303
  shadow.append(selector.nextElementSibling);
304
304
  }
305
305
  }
306
+ connectedCallback() {
307
+ const form = this.closest('form');
308
+ if (!form) return;
309
+ this._form = form;
310
+ this._formdataHandler = (event) => {
311
+ const selector = this.querySelector('select');
312
+ if (!selector || !selector.name) return;
313
+ const ts = selector.tomselect;
314
+ if (!ts) return;
315
+ const name = selector.name;
316
+ const values = ts.getValue();
317
+ const valueArray = Array.isArray(values) ? values : (values ? [values] : []);
318
+ // Remove the native select values so consumers only receive the structured pairs
319
+ event.formData.delete(name);
320
+ valueArray.forEach((id, idx) => {
321
+ const option = ts.options[id];
322
+ if (option) {
323
+ event.formData.append(`${name}[${idx}][uri]`, id);
324
+ event.formData.append(`${name}[${idx}][name]`, option.pref_label);
325
+ }
326
+ });
327
+ };
328
+ form.addEventListener('formdata', this._formdataHandler);
329
+ }
330
+ disconnectedCallback() {
331
+ if (this._form && this._formdataHandler) {
332
+ this._form.removeEventListener('formdata', this._formdataHandler);
333
+ this._form = null;
334
+ this._formdataHandler = null;
335
+ }
336
+ }
306
337
  get noLangOption() {
307
338
  const label = this.getAttribute("data-no-lang");
308
339
  if (!label) return null;