react-form-action
Version:
State management helpers for the react form actions.
109 lines (102 loc) • 5.37 kB
text/typescript
import * as React$1 from 'react';
import React__default, { PropsWithChildren, JSX } from 'react';
import { F as FormAction, A as ActionState, I as InitialState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DjhozgiG.mjs';
import { $ZodErrorTree } from 'zod/v4/core';
type ActionProps<Data, Error, ValidationError, Arguments extends unknown[] = []> = PropsWithChildren<Arguments extends [unknown, ...unknown[]] ? {
action: FormAction<Data, Error, ValidationError, FormData, Arguments>;
args: Arguments;
initialData: Data;
permalink?: string;
} : {
action: FormAction<Data, Error, ValidationError, FormData, Arguments>;
initialData: Data;
args?: undefined;
permalink?: string;
}>;
type ActionStatusFlags<T extends ActionState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
isInitial: T extends "initial" ? true : false;
isInvalid: T extends "invalid" ? true : false;
isFailure: T extends "failure" ? true : false;
isSuccess: T extends "success" ? true : false;
};
type ActionContextState<T extends ActionState<unknown, unknown, unknown>> = T & ActionStatusFlags<T["type"]> & {
/**
* The dispatch function returned from React.useActionState()
*/
action: (payload: FormData) => void;
isPending: boolean;
};
/**
* NOTE: ActionContextState<ActionState<...>> would not allow discriminate the union inside of the ActionState.
*/
type SpreadActionContext<Data = unknown, Error = unknown, ValidationError = unknown> = ActionContextState<InitialState<Data>> | ActionContextState<InvalidState<ValidationError>> | ActionContextState<FailureState<Error>> | ActionContextState<SuccessState<Data>>;
/**
* A hook to consume the form action state from the context.
*/
declare function useActionContext<Data, Error, ValidationError, Args extends unknown[] = []>(action?: FormAction<Data, Error, ValidationError, FormData, Args>): SpreadActionContext<Data, Error, ValidationError>;
declare function Action<Data, Error, ValidationError, Args extends unknown[] = []>({ children, action: formAction, initialData, args, permalink, }: ActionProps<Data, Error, ValidationError, Args>): React__default.JSX.Element;
type FormProps = Omit<JSX.IntrinsicElements["form"], "action"> & {
/**
* Opt-in into automatic form reset by using the form "action" prop.
* By default, the onSubmit with a custom transition which opts-out of the implicit form reset.
* See for more. https://github.com/facebook/react/issues/29034
* @default false
*/
autoReset?: boolean;
};
declare function Form({ autoReset, ...props }: FormProps): JSX.Element;
declare const SEPARATOR: ".";
type Paths<Shape extends $ZodErrorTree<object>, Props = Required<NonNullable<Shape["properties"]>>> = {
[k in keyof Props]: k extends string ? NonNullable<Props[k]> extends $ZodErrorTree<object> ? "properties" extends keyof NonNullable<Props[k]> ? `${k}${typeof SEPARATOR}${Paths<NonNullable<Props[k]>>}` : k : k : never;
}[keyof Props];
type InferZodErrorPaths<T> = T extends $ZodErrorTree<object> ? Paths<T> : never;
type ZodFieldErrorProps<Errors extends $ZodErrorTree<any>, Name extends "" | InferZodErrorPaths<Errors>> = {
errors: Errors;
name: Name;
children?: (props: ZodFieldErrorChildrenProps<Name>) => React__default.ReactNode;
};
type ZodFieldErrorChildrenProps<Name> = {
name: Name;
error?: string;
errors: readonly string[];
};
declare const noError: Readonly<{
errors: readonly string[];
}>;
declare function ZodFieldError<Errors extends $ZodErrorTree<object>, Name extends "" | InferZodErrorPaths<Errors> = "">({ errors, name, children, }: ZodFieldErrorProps<Errors, Name>): React__default.ReactNode;
/**
* Creates a typed components for actions created with the formAction builder.
*/
declare function createComponents<Data, Error, ValidationError extends $ZodErrorTree<any>, Args extends unknown[] = []>(action: FormAction<Data, Error, ValidationError, FormData, Args>): {
FieldError: <Name extends "" | InferZodErrorPaths<ValidationError>>({ name, children, }: {
name: Name;
children?: (props: ZodFieldErrorChildrenProps<Name>) => React__default.ReactNode;
}) => React__default.JSX.Element;
Success: ({ children, }: PropsWithChildren | {
children?: (props: {
isSuccess: false;
data: Data | null;
} | {
isSuccess: true;
data: Data;
}) => React__default.ReactNode;
}) => React__default.ReactNode;
Invalid: ({ children, }: PropsWithChildren | {
children?: (props: {
isInvalid: false;
validationError: null;
} | {
isInvalid: true;
validationError: ValidationError;
}) => React__default.ReactNode;
}) => React__default.ReactNode;
};
/**
* Conditionally renders the children, when the form action is in the "pending" state.
*/
declare function Pending({ children, }: PropsWithChildren | {
children?: (props: {
isPending: boolean;
}) => React.ReactNode;
}): React$1.ReactNode;
export { Action, type ActionContextState, type ActionProps, Form, type FormProps, type InferZodErrorPaths, type Paths, Pending, ZodFieldError, type ZodFieldErrorChildrenProps, type ZodFieldErrorProps, createComponents, noError, useActionContext };