Skip to content

Commit 50c860e

Browse files
Fix React warning in ButtonGroup (#1323)
* Fix React warning in ButtonGroup This change fixes the following issue: ButtonGroup was adding positionInGroup to all children rather than just Buttons. For example, in the following, both the Buttons and the child spans get positionInGroup props: ``` <Button.Group><Button><span>b1</span></Button><Button><span>b2</span></Button></Button.Group> ``` This results in a React warning: “Warning: React does not recognize the `positionInGroup` prop on a DOM element...” The review comment in the original code change causing this hints at this issue as well: #1273 (comment) * fix formatting
1 parent c580a26 commit 50c860e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/ui/src/components/Button/ButtonGroup.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { twMerge } from "tailwind-merge";
44
import { mergeDeep } from "../../helpers/merge-deep";
55
import { getTheme } from "../../theme-store";
66
import type { DeepPartial } from "../../types";
7-
import type { ButtonProps } from "../Button";
7+
import { Button, type ButtonProps } from "../Button";
88

99
export interface FlowbiteButtonGroupTheme {
1010
base: string;
@@ -29,27 +29,29 @@ const processChildren = (
2929
): ReactNode => {
3030
return Children.map(children as ReactElement<ButtonProps>[], (child, index) => {
3131
if (isValidElement(child)) {
32+
const positionInGroupProp =
33+
child.type == Button ? { positionInGroup: determinePosition(index, Children.count(children)) } : {};
3234
// Check if the child has nested children
3335
if (child.props.children) {
3436
// Recursively process nested children
3537
return cloneElement(child, {
3638
...child.props,
3739
children: processChildren(child.props.children, outline, pill),
38-
positionInGroup: determinePosition(index, Children.count(children)),
40+
...positionInGroupProp,
3941
});
4042
} else {
4143
return cloneElement(child, {
4244
outline,
4345
pill,
44-
positionInGroup: determinePosition(index, Children.count(children)),
46+
...positionInGroupProp,
4547
});
4648
}
4749
}
4850
return child;
4951
});
5052
};
5153

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

0 commit comments

Comments
 (0)