@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
106 lines (104 loc) • 4.58 kB
JavaScript
import * as React from "react";
import { isArray, isEmpty, isNumber, isString, isUndefined } from "@yamada-ui/utils";
import { jsx } from "react/jsx-runtime";
//#region src/utils/children.tsx
function getValidChildren(children) {
return React.Children.toArray(children).filter((child) => React.isValidElement(child));
}
function useValidChildren(children) {
return React.useMemo(() => getValidChildren(children), [children]);
}
function isValidElement$1(child) {
return React.isValidElement(child) || isString(child) || isNumber(child);
}
function isSomeDisplayName(a, b) {
if (isUndefined(a) || isUndefined(b)) return false;
if (isArray(a)) {
if (a.includes(b)) return true;
if (!!b.displayName && a.includes(b.displayName)) return true;
if (!!b.name && a.includes(b.name)) return true;
} else {
if (a === b) return true;
if (!!a.displayName && !!b.displayName && a.displayName === b.displayName) return true;
if (!!a.name && !!b.name && a.name === b.name) return true;
if (!!a.displayName && !!b.name && a.displayName === b.name) return true;
if (!!a.name && !!b.displayName && a.name === b.displayName) return true;
}
return false;
}
function isSomeElement(a, b) {
if (isUndefined(a) || isUndefined(b)) return false;
if (a === b) return true;
if (isSomeDisplayName(a, b)) return true;
a = a._payload?.value;
if (isUndefined(a)) return false;
if (isSomeDisplayName(a, b)) return true;
return false;
}
function findChild(children, ...types) {
return children.find((child) => types.some((type) => isSomeElement(child.type, type)));
}
function useFindChild(children, ...types) {
const typesRef = React.useRef(types);
return React.useMemo(() => findChild(children, ...typesRef.current), [children]);
}
function findChildren(children, ...types) {
if (findChild(children, ...types)) return children.sort((a, b) => {
if (types.some((type) => isSomeElement(a.type, type))) return -1;
else if (types.some((type) => isSomeElement(b.type, type))) return 1;
else return 0;
});
else return [void 0, ...children];
}
function useFindChildren(children, ...types) {
const typesRef = React.useRef(types);
return React.useMemo(() => findChildren(children, ...typesRef.current), [children]);
}
function includesChildren(children, ...types) {
return children.some((child) => {
if (types.some((type) => isSomeElement(child.type, type))) return true;
const children$1 = getValidChildren(child.props.children);
return children$1.length ? includesChildren(children$1, ...types) : false;
});
}
function useIncludesChildren(children, ...types) {
return React.useMemo(() => includesChildren(children, ...types), [children, types]);
}
function omitChildren(children, ...types) {
return children.filter((child) => types.every((type) => !isSomeElement(child.type, type)));
}
function useOmitChildren(children, ...types) {
const typesRef = React.useRef(types);
return React.useMemo(() => omitChildren(children, ...typesRef.current), [children]);
}
function pickChildren(children, ...types) {
return children.filter((child) => types.some((type) => isSomeElement(child.type, type)));
}
function usePickChildren(children, ...types) {
const typesRef = React.useRef(types);
return React.useMemo(() => pickChildren(children, ...typesRef.current), [children]);
}
function splitChildren(children, ...types) {
const validChildren = getValidChildren(children);
if (isEmpty(validChildren)) return [children];
const pickedChildren = types.map((types$1) => isArray(types$1) ? findChild(validChildren, ...types$1) : findChild(validChildren, types$1));
return [omitChildren(validChildren, ...types.flat()), ...pickedChildren];
}
function useSplitChildren(children, ...types) {
const typesRef = React.useRef(types);
return React.useMemo(() => splitChildren(children, ...typesRef.current), [children]);
}
const wrapOrPassProps = (Component, nodeOrProps, additionalProps) => {
if (isUndefined(nodeOrProps)) return null;
else if (isValidElement$1(nodeOrProps)) {
additionalProps ??= {};
additionalProps.children = nodeOrProps;
return /* @__PURE__ */ jsx(Component, { ...additionalProps });
} else return /* @__PURE__ */ jsx(Component, {
...additionalProps,
...nodeOrProps
});
};
//#endregion
export { findChild, findChildren, getValidChildren, includesChildren, isSomeDisplayName, isSomeElement, isValidElement$1 as isValidElement, omitChildren, pickChildren, splitChildren, useFindChild, useFindChildren, useIncludesChildren, useOmitChildren, usePickChildren, useSplitChildren, useValidChildren, wrapOrPassProps };
//# sourceMappingURL=children.js.map