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

8629 junk: button type safety #21

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ storybook-static
.DS_Store
*.code-workspace

# npm audit output
audit*.txt

# Misc
temp/
audit.txt
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
"sass-loader": "^8.0.0",
"sass-mq": "^5.0.1",
"style-loader": "^1.1.2",
"stylelint": "^12.0.1",
"stylelint-config-sass-guidelines": "^6.2.0",
"stylelint": "^13.13.1",
"stylelint-config-sass-guidelines": "^8.0.0",
"typescript": "^4.2.2",
"webpack": "^4.46.0"
},
Expand Down
50 changes: 29 additions & 21 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
ReactNode,
Ref
} from 'react';

import cx from 'classnames';

import Icon from 'Icon/Icon';
Expand All @@ -17,34 +18,42 @@ import iconMapping from 'Icon/iconMapping';
*
* @see {@link https://github.com/wellcometrust/corporate/issues/8629}
*/
export type ButtonProps = {
autoFocus?: boolean;
export type ButtonOwnProps<E extends React.ElementType = React.ElementType> = {
as?: E;
// autoFocus?: boolean;
className?: string;
children: ReactNode;
disabled?: boolean;
href?: string;
icon?: keyof typeof iconMapping;
iconClassName?: string;
iconPlacementSwitch?: boolean;
id?: string;
onBlur?: FocusEventHandler;
onClick?: MouseEventHandler;
onFocus?: FocusEventHandler;
onMouseEnter?: MouseEventHandler;
onMouseLeave?: MouseEventHandler;
role?: string;
tabIndex?: number;
// id?: string;
// onBlur?: FocusEventHandler;
// onClick?: MouseEventHandler;
// onFocus?: FocusEventHandler;
// onMouseEnter?: MouseEventHandler;
// onMouseLeave?: MouseEventHandler;
// role?: string;
// tabIndex?: number;
textClassName?: string;
type?: 'button' | 'submit' | 'reset' | undefined;
// type?: 'button' | 'submit' | 'reset' | undefined;
variant?: 'primary' | 'secondary' | 'tertiary' | 'link' | 'unstyled';
};

export type ButtonProps<
E extends React.ElementType = React.ElementType
> = ButtonOwnProps & Omit<React.ComponentProps<E>, keyof ButtonOwnProps>;

const defaultElement = 'button';

/**
* Functional call to action
*/
export const Button = forwardRef(
(
<E extends React.ElementType = typeof defaultElement>(
{
as,
autoFocus = false,
children,
className,
Expand All @@ -63,14 +72,14 @@ export const Button = forwardRef(
tabIndex,
textClassName,
type = 'button',
variant = 'primary',
...props
}: ButtonProps,
// TODO - use type generics to dynamically set the ref type
ref: Ref<HTMLAnchorElement> & Ref<HTMLButtonElement>
variant = 'primary'
}: ButtonProps<E>,
ref: Ref<React.ComponentProps<E>>
) => {
const isAnchor = !!href;
const Element: React.ElementType = isAnchor ? 'a' : 'button';
// const isAnchor = !!href;
// const Element: React.ElementType = isAnchor ? 'a' : defaultElement;
const Element = as || defaultElement;
const isAnchor = Element === 'a';
const hasStyles = variant !== 'unstyled';

const classNames = cx({
Expand All @@ -93,7 +102,7 @@ export const Button = forwardRef(
<Element
autoFocus={autoFocus}
className={classNames}
disabled={disabled}
disabled={!isAnchor && disabled}
href={href}
id={id}
onBlur={(e: FocusEvent) => {
Expand Down Expand Up @@ -125,7 +134,6 @@ export const Button = forwardRef(
role={role}
tabIndex={tabIndex}
type={!isAnchor ? type : undefined}
{...props}
>
{icon && !iconPlacementSwitch && (
<Icon name={icon} className={iconClassNames} />
Expand Down