Skip to content

Commit

Permalink
[Fizz] Check for nullish values on ReactCustomFormAction (#26770)
Browse files Browse the repository at this point in the history
Usually we don't have to do this since we only set these in the loop but
the ReactCustomFormAction props are optional so they might be undefined.

Also moved it to a general type since it's a semi-public API.
  • Loading branch information
sebmarkbage authored May 3, 2023
1 parent b797282 commit fa7a447
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
33 changes: 12 additions & 21 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {ReactNodeList} from 'shared/ReactTypes';
import type {ReactNodeList, ReactCustomFormAction} from 'shared/ReactTypes';

import {
checkHtmlStringCoercion,
Expand Down Expand Up @@ -668,15 +668,6 @@ function pushStringAttribute(
}
}

type CustomFormAction = {
name?: string,
action?: string,
encType?: string,
method?: string,
target?: string,
data?: FormData,
};

function makeFormFieldPrefix(responseState: ResponseState): string {
// I'm just reusing this counter. It's not really the same namespace as "name".
// It could just be its own counter.
Expand Down Expand Up @@ -761,7 +752,7 @@ function pushFormActionAttribute(
);
}
}
const customAction: CustomFormAction = formAction.$$FORM_ACTION;
const customAction: ReactCustomFormAction = formAction.$$FORM_ACTION;
if (typeof customAction === 'function') {
// This action has a custom progressive enhancement form that can submit the form
// back to the server if it's invoked before hydration. Such as a Server Action.
Expand Down Expand Up @@ -794,19 +785,19 @@ function pushFormActionAttribute(
injectFormReplayingRuntime(responseState);
}
}
if (name !== null) {
if (name != null) {
pushAttribute(target, 'name', name);
}
if (formAction !== null) {
if (formAction != null) {
pushAttribute(target, 'formAction', formAction);
}
if (formEncType !== null) {
if (formEncType != null) {
pushAttribute(target, 'formEncType', formEncType);
}
if (formMethod !== null) {
if (formMethod != null) {
pushAttribute(target, 'formMethod', formMethod);
}
if (formTarget !== null) {
if (formTarget != null) {
pushAttribute(target, 'formTarget', formTarget);
}
return formData;
Expand Down Expand Up @@ -1455,7 +1446,7 @@ function pushStartForm(
);
}
}
const customAction: CustomFormAction = formAction.$$FORM_ACTION;
const customAction: ReactCustomFormAction = formAction.$$FORM_ACTION;
if (typeof customAction === 'function') {
// This action has a custom progressive enhancement form that can submit the form
// back to the server if it's invoked before hydration. Such as a Server Action.
Expand Down Expand Up @@ -1487,16 +1478,16 @@ function pushStartForm(
injectFormReplayingRuntime(responseState);
}
}
if (formAction !== null) {
if (formAction != null) {
pushAttribute(target, 'action', formAction);
}
if (formEncType !== null) {
if (formEncType != null) {
pushAttribute(target, 'encType', formEncType);
}
if (formMethod !== null) {
if (formMethod != null) {
pushAttribute(target, 'method', formMethod);
}
if (formTarget !== null) {
if (formTarget != null) {
pushAttribute(target, 'target', formTarget);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ export type StartTransitionOptions = {
};

export type Usable<T> = Thenable<T> | ReactContext<T>;

export type ReactCustomFormAction = {
name?: string,
action?: string,
encType?: string,
method?: string,
target?: string,
data?: null | FormData,
};

0 comments on commit fa7a447

Please sign in to comment.