Refactor button component to convert children to title case

This commit is contained in:
anovazzi1 2024-03-29 18:43:52 -03:00
commit 06f4ade55f

View file

@ -41,14 +41,27 @@ export interface ButtonProps
asChild?: boolean; asChild?: boolean;
} }
function toTitleCase(text: string) {
return text
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>( const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => { ({ className, variant, size, asChild = false,children, ...props }, ref) => {
const Comp = asChild ? Slot : "button"; const Comp = asChild ? Slot : "button";
let newChildren = children;
if (typeof(children)==="string"){
newChildren = toTitleCase(children)
}
return ( return (
<Comp <Comp
className={cn(buttonVariants({ variant, size, className }))} className={cn(buttonVariants({ variant, size, className }))}
ref={ref} ref={ref}
children={newChildren}
{...props} {...props}
/> />
); );
} }