Skip to content
Open
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
54 changes: 24 additions & 30 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
/* eslint-disable react/jsx-no-useless-fragment */
import * as React from 'react';
import React, { memo, useMemo } from "react";

type WhenProps = {
children: React.ReactNode,
isTrue?: boolean,
limit?: number,
};
type WhenProps = React.PropsWithChildren<{
condition?: boolean;
limit?: number;
}>;

const RenderWhen = ({ limit, isTrue, children }:WhenProps) => {
const list:React.ReactNode[] = [];
const Wrapper: React.FC<WhenProps> = memo(
({ limit = 1, condition = true, children }) => {
const list = useMemo(() => {
return React.Children.map(children, (child) => {
const { condition: childCondition } = child?.props || {};

if (isTrue !== true) {
return null;
}

React.Children.map(children, (child:any) => {
const { isTrue: isChildTrue } = child?.props || {};

if (isChildTrue === true && list.length < limit) {
list.push(child);
if (childCondition && list.length < limit) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on this line (col 31) throws a ts error

'list' is possibly 'null' or 'undefined'.`

return child;
}
});
});
}, [children, limit]);

return (
<>
{list}
</>
);
};
if (!condition) {
return null;
}

RenderWhen.defaultProps = {
limit: 1,
isTrue: true,
};
return <>{list}</>;
}
);

RenderWhen.If = ({ children, isTrue }) => children;
const If: React.FC<WhenProps> = memo(({ children }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on this line, (col 41) throws this error

  Overload 1 of 2, '(Component: FunctionComponent<{ condition?: boolean | undefined; limit?: number | undefined; } & { children?: ReactNode; }>, propsAreEqual?: ((prevProps: Readonly<{ condition?: boolean | undefined; limit?: number | undefined; } & { ...; }>, nextProps: Readonly<...>) => boolean) | undefined): NamedExoticComponent<...>', gave the following error.
    Argument of type '({ children }: { condition?: boolean | undefined; limit?: number | undefined; } & { children?: ReactNode; }) => ReactNode' is not assignable to parameter of type 'FunctionComponent<{ condition?: boolean | undefined; limit?: number | undefined; } & { children?: ReactNode; }>'.
      Type 'ReactNode' is not assignable to type 'ReactElement<any, any> | null'.
        Type 'undefined' is not assignable to type 'ReactElement<any, any> | null'.
  Overload 2 of 2, '(Component: ComponentType<any>, propsAreEqual?: ((prevProps: Readonly<any>, nextProps: Readonly<any>) => boolean) | undefined): MemoExoticComponent<ComponentType<any>>', gave the following error.
    Argument of type '({ children }: { condition?: boolean | undefined; limit?: number | undefined; } & { children?: ReactNode; }) => ReactNode' is not assignable to parameter of type 'ComponentType<any>'.
      Type '({ children }: { condition?: boolean | undefined; limit?: number | undefined; } & { children?: ReactNode; }) => ReactNode' is not assignable to type 'FunctionComponent<any>'.
        Type 'ReactNode' is not assignable to type 'ReactElement<any, any> | null'.

return children;
});

export default RenderWhen;
export default { Wrapper, If };