mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Scaffolding for requestFormReset API (#28808)
Based on: - #28804 --- 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 will be explained in the next PR.
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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,
|
||||
@@ -1951,6 +1953,23 @@ function flushSyncWork() {
|
||||
}
|
||||
}
|
||||
|
||||
function requestFormReset(form: HTMLFormElement) {
|
||||
const formInst = getInstanceFromNodeDOMTree(form);
|
||||
if (
|
||||
formInst !== null &&
|
||||
formInst.tag === HostComponent &&
|
||||
formInst.type === 'form'
|
||||
) {
|
||||
requestFormResetOnFiber(formInst);
|
||||
} else {
|
||||
// This form was either not rendered by this React renderer (or it's an
|
||||
// invalid type). Try the next one.
|
||||
//
|
||||
// The last implementation in the sequence will throw an error.
|
||||
previousDispatcher.r(/* requestFormReset */ form);
|
||||
}
|
||||
}
|
||||
|
||||
// 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'
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ export {
|
||||
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
prefetchDNS,
|
||||
preconnect,
|
||||
preload,
|
||||
|
||||
@@ -17,6 +17,7 @@ export {
|
||||
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
prefetchDNS,
|
||||
preconnect,
|
||||
preload,
|
||||
|
||||
Vendored
+1
@@ -20,6 +20,7 @@ export {
|
||||
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
prefetchDNS,
|
||||
preconnect,
|
||||
preload,
|
||||
|
||||
@@ -16,6 +16,7 @@ export {
|
||||
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
prefetchDNS,
|
||||
preconnect,
|
||||
preload,
|
||||
|
||||
@@ -16,6 +16,7 @@ export {
|
||||
unstable_batchedUpdates,
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
prefetchDNS,
|
||||
preconnect,
|
||||
preload,
|
||||
|
||||
@@ -29,8 +29,16 @@ export type ReactDOMInternalsDev = ReactDOMInternals & {
|
||||
|
||||
function noop() {}
|
||||
|
||||
function requestFormReset(element: HTMLFormElement) {
|
||||
throw new Error(
|
||||
'Invalid form element. requestFormReset must be passed a form that was ' +
|
||||
'rendered by React.',
|
||||
);
|
||||
}
|
||||
|
||||
const DefaultDispatcher: HostDispatcher = {
|
||||
f /* flushSyncWork */: noop,
|
||||
r /* requestFormReset */: requestFormReset,
|
||||
D /* prefetchDNS */: noop,
|
||||
C /* preconnect */: noop,
|
||||
L /* preload */: noop,
|
||||
|
||||
@@ -27,6 +27,7 @@ function noop() {}
|
||||
|
||||
const DefaultDispatcher: HostDispatcher = {
|
||||
f /* flushSyncWork */: noop,
|
||||
r /* requestFormReset */: noop,
|
||||
D /* prefetchDNS */: noop,
|
||||
C /* preconnect */: noop,
|
||||
L /* preload */: noop,
|
||||
|
||||
+1
@@ -48,6 +48,7 @@ export {
|
||||
export {
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
} from 'react-dom-bindings/src/shared/ReactDOMFormActions';
|
||||
|
||||
if (__DEV__) {
|
||||
|
||||
+1
@@ -48,6 +48,7 @@ export {
|
||||
export {
|
||||
useFormStatus,
|
||||
useFormState,
|
||||
requestFormReset,
|
||||
} from 'react-dom-bindings/src/shared/ReactDOMFormActions';
|
||||
|
||||
if (__DEV__) {
|
||||
|
||||
@@ -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 */: (
|
||||
|
||||
+7
-2
@@ -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__) {
|
||||
|
||||
@@ -506,5 +506,6 @@
|
||||
"518": "Saw multiple hydration diff roots in a pass. This is a bug in React.",
|
||||
"519": "Hydration Mismatch Exception: This is not a real error, and should not leak into userspace. If you're seeing this, it's likely a bug in React.",
|
||||
"520": "There was an error during concurrent rendering but React was able to recover by instead synchronously rendering the entire root.",
|
||||
"521": "flushSyncWork should not be called from builds that support legacy mode. This is a bug in React."
|
||||
"521": "flushSyncWork should not be called from builds that support legacy mode. This is a bug in React.",
|
||||
"522": "Invalid form element. requestFormReset must be passed a form that was rendered by React."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user