@usefillo/dom 0.2.0 → 0.2.2

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
@@ -470,14 +470,22 @@ function fileUpload(context) {
470
470
  }
471
471
  return shell(field, context.error, wrap);
472
472
  }
473
+ var shuffleCache = /* @__PURE__ */ new Map();
473
474
  function displayOptions(field) {
474
475
  if (!field.shuffleOptions) return field.options;
475
- const options = [...field.options];
476
- for (let i = options.length - 1; i > 0; i--) {
477
- const j = Math.floor(Math.random() * (i + 1));
478
- [options[i], options[j]] = [options[j], options[i]];
476
+ const cacheKey = `${field.id}|${field.options.map((o) => o.id).join(",")}`;
477
+ let order = shuffleCache.get(cacheKey);
478
+ if (!order) {
479
+ order = field.options.map((o) => o.id);
480
+ for (let i = order.length - 1; i > 0; i--) {
481
+ const j = Math.floor(Math.random() * (i + 1));
482
+ [order[i], order[j]] = [order[j], order[i]];
483
+ }
484
+ shuffleCache.set(cacheKey, order);
479
485
  }
480
- return options;
486
+ const byId = new Map(field.options.map((o) => [o.id, o]));
487
+ const shuffled = order.flatMap((id) => byId.get(id) ?? []);
488
+ return shuffled.length === field.options.length ? shuffled : field.options;
481
489
  }
482
490
  var DEFAULT_COMPONENTS = {
483
491
  short_text: (ctx) => textInput("text", ctx),
@@ -582,9 +590,10 @@ var DomFormController = class {
582
590
  this.setSchema(schema, this.options.theme, this.options.formId);
583
591
  return;
584
592
  }
585
- if (!this.options.client || !this.options.formId) {
586
- throw new FilloError("Provide either `form`, or `client` with `formId`.", 0);
593
+ if (!this.options.formId) {
594
+ throw new FilloError("Provide either `form`, or a `formId`.", 0);
587
595
  }
596
+ this.options.client ??= createClient();
588
597
  this.render();
589
598
  const published = await this.options.client.getForm(this.options.formId);
590
599
  this.closed = Boolean(published.closed);
@@ -891,7 +900,7 @@ var FilloFormElement = class extends HTMLElement {
891
900
  const baseUrl = this.getAttribute("base-url");
892
901
  const key = this.getAttribute("publishable-key") ?? void 0;
893
902
  const formId = this.getAttribute("form-id") ?? void 0;
894
- const client = this.assignedClient ?? (baseUrl ? createClient({ baseUrl, key }) : void 0);
903
+ const client = this.assignedClient ?? (formId || key || baseUrl ? createClient({ baseUrl: baseUrl ?? void 0, key }) : void 0);
895
904
  this.instance = renderForm(this, {
896
905
  form: this.assignedForm,
897
906
  client,