Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 27 additions & 35 deletions packages/button/src/Button.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ const buttonClass = css`
}
`;

function CustomElement({
customProp,
...rest
}: {
customProp: string;
}): React.ReactElement {
return (
<div>
Lookit, {customProp}!<br />
<button {...rest} />
</div>
);
}

storiesOf('Buttons', module)
.add('Default', () => (
<Button
Expand Down Expand Up @@ -121,24 +107,30 @@ storiesOf('Buttons', module)
{text('Children', 'Button')}
</Button>
))
.add('as custom component', () => (
<Button
as={CustomElement}
size={select('Size', Object.values(Size) as Array<Size>, Size.Normal)}
variant={select(
'Variant',
Object.values(Variant) as Array<Variant>,
Variant.Default,
)}
title={text('Title', 'The button title')}
disabled={boolean('Disabled', false)}
href={select(
'Href',
{ 'mongodb.design': 'http://mongodb.design', none: null },
null,
)}
className={buttonClass}
>
{text('Children', 'Button')}
</Button>
));
.add('as custom component', () => {
const CustomRoot = select(
'div',
{ div: 'div', span: 'span', button: 'button' },
'div',
);

function CustomElement(props: object): React.ReactElement {
return <CustomRoot {...props} />;
}

return (
<Button
as={CustomElement}
size={select('Size', Object.values(Size) as Array<Size>, Size.Normal)}
variant={select(
'Variant',
Object.values(Variant) as Array<Variant>,
Variant.Default,
)}
disabled={boolean('Disabled', false)}
className={buttonClass}
>
{text('Children', 'Button')}
</Button>
);
});
43 changes: 17 additions & 26 deletions packages/button/src/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,10 @@ export default function Button(props: ButtonProps) {
'children',
]);

if (usesCustomElement(props)) {
const Root = props.as;

return (
<Root {...rest} {...commonProps}>
{children}
</Root>
);
}

if (usesLinkElement(props)) {
return (
<a {...(rest as HTMLElementProps<'a'>)} {...commonProps}>
{children}
</a>
);
}

// NOTE(JeT): The button's `type` will be overridden if it is in the passed-in props
return (
<button
type="button"
{...(rest as HTMLElementProps<'button'>)}
const renderButton = (Root: React.ElementType<any> = 'button') => (
<Root
type={Root === 'button' ? 'button' : null}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default should probably be undefined.

{...(rest as HTMLElementProps<any>)}
{...commonProps}
>
<span
Expand All @@ -341,13 +322,23 @@ export default function Button(props: ButtonProps) {
>
{children}
</span>
</button>
</Root>
);

if (usesCustomElement(props)) {
return renderButton(props.as);
}

if (usesLinkElement(props)) {
return renderButton('a');
}

return renderButton();
}

Button.propTypes = {
variant: PropTypes.oneOf(['default', 'primary', 'info', 'danger', 'dark']),
size: PropTypes.oneOf(['xsmall', 'small', 'normal', 'large']),
variant: PropTypes.oneOf(Object.keys(Variant)),
size: PropTypes.oneOf(Object.keys(Size)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be Object.values, not keys?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂ Yep, whoops. Fixed!

className: PropTypes.string,
children: PropTypes.node,
disabled: PropTypes.bool,
Expand Down