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: pass index & children to deepMap, deepForeach & deepFind #17

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/lib/deepFind.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Children, isValidElement, ReactNode } from 'react';
import hasComplexChildren from './hasComplexChildren';

export type FindFunction = (child: ReactNode, index?: number, children?: ReactNode[]) => boolean;
export type FindFunction = (
child: ReactNode,
index?: number,
children?: readonly ReactNode[],
) => boolean;

const deepFind = (children: ReactNode, deepFindFn: FindFunction): ReactNode | undefined => {
let found;

Children.toArray(children).find((child: ReactNode) => {
if (deepFindFn(child)) {
found = child;
return true;
}
Children.toArray(children).find(
(child: ReactNode, index: number, findChildren: readonly ReactNode[]) => {
if (deepFindFn(child, index, findChildren)) {
found = child;
return true;
}

if (isValidElement(child) && hasComplexChildren(child)) {
// Find inside the child that has children
found = deepFind(child.props.children, deepFindFn);
return typeof found !== 'undefined';
}
if (isValidElement(child) && hasComplexChildren(child)) {
// Find inside the child that has children
found = deepFind(child.props.children, deepFindFn);
return typeof found !== 'undefined';
}

return false;
});
return false;
},
);

return found;
};
Expand Down
6 changes: 3 additions & 3 deletions src/lib/deepForEach.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Children, isValidElement, ReactNode } from 'react';
import hasComplexChildren from './hasComplexChildren';

export type ForEachFunction = (child: ReactNode, index?: number, children?: ReactNode[]) => void;
export type ForEachFunction = (child: ReactNode, index?: number) => void;

const deepForEach = (children: ReactNode, deepForEachFn: ForEachFunction): void => {
Children.forEach(children, (child: ReactNode) => {
Children.forEach(children, (child: ReactNode, index: number) => {
if (isValidElement(child) && hasComplexChildren(child)) {
// Each inside the child that has children
deepForEach(child.props.children, deepForEachFn);
}
deepForEachFn(child);
deepForEachFn(child, index);
});
};

Expand Down
32 changes: 19 additions & 13 deletions src/lib/deepMap.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { Children, cloneElement, isValidElement, ReactNode } from 'react';
import hasComplexChildren from './hasComplexChildren';

export type MapFunction = (child: ReactNode, index?: number, children?: ReactNode[]) => ReactNode;
export type MapFunction = (
child: ReactNode,
index?: number,
children?: readonly ReactNode[],
) => ReactNode;

const deepMap = (children: ReactNode, deepMapFn: MapFunction): ReactNode[] => {
return Children.toArray(children).map((child: ReactNode) => {
if (isValidElement(child) && hasComplexChildren(child)) {
// Clone the child that has children and map them too
return deepMapFn(
cloneElement(child, {
...child.props,
children: deepMap(child.props.children, deepMapFn),
}),
);
}
return deepMapFn(child);
});
return Children.toArray(children).map(
(child: ReactNode, index: number, mapChildren: readonly ReactNode[]) => {
if (isValidElement(child) && hasComplexChildren(child)) {
// Clone the child that has children and map them too
return deepMapFn(
cloneElement(child, {
...child.props,
children: deepMap(child.props.children, deepMapFn),
}),
);
}
return deepMapFn(child, index, mapChildren);
},
);
};

export default deepMap;