From fb09a097957e31f8001d2f4c0f4da3d0eb89c589 Mon Sep 17 00:00:00 2001 From: myNameIsDu Date: Thu, 14 Sep 2023 14:37:26 +0800 Subject: [PATCH] fix: fix Field allways passes a undefined className to custom component --- .changeset/real-wolves-cough.md | 5 ++ packages/formik/src/Field.tsx | 31 +++++++------ packages/formik/test/Field.test.tsx | 72 ++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 .changeset/real-wolves-cough.md diff --git a/.changeset/real-wolves-cough.md b/.changeset/real-wolves-cough.md new file mode 100644 index 000000000..08006fe5f --- /dev/null +++ b/.changeset/real-wolves-cough.md @@ -0,0 +1,5 @@ +--- +'formik': patch +--- + +fix Field allways passes a undefined className to custom component diff --git a/packages/formik/src/Field.tsx b/packages/formik/src/Field.tsx index 5e33d8331..91df541a4 100644 --- a/packages/formik/src/Field.tsx +++ b/packages/formik/src/Field.tsx @@ -22,19 +22,19 @@ export interface FieldConfig { * Field component to render. Can either be a string like 'select' or a component. */ component?: - | string - | React.ComponentType> - | React.ComponentType - | React.ForwardRefExoticComponent; + | string + | React.ComponentType> + | React.ComponentType + | React.ForwardRefExoticComponent; /** * Component to render. Can either be a string e.g. 'select', 'input', or 'textarea', or a component. */ as?: - | React.ComponentType['field']> - | string - | React.ComponentType - | React.ForwardRefExoticComponent; + | React.ComponentType['field']> + | string + | React.ComponentType + | React.ForwardRefExoticComponent; /** * Render prop (works like React router's } />) @@ -72,10 +72,12 @@ export interface FieldConfig { innerRef?: (instance: any) => void; } -export type FieldAttributes = { className?: string; } & GenericFieldHTMLAttributes & +export type FieldAttributes = { + className?: string; +} & GenericFieldHTMLAttributes & FieldConfig & T & { - name: string, + name: string; }; export type FieldHookConfig = GenericFieldHTMLAttributes & FieldConfig; @@ -141,7 +143,6 @@ export function Field({ children, as: is, // `as` is reserved in typescript lol component, - className, ...props }: FieldAttributes) { const { @@ -205,14 +206,14 @@ export function Field({ const { innerRef, ...rest } = props; return React.createElement( component, - { ref: innerRef, ...field, ...rest, className }, + { ref: innerRef, ...field, ...rest }, children ); } // We don't pass `meta` for backwards compat return React.createElement( component, - { field, form: formik, ...props, className }, + { field, form: formik, ...props }, children ); } @@ -224,10 +225,10 @@ export function Field({ const { innerRef, ...rest } = props; return React.createElement( asElement, - { ref: innerRef, ...field, ...rest, className }, + { ref: innerRef, ...field, ...rest }, children ); } - return React.createElement(asElement, { ...field, ...props, className }, children); + return React.createElement(asElement, { ...field, ...props }, children); } diff --git a/packages/formik/test/Field.test.tsx b/packages/formik/test/Field.test.tsx index 9103f982e..09331b7c5 100644 --- a/packages/formik/test/Field.test.tsx +++ b/packages/formik/test/Field.test.tsx @@ -100,10 +100,14 @@ describe('Field / FastField', () => { describe('renders an by default', () => { it('', () => { - const className = 'field-custom' - const { container } = renderForm(); + const className = 'field-custom'; + const { container } = renderForm( + + ); expect(container.querySelectorAll('input')).toHaveLength(1); - expect(container.querySelector(`.${className}`)?.getAttribute('value')).toEqual('jared') + expect( + container.querySelector(`.${className}`)?.getAttribute('value') + ).toEqual('jared'); }); it('', () => { @@ -112,22 +116,6 @@ describe('Field / FastField', () => { }); }); - describe('renders an with className', () => { - it('', () => { - const className = 'field-custom' - const { container } = renderForm(); - expect(container.querySelectorAll(`.${className}`)).toHaveLength(1) - expect(container.querySelector(`.${className}`)?.getAttribute('value')).toEqual('jared') - }); - - it('', () => { - const className = 'field-custom' - const { container } = renderForm(); - expect(container.querySelectorAll(`.${className}`)).toHaveLength(1) - expect(container.querySelector(`.${className}`)?.getAttribute('value')).toEqual('jared') - }); - }); - describe('receives { field, form, meta } props and renders element', () => { it('', () => { let injected: FieldProps[] = []; @@ -222,7 +210,7 @@ describe('Field / FastField', () => { }); describe('children', () => { - cases('renders a child element with component', () => { + it('renders a child element with component', () => { const { container } = renderForm(