rsuite
Version:
A suite of react components
154 lines (146 loc) • 4.17 kB
JavaScript
'use client';
import React from 'react';
import { isFragment } from "./ReactIs.js";
/**
* Flattens the given children into an array.
* @param children - The children to flatten.
* @returns The flattened array of children.
*/
function flatChildren(children) {
return React.Children.toArray(React.Children.map(children, child => {
if (isFragment(child)) {
var _child$props;
return React.Children.toArray(((_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.children) || []);
}
return child;
}));
}
/**
* Finds the first child that satisfies the given condition.
* @param children - The children to search.
* @param func - The condition function.
* @param context - The context to use for the condition function.
* @returns The first child that satisfies the condition, or undefined if no child is found.
*/
export function find(children, func, context) {
let index = 0;
let result;
React.Children.forEach(flatChildren(children), child => {
if (result) {
return;
}
index += 1;
if (func.call(context, child, index)) {
result = child;
}
});
return result;
}
/**
* Maps over the children and applies the given function to each child.
* @param children - The children to map over.
* @param func - The function to apply to each child.
* @param context - The context to use for the function.
* @returns An array of the results of applying the function to each child.
*/
export function map(children, func, context) {
let index = 0;
return React.Children.map(flatChildren(children), child => {
if (! /*#__PURE__*/React.isValidElement(child)) {
return child;
}
const handle = func.call(context, child, index);
index += 1;
return handle;
});
}
/**
* Maps over the children and clones each child element with the provided props.
* @param children - The children to clone and map over.
* @param func - The function to apply to each child element.
* @param context - The context to use for the function.
* @returns An array of the cloned and modified child elements.
*/
export function mapCloneElement(children, func, context) {
return map(children, (child, index) => /*#__PURE__*/React.cloneElement(child, {
key: index,
...func(child, index)
}), context);
}
/**
* Iterates over children that are in flat array form.
* @param children
* @param func
* @param context
*/
export function forEach(children, func, context) {
let index = 0;
React.Children.forEach(flatChildren(children), child => {
if (! /*#__PURE__*/React.isValidElement(child)) {
return;
}
func.call(context, child, index);
index += 1;
});
}
/**
* Returns the number of children.
* @param children - The children to count.
* @returns The number of children.
*/
export function count(children) {
return React.Children.count(flatChildren(children));
}
/**
* Checks if any child satisfies the given condition.
* @param children - The children to check.
* @param func - The condition function.
* @param context - The context to use for the condition function.
* @returns True if any child satisfies the condition, false otherwise.
*/
function some(children, func, context) {
let index = 0;
let result = false;
React.Children.forEach(flatChildren(children), child => {
if (result) {
return;
}
if (! /*#__PURE__*/React.isValidElement(child)) {
return;
}
if (func.call(context, child, index += 1)) {
result = true;
}
});
return result;
}
/**
* Utility functions for working with React children.
*/
export const rch = {
/**
* Maps over the children and clones each child element with the provided props.
*/
mapCloneElement,
/**
* Returns the number of children.
*/
count,
/**
* Checks if any child satisfies the given condition.
*/
some,
/**
* Maps over the children and applies the given function to each child.
*/
map,
/**
* Iterates over children that are in flat array form.
*/
forEach,
/**
* Finds the first child that satisfies the given condition.
*/
find
};
export default rch;