Skip to content

Commit

Permalink
Scaffolding for requestFormReset API
Browse files Browse the repository at this point in the history
This sets adds a new ReactDOM export called requestFormReset, including
setting up the export and creating a method on the internal ReactDOM
dispatcher. It does not yet add any implementation.

Doing this in its own commit for review purposes.

The API itself is explained in the next commit.
  • Loading branch information
acdlite committed Apr 10, 2024
1 parent 41e6173 commit c632ed3
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem';
import {validateLinkPropsForStyleResource} from '../shared/ReactDOMResourceValidation';
import escapeSelectorAttributeValueInsideDoubleQuotes from './escapeSelectorAttributeValueInsideDoubleQuotes';
import {flushSyncWork as flushSyncWorkOnAllRoots} from 'react-reconciler/src/ReactFiberWorkLoop';
import {requestFormReset as requestFormResetOnFiber} from 'react-reconciler/src/ReactFiberHooks';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';

Expand Down Expand Up @@ -1928,6 +1929,7 @@ ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
f /* flushSyncWork */: disableLegacyMode
? flushSyncWork
: previousDispatcher.f /* flushSyncWork */,
r: requestFormReset,
D /* prefetchDNS */: prefetchDNS,
C /* preconnect */: preconnect,
L /* preload */: preload,
Expand All @@ -1951,6 +1953,17 @@ function flushSyncWork() {
}
}

function requestFormReset(form: HTMLFormElement) {
previousDispatcher.r(/* requestFormReset */ form);
const formInst = getInstanceFromNodeDOMTree(form);
if (formInst !== null) {
requestFormResetOnFiber(formInst);
} else {
// TODO: What if none of the dispatchers find a matching form instance?
// Should we detect this in dev and warn?
}
}

// We expect this to get inlined. It is a function mostly to communicate the special nature of
// how we resolve the HoistableRoot for ReactDOM.pre*() methods. Because we support calling
// these methods outside of render there is no way to know which Document or ShadowRoot is 'scoped'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const previousDispatcher =
ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
f /* flushSyncWork */: previousDispatcher.f /* flushSyncWork */,
r /* requestFormReset */: previousDispatcher.r /* requestFormReset */,
D /* prefetchDNS */: prefetchDNS,
C /* preconnect */: preconnect,
L /* preload */: preload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const previousDispatcher =
ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
f /* flushSyncWork */: previousDispatcher.f /* flushSyncWork */,
r /* requestFormReset */: previousDispatcher.r /* requestFormReset */,
D /* prefetchDNS */: prefetchDNS,
C /* preconnect */: preconnect,
L /* preload */: preload,
Expand Down
6 changes: 6 additions & 0 deletions packages/react-dom-bindings/src/shared/ReactDOMFormActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {Awaited} from 'shared/ReactTypes';

import {enableAsyncActions} from 'shared/ReactFeatureFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';

type FormStatusNotPending = {|
pending: false,
Expand Down Expand Up @@ -87,3 +88,8 @@ export function useFormState<S, P>(
return dispatcher.useFormState(action, initialState, permalink);
}
}

export function requestFormReset(form: HTMLFormElement) {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.r(/* requestFormReset */ form);
}
1 change: 1 addition & 0 deletions packages/react-dom/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
unstable_batchedUpdates,
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/ReactDOMSharedInternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function noop() {}

const DefaultDispatcher: HostDispatcher = {
f /* flushSyncWork */: noop,
r /* requestFormReset */: noop,
D /* prefetchDNS */: noop,
C /* preconnect */: noop,
L /* preload */: noop,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/ReactDOMSharedInternalsFB.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function noop() {}

const DefaultDispatcher: HostDispatcher = {
f /* flushSyncWork */: noop,
r /* requestFormReset */: noop,
D /* prefetchDNS */: noop,
C /* preconnect */: noop,
L /* preload */: noop,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
export {
useFormStatus,
useFormState,
requestFormReset,
} from 'react-dom-bindings/src/shared/ReactDOMFormActions';

if (__DEV__) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/client/ReactDOMFB.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
export {
useFormStatus,
useFormState,
requestFormReset,
} from 'react-dom-bindings/src/shared/ReactDOMFormActions';

if (__DEV__) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/shared/ReactDOMTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export type PreinitModuleScriptOptions = {

export type HostDispatcher = {
f /* flushSyncWork */: () => boolean | void,
r /* requestFormReset */: (form: HTMLFormElement) => void,
D /* prefetchDNS */: (href: string) => void,
C /* preconnect */: (href: string, crossOrigin?: ?CrossOriginEnum) => void,
L /* preload */: (
Expand Down
9 changes: 7 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3008,13 +3008,18 @@ export function startHostTransition<F>(
// once more of this function is implemented.
() => {
// Automatically reset the form when the action completes.
requestFormReset(formFiber);
requestFormResetImpl(formFiber);
return callback(formData);
},
);
}

function requestFormReset(formFiber: Fiber) {
export function requestFormReset(formFiber: Fiber) {
// TODO: Not yet implemented. Need to upgrade the fiber to be stateful
// before scheduling the form reset.
}

function requestFormResetImpl(formFiber: Fiber) {
const transition = requestCurrentTransition();

if (__DEV__) {
Expand Down

0 comments on commit c632ed3

Please sign in to comment.