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

ButtonProps type is missing onClick prop #89

Open
graham-a3 opened this issue Sep 3, 2024 · 2 comments
Open

ButtonProps type is missing onClick prop #89

graham-a3 opened this issue Sep 3, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@graham-a3
Copy link

image

Current work around is to explicitly add the onClick prop to the wrapper.

@graham-a3 graham-a3 changed the title ButtonProps is missing onClick ButtonProps type is missing onClick prop Sep 3, 2024
@rowellx68
Copy link
Owner

We can define an onClick property in ButtonProps but it will remove the typing that the polymorphic component brings.

From these:

// when we set `as` to `a`
onClick?: React.MouseEventHandler<HTMLAnchorElement> | undefined

// without setting the `as` property
onClick?: React.MouseEventHandler<HTMLButtonElement> | undefined

to this

onClick?: React.MouseEventHandler<HTMLElement> | undefined

@rowellx68
Copy link
Owner

The other option is to augment the type like you have. Also, as per mantine's docs regarding wrapping polymorphic components it suggests to do it like so:

import React, { forwardRef, MouseEventHandler } from 'react';
import { Button, ButtonProps } from 'nhsuk-frontend-react';

type WrappedButtonProps = ButtonProps & {
  isLoading?: boolean;
  onClick?: MouseEventHandler<HTMLButtonElement>;
};

export const WrappedButton = forwardRef<HTMLButtonElement, WrappedButtonProps>(
  ({ isLoading, children, ...props }, ref) => (
    <Button {...props} disabled={isLoading} ref={ref}>
      {isLoading ? 'Loading...' : children}
    </Button>
  ),
);

@rowellx68 rowellx68 added the documentation Improvements or additions to documentation label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants