1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00
unleash.unleash/frontend/src/component/common/ConditionallyRender/ConditionallyRender.tsx
Fredrik Strand Oseberg 524936912d Feat/user flow (#267)
* feat: add new user page

* feat: passwordchecker

* fix: remove loading

* feat: reset password

* fix: move swr to devDeps

* feat: generate reset link

* feat: add reset password form

* fix: remove console log

* fix: rename to forgotten password

* feat: add simple menu

* fix: change password checker title

* fix: change text in new-user view

* fix: lint errors

* fix: add status code to constants

* fix: comment

* fix: add classes for new user component

* fix: tests

* fix: remove console log

* fix: remove retry method

* fix: invalid token constant

* fix: remove console log

* fix: dependency array on useCallback

* fix: featureview

* fix: redirect on authenticated

* refactor: progresswheel

* fix: lint deps
2021-04-19 10:55:15 +02:00

46 lines
1.2 KiB
TypeScript

interface IConditionallyRenderProps {
condition: boolean;
show: JSX.Element | RenderFunc;
elseShow?: JSX.Element | RenderFunc;
}
type RenderFunc = () => JSX.Element;
const ConditionallyRender = ({
condition,
show,
elseShow,
}: IConditionallyRenderProps): JSX.Element | null => {
const handleFunction = (renderFunc: RenderFunc): JSX.Element | null => {
const result = renderFunc();
if (!result) {
/* eslint-disable-next-line */
console.warn(
'Nothing was returned from your render function. Verify that you are returning a valid react component'
);
return null;
}
return result;
};
const isFunc = (param: JSX.Element | RenderFunc) =>
typeof param === 'function';
if (condition) {
if (isFunc(show)) {
return handleFunction(show as RenderFunc);
}
return show as JSX.Element;
}
if (!condition && elseShow) {
if (isFunc(elseShow)) {
return handleFunction(elseShow as RenderFunc);
}
return elseShow as JSX.Element;
}
return null;
};
export default ConditionallyRender;