@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
127 lines (125 loc) • 5.56 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let react = require("react");
react = require_rolldown_runtime.__toESM(react);
let __yamada_ui_utils = require("@yamada-ui/utils");
__yamada_ui_utils = require_rolldown_runtime.__toESM(__yamada_ui_utils);
let react_jsx_runtime = require("react/jsx-runtime");
react_jsx_runtime = require_rolldown_runtime.__toESM(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(child) {
return react.isValidElement(child) || (0, __yamada_ui_utils.isString)(child) || (0, __yamada_ui_utils.isNumber)(child);
}
function isSomeDisplayName(a, b) {
if ((0, __yamada_ui_utils.isUndefined)(a) || (0, __yamada_ui_utils.isUndefined)(b)) return false;
if ((0, __yamada_ui_utils.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 ((0, __yamada_ui_utils.isUndefined)(a) || (0, __yamada_ui_utils.isUndefined)(b)) return false;
if (a === b) return true;
if (isSomeDisplayName(a, b)) return true;
a = a._payload?.value;
if ((0, __yamada_ui_utils.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 ((0, __yamada_ui_utils.isEmpty)(validChildren)) return [children];
const pickedChildren = types.map((types$1) => (0, __yamada_ui_utils.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 ((0, __yamada_ui_utils.isUndefined)(nodeOrProps)) return null;
else if (isValidElement(nodeOrProps)) {
additionalProps ??= {};
additionalProps.children = nodeOrProps;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, { ...additionalProps });
} else return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, {
...additionalProps,
...nodeOrProps
});
};
//#endregion
exports.findChild = findChild;
exports.findChildren = findChildren;
exports.getValidChildren = getValidChildren;
exports.includesChildren = includesChildren;
exports.isSomeDisplayName = isSomeDisplayName;
exports.isSomeElement = isSomeElement;
exports.isValidElement = isValidElement;
exports.omitChildren = omitChildren;
exports.pickChildren = pickChildren;
exports.splitChildren = splitChildren;
exports.useFindChild = useFindChild;
exports.useFindChildren = useFindChildren;
exports.useIncludesChildren = useIncludesChildren;
exports.useOmitChildren = useOmitChildren;
exports.usePickChildren = usePickChildren;
exports.useSplitChildren = useSplitChildren;
exports.useValidChildren = useValidChildren;
exports.wrapOrPassProps = wrapOrPassProps;
//# sourceMappingURL=children.cjs.map