flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
1 lines • 3.93 kB
Source Map (JSON)
{"version":3,"file":"ButtonGroup.mjs","sources":["../../../../src/components/Button/ButtonGroup.tsx"],"sourcesContent":["import type { ComponentProps, FC, ReactElement, ReactNode } from \"react\";\nimport { Children, cloneElement, isValidElement, useMemo } from \"react\";\nimport { twMerge } from \"tailwind-merge\";\nimport { mergeDeep } from \"../../helpers/merge-deep\";\nimport { getTheme } from \"../../theme-store\";\nimport type { DeepPartial } from \"../../types\";\nimport type { ButtonProps } from \"../Button\";\n\nexport interface FlowbiteButtonGroupTheme {\n base: string;\n position: PositionInButtonGroup;\n}\n\nexport interface PositionInButtonGroup {\n none: string;\n start: string;\n middle: string;\n end: string;\n}\n\nexport interface ButtonGroupProps extends ComponentProps<\"div\">, Pick<ButtonProps, \"outline\" | \"pill\"> {\n theme?: DeepPartial<FlowbiteButtonGroupTheme>;\n}\n\nconst processChildren = (\n children: React.ReactNode,\n outline: boolean | undefined,\n pill: boolean | undefined,\n): ReactNode => {\n return Children.map(children as ReactElement<ButtonProps>[], (child, index) => {\n if (isValidElement(child)) {\n // Check if the child has nested children\n if (child.props.children) {\n // Recursively process nested children\n return cloneElement(child, {\n ...child.props,\n children: processChildren(child.props.children, outline, pill),\n positionInGroup: determinePosition(index, Children.count(children)),\n });\n } else {\n return cloneElement(child, {\n outline,\n pill,\n positionInGroup: determinePosition(index, Children.count(children)),\n });\n }\n }\n return child;\n });\n};\n\nconst determinePosition = (index: number, totalChildren: number) => {\n return index === 0 ? \"start\" : index === totalChildren - 1 ? \"end\" : \"middle\";\n};\n\nexport const ButtonGroup: FC<ButtonGroupProps> = ({\n children,\n className,\n outline,\n pill,\n theme: customTheme = {},\n ...props\n}: ButtonGroupProps) => {\n const items = useMemo(() => processChildren(children, outline, pill), [children, outline, pill]);\n\n const theme = mergeDeep(getTheme().buttonGroup, customTheme);\n\n return (\n <div className={twMerge(theme.base, className)} role=\"group\" {...props}>\n {items}\n </div>\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