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 React warning in ButtonGroup #1323

Merged
Merged
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
10 changes: 6 additions & 4 deletions packages/ui/src/components/Button/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { twMerge } from "tailwind-merge";
import { mergeDeep } from "../../helpers/merge-deep";
import { getTheme } from "../../theme-store";
import type { DeepPartial } from "../../types";
import type { ButtonProps } from "../Button";
import { Button, type ButtonProps } from "../Button";

export interface FlowbiteButtonGroupTheme {
base: string;
Expand All @@ -29,27 +29,29 @@ const processChildren = (
): ReactNode => {
return Children.map(children as ReactElement<ButtonProps>[], (child, index) => {
if (isValidElement(child)) {
const positionInGroupProp =
child.type == Button ? { positionInGroup: determinePosition(index, Children.count(children)) } : {};
// Check if the child has nested children
if (child.props.children) {
// Recursively process nested children
return cloneElement(child, {
...child.props,
children: processChildren(child.props.children, outline, pill),
positionInGroup: determinePosition(index, Children.count(children)),
...positionInGroupProp,
});
} else {
return cloneElement(child, {
outline,
pill,
positionInGroup: determinePosition(index, Children.count(children)),
...positionInGroupProp,
});
}
}
return child;
});
};

const determinePosition = (index: number, totalChildren: number) => {
const determinePosition = (index: number, totalChildren: number): keyof PositionInButtonGroup => {
return index === 0 ? "start" : index === totalChildren - 1 ? "end" : "middle";
};

Expand Down