Skip to content

Commit b5733ca

Browse files
author
erikras-dinesh-agent
committed
Fix #1051: Make types compatible with React Native
Changed element type parameter from 'T extends HTMLElement = HTMLElement' to 'T = any' to support both web and React Native environments. HTMLElement doesn't exist in pure React Native (without react-native-web), which broke type checking for React Native users in v7.0.0. Changes: - FieldInputProps: Changed T from 'extends HTMLElement = HTMLElement' to '= any' - FieldRenderProps: Changed T from 'extends HTMLElement = HTMLElement' to '= any' - FieldProps: Changed T from 'extends HTMLElement = HTMLElement' to '= any' - Field component: Updated generic constraint to match - useField hook: Updated generic constraint to match - Added FieldMetaState type export for backward compatibility - Fixed getIn call to handle undefined initialValues All existing tests pass. TypeScript build succeeds.
1 parent b6ae36b commit b5733ca

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/Field.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import useField from "./useField";
55

66
function FieldComponent<
77
FieldValue = any,
8-
T extends HTMLElement = HTMLElement,
8+
T = any,
99
FormValues = Record<string, any>,
1010
>(
1111
{
@@ -95,7 +95,7 @@ function FieldComponent<
9595
// Create a properly typed forwardRef component that preserves generics
9696
const Field = React.forwardRef(FieldComponent as any) as <
9797
FieldValue = any,
98-
T extends HTMLElement = HTMLElement,
98+
T = any,
9999
FormValues = Record<string, any>,
100100
>(
101101
props: FieldProps<FieldValue, T, FormValues> & { ref?: React.Ref<T> },

src/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ export interface ReactContext<FormValues = Record<string, any>> {
1515
reactFinalForm: FormApi<FormValues>;
1616
}
1717

18-
export interface FieldInputProps<
19-
FieldValue = any,
20-
T extends HTMLElement = HTMLElement,
21-
> {
18+
export interface FieldInputProps<FieldValue = any, T = any> {
2219
name: string;
2320
onBlur: (event?: React.FocusEvent<T>) => void;
2421
onChange: (event: React.ChangeEvent<T> | any) => void;
@@ -31,7 +28,7 @@ export interface FieldInputProps<
3128

3229
export interface FieldRenderProps<
3330
FieldValue = any,
34-
T extends HTMLElement = HTMLElement,
31+
T = any,
3532
_FormValues = any,
3633
> {
3734
input: FieldInputProps<FieldValue, T>;
@@ -58,6 +55,10 @@ export interface FieldRenderProps<
5855
};
5956
}
6057

58+
// Backward compatibility: Export FieldMetaState type
59+
export type FieldMetaState<FieldValue = any> =
60+
FieldRenderProps<FieldValue>["meta"];
61+
6162
export interface SubmitEvent {
6263
preventDefault?: () => void;
6364
stopPropagation?: () => void;
@@ -120,7 +121,7 @@ export interface UseFieldConfig extends UseFieldAutoConfig {
120121

121122
export interface FieldProps<
122123
FieldValue = any,
123-
T extends HTMLElement = HTMLElement,
124+
T = any,
124125
_FormValues = Record<string, any>,
125126
> extends UseFieldConfig,
126127
Omit<RenderableProps<FieldRenderProps<FieldValue, T>>, "children"> {

src/useField.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ const defaultParse = (value: any, _name: string) =>
2929

3030
const defaultIsEqual = (a: any, b: any): boolean => a === b;
3131

32-
function useField<
33-
FieldValue = any,
34-
T extends HTMLElement = HTMLElement,
35-
FormValues = Record<string, any>,
36-
>(name: string, config: UseFieldConfig = {}): FieldRenderProps<FieldValue, T> {
32+
function useField<FieldValue = any, T = any, FormValues = Record<string, any>>(
33+
name: string,
34+
config: UseFieldConfig = {},
35+
): FieldRenderProps<FieldValue, T> {
3736
const {
3837
afterSubmit,
3938
allowNull,
@@ -117,7 +116,10 @@ function useField<
117116
// If no existing state, create a proper initial state
118117
const formState = form.getState();
119118
// Use getIn to support nested field paths like "user.name" or "items[0].id"
120-
const formInitialValue = getIn(formState.initialValues, name);
119+
const formInitialValue = getIn(
120+
formState.initialValues || ({} as FormValues),
121+
name,
122+
);
121123

122124
// Use Form initialValues if available, otherwise use field initialValue
123125
let initialStateValue = formInitialValue !== undefined ? formInitialValue : initialValue;

0 commit comments

Comments
 (0)