Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(3954): removed ternary operators #3971

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-ears-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': minor
---

Remove ternary operator use and it will close #3954
55 changes: 41 additions & 14 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,12 @@ export function useFormik<Values extends FormikValues = FormikValues>({
dispatchFn();
}
},
[props.initialErrors, props.initialStatus, props.initialTouched, props.onReset]
[
props.initialErrors,
props.initialStatus,
props.initialTouched,
props.onReset,
]
);

React.useEffect(() => {
Expand Down Expand Up @@ -1018,21 +1023,43 @@ export function Formik<
// eslint-disable-next-line
}, []);
}

const getElementToRender = ({
component,
render,
children,
formikbag,
}: {
component:
| React.ComponentType<FormikProps<Values>>
| React.ReactNode
| undefined;
render: ((bag: FormikProps<Values>) => React.ReactNode) | undefined;
children:
| ((bag: FormikProps<Values>) => React.ReactNode)
| React.ReactNode
| undefined;
formikbag: FormikProps<Values>;
}): React.ReactNode | null => {
if (component && React.isValidElement(component))
return React.createElement(
(component as unknown) as React.ComponentType<FormikProps<Values>>,
formikbag
);
if (render) return render(formikbag);
if (children) {
if (isFunction(children))
return (children as (bag: FormikProps<Values>) => React.ReactNode)(
formikbag as FormikProps<Values>
);
if (!isEmptyChildren(children)) return React.Children.only(children);
}
return null;
};

return (
<FormikProvider value={formikbag}>
{component
? React.createElement(component as any, formikbag)
: render
? render(formikbag)
: children // children come last, always called
? isFunction(children)
? (children as (bag: FormikProps<Values>) => React.ReactNode)(
formikbag as FormikProps<Values>
)
: !isEmptyChildren(children)
? React.Children.only(children)
: null
: null}
{getElementToRender({ component, render, children, formikbag })}
</FormikProvider>
);
}
Expand Down