mm
This commit is contained in:
29
packages/ui-web-headless/componnets/button/useButtonSlots.ts
Normal file
29
packages/ui-web-headless/componnets/button/useButtonSlots.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import React, { ReactNode, isValidElement, Children } from "react";
|
||||
import { ButtonIcon } from "./ButtonIcon";
|
||||
import { ButtonLoading } from "./ButtonLoading";
|
||||
|
||||
export function useButtonSlot(children: ReactNode) {
|
||||
const iconNodes: React.ReactElement[] = [];
|
||||
const loadingNodes: React.ReactElement[] = [];
|
||||
|
||||
const collectNodes = (nodes: ReactNode) => {
|
||||
Children.forEach(nodes, (child) => {
|
||||
if (!isValidElement(child)) return;
|
||||
|
||||
if (child.type === ButtonIcon) {
|
||||
iconNodes.push(child);
|
||||
} else if (child.type === ButtonLoading) {
|
||||
loadingNodes.push(child);
|
||||
} else if ((child as any).props.children) {
|
||||
collectNodes((child as any).props.children);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
collectNodes(children);
|
||||
|
||||
return {
|
||||
iconNode: iconNodes.at(-1),
|
||||
loadingNode: loadingNodes.at(-1),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user