@farm.js/react 0.1.0-beta.40 → 0.1.0-beta.42

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.
@@ -1725,10 +1725,17 @@ function updateSelectValue(element, value) {
1725
1725
  function compilerAttributeName(name) {
1726
1726
  return name === "className" ? "class" : name === "htmlFor" ? "for" : name;
1727
1727
  }
1728
+ var compilerControlledValues = /* @__PURE__ */ new WeakMap();
1729
+ var compilerControlledChecked = /* @__PURE__ */ new WeakMap();
1730
+ function recordControlledField(fields, element, value) {
1731
+ if (value == null) fields.delete(element);
1732
+ else fields.set(element, value);
1733
+ }
1728
1734
  function updateAttribute(element, name, value) {
1729
1735
  const attributeName = compilerAttributeName(name);
1730
1736
  const stringifiesBoolean = attributeName.startsWith("data-") || attributeName.startsWith("aria-");
1731
1737
  if (name === "value" && (isInputElement(element) || isTextAreaElement(element))) {
1738
+ recordControlledField(compilerControlledValues, element, value);
1732
1739
  const nextValue = value === null || value === void 0 ? "" : String(value);
1733
1740
  if (element.value !== nextValue) element.value = nextValue;
1734
1741
  if (value != null && (isTextAreaElement(element) || isInputElement(element) && (element.type !== "number" || element.ownerDocument.activeElement !== element)) && element.defaultValue !== nextValue) {
@@ -1737,10 +1744,12 @@ function updateAttribute(element, name, value) {
1737
1744
  return;
1738
1745
  }
1739
1746
  if (name === "value" && isSelectElement(element)) {
1747
+ recordControlledField(compilerControlledValues, element, value);
1740
1748
  updateSelectValue(element, value);
1741
1749
  return;
1742
1750
  }
1743
1751
  if (name === "checked" && isInputElement(element)) {
1752
+ recordControlledField(compilerControlledChecked, element, value);
1744
1753
  element.checked = Boolean(value);
1745
1754
  return;
1746
1755
  }
@@ -2818,8 +2827,10 @@ function applyPreparedHostConditionalBindings(branch, instance) {
2818
2827
  }
2819
2828
  }
2820
2829
  var UNSET_HOST_CONDITIONAL_BINDING = /* @__PURE__ */ Symbol("unset host conditional binding");
2830
+ var compilerHostElementEvents = /* @__PURE__ */ new WeakMap();
2821
2831
  function mountCompilerHostTree(owner, element, descriptor, onFallback) {
2822
2832
  if (!matchesCompilerHostElement(element, descriptor)) return null;
2833
+ if (descriptor.events) compilerHostElementEvents.set(element, descriptor.events);
2823
2834
  if (descriptor.block?.kind === "conditional-ranges") {
2824
2835
  const controller = new CompilerNestedConditionalRanges(
2825
2836
  owner,
@@ -4447,8 +4458,11 @@ function reorderCompilerKeyedRows(root, instances, sequence, anchor) {
4447
4458
  restoreCompilerFocus(activeElement);
4448
4459
  }
4449
4460
  var RESOLVED_COMPILER_MICROTASK = Promise.resolve();
4461
+ var pendingCompilerMicrotasks = 0;
4450
4462
  function scheduleCompilerMicrotask(callback) {
4463
+ pendingCompilerMicrotasks += 1;
4451
4464
  void RESOLVED_COMPILER_MICROTASK.then(() => {
4465
+ pendingCompilerMicrotasks -= 1;
4452
4466
  try {
4453
4467
  callback();
4454
4468
  } catch (error) {
@@ -6145,7 +6159,23 @@ function createKeyedRowHostRuntime() {
6145
6159
  }
6146
6160
  };
6147
6161
  return {
6148
- mount: (element, descriptor, requestFallback) => mountCompilerHostTree(owner, element, descriptor, requestFallback)
6162
+ mount: (element, descriptor, requestFallback) => mountCompilerHostTree(owner, element, descriptor, requestFallback),
6163
+ matchEvents(row, target, name, events, matches) {
6164
+ const elements = [];
6165
+ let element = target.nodeType === 1 ? target : target.parentElement;
6166
+ while (element) {
6167
+ elements.push(element);
6168
+ element = element === row ? null : element.parentElement;
6169
+ }
6170
+ elements.forEach((element2, index) => {
6171
+ for (const id of compilerHostElementEvents.get(element2) || []) {
6172
+ const descriptor = events[id];
6173
+ if (descriptor?.name === name && descriptor.path === void 0) {
6174
+ matches.push({ descriptor, target: element2, depth: elements.length - 1 - index });
6175
+ }
6176
+ }
6177
+ });
6178
+ }
6149
6179
  };
6150
6180
  }
6151
6181
  function createKeyedRowsBlockComponentCore(owner, options = {}) {
@@ -6156,7 +6186,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6156
6186
  // here (onChange under React DOM) are rendered by React from the start.
6157
6187
  this.state = {
6158
6188
  fallback: Boolean(
6159
- this.props.hostBlocks && this.props.events?.length && !(this.props.delegateEvents && this.props.events.every((event) => event.path !== void 0))
6189
+ this.props.hostBlocks && this.props.events?.length && !this.props.delegateEvents
6160
6190
  )
6161
6191
  };
6162
6192
  this.root = null;
@@ -6178,6 +6208,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6178
6208
  this.elementIndexComplete = true;
6179
6209
  this.captureRoot = (root) => {
6180
6210
  this.root = root;
6211
+ this.currentProps.rootRef?.(root);
6181
6212
  };
6182
6213
  this.requestHostScopeFallback = () => {
6183
6214
  this.activateFallback();
@@ -6217,13 +6248,16 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6217
6248
  if (!(index in descriptors)) continue;
6218
6249
  const descriptor = descriptors[index];
6219
6250
  if (descriptor.name !== name || descriptor.path === void 0) continue;
6220
- const target = findCompilerHostTarget(instance.element, descriptor.path || []);
6251
+ const target = findCompilerHostTarget(instance.element, descriptor.path);
6221
6252
  if (target && (target === eventTarget || target.contains(eventTarget))) {
6222
- matches.push({ descriptor, target });
6253
+ matches.push({ descriptor, target, depth: descriptor.path.length });
6223
6254
  }
6224
6255
  }
6256
+ if (this.currentProps.hostBlocks) {
6257
+ options.hostBlocks?.matchEvents(instance.element, eventTarget, name, descriptors, matches);
6258
+ }
6225
6259
  matches.sort(
6226
- (left, right) => capture ? (left.descriptor.path?.length || 0) - (right.descriptor.path?.length || 0) : (right.descriptor.path?.length || 0) - (left.descriptor.path?.length || 0)
6260
+ (left, right) => capture ? left.depth - right.depth : right.depth - left.depth
6227
6261
  );
6228
6262
  for (const match of matches) {
6229
6263
  this.invokeDelegatedEvent(match.descriptor, instance, event, match.target);
@@ -6430,7 +6464,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6430
6464
  }
6431
6465
  usesDelegatedEvents(props = this.currentProps) {
6432
6466
  return Boolean(
6433
- props.delegateEvents && props.events?.length && props.events.every((event) => event.path !== void 0)
6467
+ props.delegateEvents && props.events?.length && // Host-block rows find an event without a path through the element that mounted it.
6468
+ (props.hostBlocks || props.events.every((event) => event.path !== void 0))
6434
6469
  );
6435
6470
  }
6436
6471
  rebuildElementIndex(instances) {
@@ -8405,6 +8440,42 @@ var mixedRangesRuntimeFeature = {
8405
8440
  MixedRanges: createMixedRangesBlockComponent(owner)
8406
8441
  })
8407
8442
  };
8443
+ var compilerEffectStates = /* @__PURE__ */ new WeakMap();
8444
+ function runCompilerEffects(component, effects) {
8445
+ let states = compilerEffectStates.get(component);
8446
+ if (!effects) {
8447
+ const mounted = states?.splice(0) ?? [];
8448
+ for (const layout of [true, false]) {
8449
+ for (const state of mounted) if (state?.layout === layout) state.cleanup?.();
8450
+ }
8451
+ return;
8452
+ }
8453
+ if (!states) compilerEffectStates.set(component, states = []);
8454
+ const changed = [];
8455
+ for (let index = 0; index < effects.length; index += 1) {
8456
+ const dependencies = effects[index][1];
8457
+ const previous = states[index];
8458
+ if (!previous || !dependencies || !previous.dependencies || dependencies.length !== previous.dependencies.length || dependencies.some((value, position) => !Object.is(value, previous.dependencies[position]))) {
8459
+ changed.push(index);
8460
+ }
8461
+ }
8462
+ for (const layout of [true, false]) {
8463
+ const phase = changed.filter((index) => Boolean(effects[index][2]) === layout);
8464
+ for (const index of phase) states[index]?.cleanup?.();
8465
+ for (const index of phase) {
8466
+ const cleanup = effects[index][0]();
8467
+ states[index] = {
8468
+ dependencies: effects[index][1],
8469
+ cleanup: typeof cleanup === "function" ? cleanup : void 0,
8470
+ layout
8471
+ };
8472
+ }
8473
+ }
8474
+ }
8475
+ var effectsRuntimeFeature = {
8476
+ name: "effects",
8477
+ create: () => ({ effects: runCompilerEffects })
8478
+ };
8408
8479
  var componentRuntimeFeature = {
8409
8480
  name: "component",
8410
8481
  create: (owner) => ({
@@ -8482,7 +8553,9 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8482
8553
  this.propCellOffset = initialState.length;
8483
8554
  this.cells = [...initialState, ...initialProps].map((initialValue, index) => {
8484
8555
  let value = initialValue;
8485
- const pending = [];
8556
+ let pending = false;
8557
+ let pendingValue;
8558
+ let pendingError;
8486
8559
  return {
8487
8560
  get: () => {
8488
8561
  if (this.activeBinding && this.activeDependencies) {
@@ -8491,21 +8564,32 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8491
8564
  return value;
8492
8565
  },
8493
8566
  set: (next) => {
8494
- pending.push({ kind: "state", update: next });
8567
+ if (!pendingError) {
8568
+ const base = pending ? pendingValue : value;
8569
+ try {
8570
+ pendingValue = typeof next === "function" ? next(base) : next;
8571
+ } catch (error) {
8572
+ pendingValue = base;
8573
+ pendingError = { error };
8574
+ }
8575
+ pending = true;
8576
+ }
8495
8577
  this.scheduleBindingFlush(index);
8496
8578
  },
8497
8579
  replace: (next) => {
8498
- pending.push({ kind: "value", value: next });
8580
+ pendingValue = next;
8581
+ pending = true;
8499
8582
  },
8500
8583
  flush: () => {
8501
- if (pending.length === 0) return false;
8584
+ if (!pending) return false;
8502
8585
  const previous = value;
8503
- for (const next of pending.splice(0)) {
8504
- if (next.kind === "value") {
8505
- value = next.value;
8506
- } else {
8507
- value = typeof next.update === "function" ? next.update(value) : next.update;
8508
- }
8586
+ value = pendingValue;
8587
+ pending = false;
8588
+ pendingValue = void 0;
8589
+ if (pendingError) {
8590
+ const { error } = pendingError;
8591
+ pendingError = void 0;
8592
+ throw error;
8509
8593
  }
8510
8594
  return !Object.is(previous, value);
8511
8595
  }
@@ -8517,7 +8601,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8517
8601
  getDefinitionVersion: () => definitionReference.current
8518
8602
  };
8519
8603
  const blockRuntime = {
8520
- target: (id) => this.bindingTarget(id)
8604
+ target: (id, ref) => this.bindingTarget(id, ref)
8521
8605
  };
8522
8606
  for (const feature of runtimeFeatures) {
8523
8607
  Object.assign(blockRuntime, feature.create(featureOwner));
@@ -8617,7 +8701,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8617
8701
  this.bindingValues.set(binding, this.bindingValue(binding, value));
8618
8702
  }
8619
8703
  }
8620
- bindingTarget(id) {
8704
+ bindingTarget(id, ref) {
8621
8705
  const existing = this.bindingTargetRefs.get(id);
8622
8706
  if (existing) return existing;
8623
8707
  const capture = (element) => {
@@ -8626,6 +8710,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8626
8710
  } else {
8627
8711
  this.bindingTargets.delete(id);
8628
8712
  }
8713
+ if (ref) ref.current = element;
8629
8714
  };
8630
8715
  this.bindingTargetRefs.set(id, capture);
8631
8716
  return capture;
@@ -8642,11 +8727,20 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8642
8727
  direction: active.selectionDirection || "none"
8643
8728
  };
8644
8729
  }
8645
- restoreInputSelection(snapshot) {
8646
- if (!snapshot || !snapshot.element.isConnected || snapshot.element.ownerDocument.activeElement !== snapshot.element) {
8647
- return;
8730
+ /**
8731
+ * Runs once compiled DOM updates have committed: restores the focused field's selection, then
8732
+ * runs the effects.
8733
+ */
8734
+ afterCommit(snapshot) {
8735
+ if (snapshot && snapshot.element.isConnected && snapshot.element.ownerDocument.activeElement === snapshot.element) {
8736
+ snapshot.element.setSelectionRange(snapshot.start, snapshot.end, snapshot.direction);
8737
+ }
8738
+ if (this.mounted) {
8739
+ this.blockRuntime.effects?.(
8740
+ this,
8741
+ definitionReference.current.effects?.(this.props, this.cells)
8742
+ );
8648
8743
  }
8649
- snapshot.element.setSelectionRange(snapshot.start, snapshot.end, snapshot.direction);
8650
8744
  }
8651
8745
  setBlockRoot(id, root, expectedRoot) {
8652
8746
  const previous = this.blockRoots.get(id);
@@ -8677,6 +8771,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8677
8771
  this.flushBindingUpdates();
8678
8772
  });
8679
8773
  }
8774
+ /** Returns true when it committed an update, after which it has run the effects. */
8680
8775
  flushBindingUpdates() {
8681
8776
  const inputSelection = this.inputSelection;
8682
8777
  this.inputSelection = null;
@@ -8723,15 +8818,16 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8723
8818
  for (const refresh of blockRefreshes) {
8724
8819
  refresh(() => {
8725
8820
  pendingBlockCommits -= 1;
8726
- if (pendingBlockCommits === 0) this.restoreInputSelection(inputSelection);
8821
+ if (pendingBlockCommits === 0) this.afterCommit(inputSelection);
8727
8822
  }, dirty);
8728
8823
  }
8729
- if (blockRefreshes.length === 0) this.restoreInputSelection(inputSelection);
8824
+ if (blockRefreshes.length === 0) this.afterCommit(inputSelection);
8730
8825
  } catch (error) {
8731
8826
  this.bindingError = error;
8732
8827
  this.hasBindingError = true;
8733
8828
  this.forceUpdate();
8734
8829
  }
8830
+ return true;
8735
8831
  }
8736
8832
  applyBinding(binding, force = false) {
8737
8833
  if (!this.root || binding.kind === "block") return;
@@ -8798,9 +8894,10 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8798
8894
  this.applyBinding(binding, true);
8799
8895
  }
8800
8896
  }
8801
- if (this.dirtyState.size) this.flushBindingUpdates();
8897
+ if (!(this.dirtyState.size && this.flushBindingUpdates())) this.afterCommit(null);
8802
8898
  }
8803
8899
  componentWillUnmount() {
8900
+ this.blockRuntime.effects?.(this);
8804
8901
  this.mounted = false;
8805
8902
  this.inputSelection = null;
8806
8903
  this.blockRefreshListeners.clear();
@@ -8839,7 +8936,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8839
8936
  this.renderedElement = renderedElement;
8840
8937
  }
8841
8938
  };
8842
- if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges) {
8939
+ if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges || element.type === this.blockRuntime.KeyedRows) {
8843
8940
  renderedElement = React__default.default.cloneElement(
8844
8941
  element,
8845
8942
  { rootRef: captureCommittedRoot }
@@ -8867,7 +8964,8 @@ var COMPLETE_RUNTIME_FEATURES = [
8867
8964
  keyedRowsCompleteCompatibilityRuntimeFeature,
8868
8965
  keyedRangesRuntimeFeature,
8869
8966
  mixedRangesRuntimeFeature,
8870
- componentRuntimeFeature
8967
+ componentRuntimeFeature,
8968
+ effectsRuntimeFeature
8871
8969
  ];
8872
8970
  function createCompiledComponent(definition) {
8873
8971
  return createCompiledComponentWithFeatures(definition, COMPLETE_RUNTIME_FEATURES);
@@ -8955,10 +9053,46 @@ function compiledRootUpdatesWithoutReact(displayName, props) {
8955
9053
  if (displayName !== "FarmCompiledKeyedRows") return false;
8956
9054
  const rows = props;
8957
9055
  const delegated = Boolean(
8958
- (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && rows.events.every((event) => event.path !== void 0)
9056
+ (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && (rows.hostBlocks || rows.events.every((event) => event.path !== void 0))
8959
9057
  );
8960
9058
  return !rows.conditionals?.length && (!rows.events?.length || delegated);
8961
9059
  }
9060
+ function restoreCompilerControlledField(element) {
9061
+ if (compilerControlledValues.has(element)) {
9062
+ const value = compilerControlledValues.get(element);
9063
+ if (isSelectElement(element)) {
9064
+ updateSelectValue(element, value);
9065
+ } else if (isInputElement(element) || isTextAreaElement(element)) {
9066
+ const changed = element.type === "number" && typeof value === "number" ? value === 0 && element.value === "" || Number(element.value) !== value : element.value !== String(value);
9067
+ if (changed) element.value = String(value);
9068
+ }
9069
+ }
9070
+ if (isInputElement(element) && compilerControlledChecked.has(element)) {
9071
+ element.checked = Boolean(compilerControlledChecked.get(element));
9072
+ if (element.type === "radio" && element.name) {
9073
+ for (const radio of element.getRootNode().querySelectorAll(
9074
+ 'input[type="radio"]'
9075
+ )) {
9076
+ const other = radio;
9077
+ if (other !== element && other.name === element.name && other.form === element.form && compilerControlledChecked.has(other)) {
9078
+ other.checked = Boolean(compilerControlledChecked.get(other));
9079
+ }
9080
+ }
9081
+ }
9082
+ }
9083
+ }
9084
+ function restoreControlledFieldOnceSettled(element) {
9085
+ queueMicrotask(
9086
+ () => pendingCompilerMicrotasks > 0 ? restoreControlledFieldOnceSettled(element) : restoreCompilerControlledField(element)
9087
+ );
9088
+ }
9089
+ function scheduleControlledFieldRestore(event) {
9090
+ const target = event.target;
9091
+ if (target instanceof Element && event.type === compiledRootChangeEvent(target) && (compilerControlledValues.has(target) || compilerControlledChecked.has(target))) {
9092
+ restoreControlledFieldOnceSettled(target);
9093
+ }
9094
+ }
9095
+ var COMPILED_ROOT_CONTROLLED_FIELD_EVENTS = ["input", "change"];
8962
9096
  function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8963
9097
  let propagationStopped = false;
8964
9098
  const own = {
@@ -8968,6 +9102,7 @@ function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8968
9102
  stopPropagation() {
8969
9103
  propagationStopped = true;
8970
9104
  nativeEvent.stopPropagation();
9105
+ scheduleControlledFieldRestore(nativeEvent);
8971
9106
  },
8972
9107
  isPropagationStopped: () => propagationStopped,
8973
9108
  isDefaultPrevented: () => nativeEvent.defaultPrevented,
@@ -9159,6 +9294,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9159
9294
  let reactRoot;
9160
9295
  let unmounted = false;
9161
9296
  const stopCompiled = () => {
9297
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9298
+ container.removeEventListener(type, scheduleControlledFieldRestore);
9299
+ }
9162
9300
  for (const instance of instances.slice().reverse()) {
9163
9301
  instance.componentWillUnmount?.();
9164
9302
  }
@@ -9206,6 +9344,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9206
9344
  container.replaceChildren(mount.fragment);
9207
9345
  instances = mount.instances;
9208
9346
  mode = "compiled";
9347
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9348
+ container.addEventListener(type, scheduleControlledFieldRestore);
9349
+ }
9209
9350
  for (const effect of mount.effects) effect();
9210
9351
  },
9211
9352
  unmount() {
@@ -9252,6 +9393,7 @@ exports.createCompilerKeyedArrayStructuralSort = createCompilerKeyedArrayStructu
9252
9393
  exports.createCompilerKeyedArrayWindowReplace = createCompilerKeyedArrayWindowReplace;
9253
9394
  exports.createCompilerKeyedCollectionUpdate = createCompilerKeyedCollectionUpdate;
9254
9395
  exports.createCompilerKeyedMapUpdate = createCompilerKeyedMapUpdate;
9396
+ exports.effectsRuntimeFeature = effectsRuntimeFeature;
9255
9397
  exports.finalizeCompilerKeyedArrayMappedStructuralUpdate = finalizeCompilerKeyedArrayMappedStructuralUpdate;
9256
9398
  exports.hostConditionalRuntimeFeature = hostConditionalRuntimeFeature;
9257
9399
  exports.keyedListRuntimeFeature = keyedListRuntimeFeature;