@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.
@@ -127,13 +127,18 @@ interface CompilerHostElement {
127
127
  value: unknown;
128
128
  }[];
129
129
  children: readonly (CompilerHostElement | unknown)[];
130
+ /** Ids of the keyed row events this element of a host-block row handles. */
131
+ events?: readonly number[];
130
132
  /** Direct-child structure owned without creating another React fiber. */
131
133
  block?: CompilerHostBlock;
132
134
  }
133
135
  type CompilerKeyedRowElement = CompilerHostElement;
134
136
  interface CompilerKeyedRowEvent {
135
137
  name: string;
136
- /** Host-element path relative to the keyed row root. */
138
+ /**
139
+ * Host-element path relative to the keyed row root. Host-block rows omit it: mounting the row
140
+ * records each element's events from its descriptor.
141
+ */
137
142
  path?: readonly number[];
138
143
  invoke(item: unknown, index: number, event: React.SyntheticEvent): unknown;
139
144
  }
@@ -245,6 +250,8 @@ interface CompilerKeyedRowBinding {
245
250
  read(item: unknown, index: number): unknown;
246
251
  }
247
252
  interface CompilerKeyedRowsBlockProps {
253
+ /** Receives the container when the rows block is the component's root element. */
254
+ rootRef?: React.RefCallback<Element>;
248
255
  id: number;
249
256
  render(event: (item: unknown, index: number, eventId: number) => React.EventHandler<React.SyntheticEvent>, conditional: (item: unknown, index: number, conditionalId: number, render: (item: unknown, index: number) => React.ReactNode) => React.ReactNode): React.ReactElement;
250
257
  items(): Iterable<unknown> | null | undefined;
@@ -127,13 +127,18 @@ interface CompilerHostElement {
127
127
  value: unknown;
128
128
  }[];
129
129
  children: readonly (CompilerHostElement | unknown)[];
130
+ /** Ids of the keyed row events this element of a host-block row handles. */
131
+ events?: readonly number[];
130
132
  /** Direct-child structure owned without creating another React fiber. */
131
133
  block?: CompilerHostBlock;
132
134
  }
133
135
  type CompilerKeyedRowElement = CompilerHostElement;
134
136
  interface CompilerKeyedRowEvent {
135
137
  name: string;
136
- /** Host-element path relative to the keyed row root. */
138
+ /**
139
+ * Host-element path relative to the keyed row root. Host-block rows omit it: mounting the row
140
+ * records each element's events from its descriptor.
141
+ */
137
142
  path?: readonly number[];
138
143
  invoke(item: unknown, index: number, event: React.SyntheticEvent): unknown;
139
144
  }
@@ -245,6 +250,8 @@ interface CompilerKeyedRowBinding {
245
250
  read(item: unknown, index: number): unknown;
246
251
  }
247
252
  interface CompilerKeyedRowsBlockProps {
253
+ /** Receives the container when the rows block is the component's root element. */
254
+ rootRef?: React.RefCallback<Element>;
248
255
  id: number;
249
256
  render(event: (item: unknown, index: number, eventId: number) => React.EventHandler<React.SyntheticEvent>, conditional: (item: unknown, index: number, conditionalId: number, render: (item: unknown, index: number) => React.ReactNode) => React.ReactNode): React.ReactElement;
250
257
  items(): Iterable<unknown> | null | undefined;
@@ -1719,10 +1719,17 @@ function updateSelectValue(element, value) {
1719
1719
  function compilerAttributeName(name) {
1720
1720
  return name === "className" ? "class" : name === "htmlFor" ? "for" : name;
1721
1721
  }
1722
+ var compilerControlledValues = /* @__PURE__ */ new WeakMap();
1723
+ var compilerControlledChecked = /* @__PURE__ */ new WeakMap();
1724
+ function recordControlledField(fields, element, value) {
1725
+ if (value == null) fields.delete(element);
1726
+ else fields.set(element, value);
1727
+ }
1722
1728
  function updateAttribute(element, name, value) {
1723
1729
  const attributeName = compilerAttributeName(name);
1724
1730
  const stringifiesBoolean = attributeName.startsWith("data-") || attributeName.startsWith("aria-");
1725
1731
  if (name === "value" && (isInputElement(element) || isTextAreaElement(element))) {
1732
+ recordControlledField(compilerControlledValues, element, value);
1726
1733
  const nextValue = value === null || value === void 0 ? "" : String(value);
1727
1734
  if (element.value !== nextValue) element.value = nextValue;
1728
1735
  if (value != null && (isTextAreaElement(element) || isInputElement(element) && (element.type !== "number" || element.ownerDocument.activeElement !== element)) && element.defaultValue !== nextValue) {
@@ -1731,10 +1738,12 @@ function updateAttribute(element, name, value) {
1731
1738
  return;
1732
1739
  }
1733
1740
  if (name === "value" && isSelectElement(element)) {
1741
+ recordControlledField(compilerControlledValues, element, value);
1734
1742
  updateSelectValue(element, value);
1735
1743
  return;
1736
1744
  }
1737
1745
  if (name === "checked" && isInputElement(element)) {
1746
+ recordControlledField(compilerControlledChecked, element, value);
1738
1747
  element.checked = Boolean(value);
1739
1748
  return;
1740
1749
  }
@@ -2812,8 +2821,10 @@ function applyPreparedHostConditionalBindings(branch, instance) {
2812
2821
  }
2813
2822
  }
2814
2823
  var UNSET_HOST_CONDITIONAL_BINDING = /* @__PURE__ */ Symbol("unset host conditional binding");
2824
+ var compilerHostElementEvents = /* @__PURE__ */ new WeakMap();
2815
2825
  function mountCompilerHostTree(owner, element, descriptor, onFallback) {
2816
2826
  if (!matchesCompilerHostElement(element, descriptor)) return null;
2827
+ if (descriptor.events) compilerHostElementEvents.set(element, descriptor.events);
2817
2828
  if (descriptor.block?.kind === "conditional-ranges") {
2818
2829
  const controller = new CompilerNestedConditionalRanges(
2819
2830
  owner,
@@ -4441,8 +4452,11 @@ function reorderCompilerKeyedRows(root, instances, sequence, anchor) {
4441
4452
  restoreCompilerFocus(activeElement);
4442
4453
  }
4443
4454
  var RESOLVED_COMPILER_MICROTASK = Promise.resolve();
4455
+ var pendingCompilerMicrotasks = 0;
4444
4456
  function scheduleCompilerMicrotask(callback) {
4457
+ pendingCompilerMicrotasks += 1;
4445
4458
  void RESOLVED_COMPILER_MICROTASK.then(() => {
4459
+ pendingCompilerMicrotasks -= 1;
4446
4460
  try {
4447
4461
  callback();
4448
4462
  } catch (error) {
@@ -6139,7 +6153,23 @@ function createKeyedRowHostRuntime() {
6139
6153
  }
6140
6154
  };
6141
6155
  return {
6142
- mount: (element, descriptor, requestFallback) => mountCompilerHostTree(owner, element, descriptor, requestFallback)
6156
+ mount: (element, descriptor, requestFallback) => mountCompilerHostTree(owner, element, descriptor, requestFallback),
6157
+ matchEvents(row, target, name, events, matches) {
6158
+ const elements = [];
6159
+ let element = target.nodeType === 1 ? target : target.parentElement;
6160
+ while (element) {
6161
+ elements.push(element);
6162
+ element = element === row ? null : element.parentElement;
6163
+ }
6164
+ elements.forEach((element2, index) => {
6165
+ for (const id of compilerHostElementEvents.get(element2) || []) {
6166
+ const descriptor = events[id];
6167
+ if (descriptor?.name === name && descriptor.path === void 0) {
6168
+ matches.push({ descriptor, target: element2, depth: elements.length - 1 - index });
6169
+ }
6170
+ }
6171
+ });
6172
+ }
6143
6173
  };
6144
6174
  }
6145
6175
  function createKeyedRowsBlockComponentCore(owner, options = {}) {
@@ -6150,7 +6180,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6150
6180
  // here (onChange under React DOM) are rendered by React from the start.
6151
6181
  this.state = {
6152
6182
  fallback: Boolean(
6153
- this.props.hostBlocks && this.props.events?.length && !(this.props.delegateEvents && this.props.events.every((event) => event.path !== void 0))
6183
+ this.props.hostBlocks && this.props.events?.length && !this.props.delegateEvents
6154
6184
  )
6155
6185
  };
6156
6186
  this.root = null;
@@ -6172,6 +6202,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6172
6202
  this.elementIndexComplete = true;
6173
6203
  this.captureRoot = (root) => {
6174
6204
  this.root = root;
6205
+ this.currentProps.rootRef?.(root);
6175
6206
  };
6176
6207
  this.requestHostScopeFallback = () => {
6177
6208
  this.activateFallback();
@@ -6211,13 +6242,16 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6211
6242
  if (!(index in descriptors)) continue;
6212
6243
  const descriptor = descriptors[index];
6213
6244
  if (descriptor.name !== name || descriptor.path === void 0) continue;
6214
- const target = findCompilerHostTarget(instance.element, descriptor.path || []);
6245
+ const target = findCompilerHostTarget(instance.element, descriptor.path);
6215
6246
  if (target && (target === eventTarget || target.contains(eventTarget))) {
6216
- matches.push({ descriptor, target });
6247
+ matches.push({ descriptor, target, depth: descriptor.path.length });
6217
6248
  }
6218
6249
  }
6250
+ if (this.currentProps.hostBlocks) {
6251
+ options.hostBlocks?.matchEvents(instance.element, eventTarget, name, descriptors, matches);
6252
+ }
6219
6253
  matches.sort(
6220
- (left, right) => capture ? (left.descriptor.path?.length || 0) - (right.descriptor.path?.length || 0) : (right.descriptor.path?.length || 0) - (left.descriptor.path?.length || 0)
6254
+ (left, right) => capture ? left.depth - right.depth : right.depth - left.depth
6221
6255
  );
6222
6256
  for (const match of matches) {
6223
6257
  this.invokeDelegatedEvent(match.descriptor, instance, event, match.target);
@@ -6424,7 +6458,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6424
6458
  }
6425
6459
  usesDelegatedEvents(props = this.currentProps) {
6426
6460
  return Boolean(
6427
- props.delegateEvents && props.events?.length && props.events.every((event) => event.path !== void 0)
6461
+ props.delegateEvents && props.events?.length && // Host-block rows find an event without a path through the element that mounted it.
6462
+ (props.hostBlocks || props.events.every((event) => event.path !== void 0))
6428
6463
  );
6429
6464
  }
6430
6465
  rebuildElementIndex(instances) {
@@ -8476,7 +8511,9 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8476
8511
  this.propCellOffset = initialState.length;
8477
8512
  this.cells = [...initialState, ...initialProps].map((initialValue, index) => {
8478
8513
  let value = initialValue;
8479
- const pending = [];
8514
+ let pending = false;
8515
+ let pendingValue;
8516
+ let pendingError;
8480
8517
  return {
8481
8518
  get: () => {
8482
8519
  if (this.activeBinding && this.activeDependencies) {
@@ -8485,21 +8522,32 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8485
8522
  return value;
8486
8523
  },
8487
8524
  set: (next) => {
8488
- pending.push({ kind: "state", update: next });
8525
+ if (!pendingError) {
8526
+ const base = pending ? pendingValue : value;
8527
+ try {
8528
+ pendingValue = typeof next === "function" ? next(base) : next;
8529
+ } catch (error) {
8530
+ pendingValue = base;
8531
+ pendingError = { error };
8532
+ }
8533
+ pending = true;
8534
+ }
8489
8535
  this.scheduleBindingFlush(index);
8490
8536
  },
8491
8537
  replace: (next) => {
8492
- pending.push({ kind: "value", value: next });
8538
+ pendingValue = next;
8539
+ pending = true;
8493
8540
  },
8494
8541
  flush: () => {
8495
- if (pending.length === 0) return false;
8542
+ if (!pending) return false;
8496
8543
  const previous = value;
8497
- for (const next of pending.splice(0)) {
8498
- if (next.kind === "value") {
8499
- value = next.value;
8500
- } else {
8501
- value = typeof next.update === "function" ? next.update(value) : next.update;
8502
- }
8544
+ value = pendingValue;
8545
+ pending = false;
8546
+ pendingValue = void 0;
8547
+ if (pendingError) {
8548
+ const { error } = pendingError;
8549
+ pendingError = void 0;
8550
+ throw error;
8503
8551
  }
8504
8552
  return !Object.is(previous, value);
8505
8553
  }
@@ -8833,7 +8881,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8833
8881
  this.renderedElement = renderedElement;
8834
8882
  }
8835
8883
  };
8836
- if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges) {
8884
+ if (element.type === this.blockRuntime.KeyedRanges || element.type === this.blockRuntime.ConditionalRanges || element.type === this.blockRuntime.MixedRanges || element.type === this.blockRuntime.KeyedRows) {
8837
8885
  renderedElement = React.cloneElement(
8838
8886
  element,
8839
8887
  { rootRef: captureCommittedRoot }
@@ -8949,10 +8997,46 @@ function compiledRootUpdatesWithoutReact(displayName, props) {
8949
8997
  if (displayName !== "FarmCompiledKeyedRows") return false;
8950
8998
  const rows = props;
8951
8999
  const delegated = Boolean(
8952
- (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && rows.events.every((event) => event.path !== void 0)
9000
+ (rows.delegateEvents || rows.delegateEventsWithoutReact) && rows.events?.length && (rows.hostBlocks || rows.events.every((event) => event.path !== void 0))
8953
9001
  );
8954
9002
  return !rows.conditionals?.length && (!rows.events?.length || delegated);
8955
9003
  }
9004
+ function restoreCompilerControlledField(element) {
9005
+ if (compilerControlledValues.has(element)) {
9006
+ const value = compilerControlledValues.get(element);
9007
+ if (isSelectElement(element)) {
9008
+ updateSelectValue(element, value);
9009
+ } else if (isInputElement(element) || isTextAreaElement(element)) {
9010
+ const changed = element.type === "number" && typeof value === "number" ? value === 0 && element.value === "" || Number(element.value) !== value : element.value !== String(value);
9011
+ if (changed) element.value = String(value);
9012
+ }
9013
+ }
9014
+ if (isInputElement(element) && compilerControlledChecked.has(element)) {
9015
+ element.checked = Boolean(compilerControlledChecked.get(element));
9016
+ if (element.type === "radio" && element.name) {
9017
+ for (const radio of element.getRootNode().querySelectorAll(
9018
+ 'input[type="radio"]'
9019
+ )) {
9020
+ const other = radio;
9021
+ if (other !== element && other.name === element.name && other.form === element.form && compilerControlledChecked.has(other)) {
9022
+ other.checked = Boolean(compilerControlledChecked.get(other));
9023
+ }
9024
+ }
9025
+ }
9026
+ }
9027
+ }
9028
+ function restoreControlledFieldOnceSettled(element) {
9029
+ queueMicrotask(
9030
+ () => pendingCompilerMicrotasks > 0 ? restoreControlledFieldOnceSettled(element) : restoreCompilerControlledField(element)
9031
+ );
9032
+ }
9033
+ function scheduleControlledFieldRestore(event) {
9034
+ const target = event.target;
9035
+ if (target instanceof Element && event.type === compiledRootChangeEvent(target) && (compilerControlledValues.has(target) || compilerControlledChecked.has(target))) {
9036
+ restoreControlledFieldOnceSettled(target);
9037
+ }
9038
+ }
9039
+ var COMPILED_ROOT_CONTROLLED_FIELD_EVENTS = ["input", "change"];
8956
9040
  function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8957
9041
  let propagationStopped = false;
8958
9042
  const own = {
@@ -8962,6 +9046,7 @@ function toCompiledRootEvent(nativeEvent, currentTarget, type) {
8962
9046
  stopPropagation() {
8963
9047
  propagationStopped = true;
8964
9048
  nativeEvent.stopPropagation();
9049
+ scheduleControlledFieldRestore(nativeEvent);
8965
9050
  },
8966
9051
  isPropagationStopped: () => propagationStopped,
8967
9052
  isDefaultPrevented: () => nativeEvent.defaultPrevented,
@@ -9153,6 +9238,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9153
9238
  let reactRoot;
9154
9239
  let unmounted = false;
9155
9240
  const stopCompiled = () => {
9241
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9242
+ container.removeEventListener(type, scheduleControlledFieldRestore);
9243
+ }
9156
9244
  for (const instance of instances.slice().reverse()) {
9157
9245
  instance.componentWillUnmount?.();
9158
9246
  }
@@ -9200,6 +9288,9 @@ function createCompiledRoot(container, options, loadReactDOM) {
9200
9288
  container.replaceChildren(mount.fragment);
9201
9289
  instances = mount.instances;
9202
9290
  mode = "compiled";
9291
+ for (const type of COMPILED_ROOT_CONTROLLED_FIELD_EVENTS) {
9292
+ container.addEventListener(type, scheduleControlledFieldRestore);
9293
+ }
9203
9294
  for (const effect of mount.effects) effect();
9204
9295
  },
9205
9296
  unmount() {