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

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) {
@@ -8482,7 +8517,9 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8482
8517
  this.propCellOffset = initialState.length;
8483
8518
  this.cells = [...initialState, ...initialProps].map((initialValue, index) => {
8484
8519
  let value = initialValue;
8485
- const pending = [];
8520
+ let pending = false;
8521
+ let pendingValue;
8522
+ let pendingError;
8486
8523
  return {
8487
8524
  get: () => {
8488
8525
  if (this.activeBinding && this.activeDependencies) {
@@ -8491,21 +8528,32 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8491
8528
  return value;
8492
8529
  },
8493
8530
  set: (next) => {
8494
- pending.push({ kind: "state", update: next });
8531
+ if (!pendingError) {
8532
+ const base = pending ? pendingValue : value;
8533
+ try {
8534
+ pendingValue = typeof next === "function" ? next(base) : next;
8535
+ } catch (error) {
8536
+ pendingValue = base;
8537
+ pendingError = { error };
8538
+ }
8539
+ pending = true;
8540
+ }
8495
8541
  this.scheduleBindingFlush(index);
8496
8542
  },
8497
8543
  replace: (next) => {
8498
- pending.push({ kind: "value", value: next });
8544
+ pendingValue = next;
8545
+ pending = true;
8499
8546
  },
8500
8547
  flush: () => {
8501
- if (pending.length === 0) return false;
8548
+ if (!pending) return false;
8502
8549
  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
- }
8550
+ value = pendingValue;
8551
+ pending = false;
8552
+ pendingValue = void 0;
8553
+ if (pendingError) {
8554
+ const { error } = pendingError;
8555
+ pendingError = void 0;
8556
+ throw error;
8509
8557
  }
8510
8558
  return !Object.is(previous, value);
8511
8559
  }
@@ -8839,7 +8887,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8839
8887
  this.renderedElement = renderedElement;
8840
8888
  }
8841
8889
  };
8842
- if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges) {
8890
+ if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges || element.type === this.blockRuntime.KeyedRows) {
8843
8891
  renderedElement = React__default.default.cloneElement(
8844
8892
  element,
8845
8893
  { rootRef: captureCommittedRoot }
@@ -8955,10 +9003,46 @@ function compiledRootUpdatesWithoutReact(displayName, props) {
8955
9003
  if (displayName !== "FarmCompiledKeyedRows") return false;
8956
9004
  const rows = props;
8957
9005
  const delegated = Boolean(
8958
- (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && rows.events.every((event) => event.path !== void 0)
9006
+ (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && (rows.hostBlocks || rows.events.every((event) => event.path !== void 0))
8959
9007
  );
8960
9008
  return !rows.conditionals?.length && (!rows.events?.length || delegated);
8961
9009
  }
9010
+ function restoreCompilerControlledField(element) {
9011
+ if (compilerControlledValues.has(element)) {
9012
+ const value = compilerControlledValues.get(element);
9013
+ if (isSelectElement(element)) {
9014
+ updateSelectValue(element, value);
9015
+ } else if (isInputElement(element) || isTextAreaElement(element)) {
9016
+ const changed = element.type === "number" && typeof value === "number" ? value === 0 && element.value === "" || Number(element.value) !== value : element.value !== String(value);
9017
+ if (changed) element.value = String(value);
9018
+ }
9019
+ }
9020
+ if (isInputElement(element) && compilerControlledChecked.has(element)) {
9021
+ element.checked = Boolean(compilerControlledChecked.get(element));
9022
+ if (element.type === "radio" && element.name) {
9023
+ for (const radio of element.getRootNode().querySelectorAll(
9024
+ 'input[type="radio"]'
9025
+ )) {
9026
+ const other = radio;
9027
+ if (other !== element && other.name === element.name && other.form === element.form && compilerControlledChecked.has(other)) {
9028
+ other.checked = Boolean(compilerControlledChecked.get(other));
9029
+ }
9030
+ }
9031
+ }
9032
+ }
9033
+ }
9034
+ function restoreControlledFieldOnceSettled(element) {
9035
+ queueMicrotask(
9036
+ () => pendingCompilerMicrotasks > 0 ? restoreControlledFieldOnceSettled(element) : restoreCompilerControlledField(element)
9037
+ );
9038
+ }
9039
+ function scheduleControlledFieldRestore(event) {
9040
+ const target = event.target;
9041
+ if (target instanceof Element && event.type === compiledRootChangeEvent(target) && (compilerControlledValues.has(target) || compilerControlledChecked.has(target))) {
9042
+ restoreControlledFieldOnceSettled(target);
9043
+ }
9044
+ }
9045
+ var COMPILED_ROOT_CONTROLLED_FIELD_EVENTS = ["input", "change"];
8962
9046
  function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8963
9047
  let propagationStopped = false;
8964
9048
  const own = {
@@ -8968,6 +9052,7 @@ function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8968
9052
  stopPropagation() {
8969
9053
  propagationStopped = true;
8970
9054
  nativeEvent.stopPropagation();
9055
+ scheduleControlledFieldRestore(nativeEvent);
8971
9056
  },
8972
9057
  isPropagationStopped: () => propagationStopped,
8973
9058
  isDefaultPrevented: () => nativeEvent.defaultPrevented,
@@ -9159,6 +9244,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9159
9244
  let reactRoot;
9160
9245
  let unmounted = false;
9161
9246
  const stopCompiled = () => {
9247
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9248
+ container.removeEventListener(type, scheduleControlledFieldRestore);
9249
+ }
9162
9250
  for (const instance of instances.slice().reverse()) {
9163
9251
  instance.componentWillUnmount?.();
9164
9252
  }
@@ -9206,6 +9294,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9206
9294
  container.replaceChildren(mount.fragment);
9207
9295
  instances = mount.instances;
9208
9296
  mode = "compiled";
9297
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9298
+ container.addEventListener(type, scheduleControlledFieldRestore);
9299
+ }
9209
9300
  for (const effect of mount.effects) effect();
9210
9301
  },
9211
9302
  unmount() {