Skip to content

Commit

Permalink
feat(Button): adds type prop
Browse files Browse the repository at this point in the history
Closes #277
  • Loading branch information
aditya-kumawat committed Aug 21, 2020
1 parent c1e4bcd commit 7584eef
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
24 changes: 17 additions & 7 deletions core/components/atoms/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import Icon from '@/components/atoms/icon';
import classNames from 'classnames';
import { BaseProps } from '@/utils/types';

export type ButtonType = 'button' | 'submit' | 'reset';
export type Appearance = 'basic' | 'primary' | 'success' | 'alert' | 'transparent';

export type Size = 'tiny' | 'regular' | 'large';

export type Alignment = 'left' | 'right';

export interface ButtonProps extends BaseProps {
/**
* Type of `Button`
*/
type?: ButtonType;
/**
* The size of `Button`
* @default "regular"
Expand Down Expand Up @@ -65,18 +68,19 @@ export interface ButtonProps extends BaseProps {
onMouseLeave?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
}

const sizeMapping = {
const sizeMapping: Record<Size, number> = {
tiny: 12,
regular: 16,
large: 20,
};

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
const {
appearance = 'basic',
size = 'regular',
iconAlign = 'left',
tabIndex = 0,
appearance,
iconAlign,
tabIndex,
type,
children,
icon,
expanded,
Expand Down Expand Up @@ -107,7 +111,7 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, r
});

return (
<button ref={ref} className={buttonClass} disabled={disabled || loading} tabIndex={tabIndex} {...rest} >
<button ref={ref} type={type} className={buttonClass} disabled={disabled || loading} tabIndex={tabIndex} {...rest} >
{loading && (
<span className={spinnerClass}>
<Spinner size="small" appearance={(appearance === 'basic' || appearance === 'transparent') ? 'secondary' : 'white'} />
Expand All @@ -128,5 +132,11 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, r
});

Button.displayName = 'Button';
Button.defaultProps = {
appearance: 'basic',
size: 'regular',
iconAlign: 'left',
tabIndex: 0
};

export default Button;
7 changes: 7 additions & 0 deletions core/components/atoms/button/__stories__/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import Button from '../Button';

// CSF format story
export const all = () => {
const type = select(
'type',
['button', 'submit', 'reset'],
undefined
);

const appearance = select(
'appearance',
['basic', 'primary', 'success', 'alert', 'transparent'],
Expand Down Expand Up @@ -53,6 +59,7 @@ export const all = () => {
onClick={action('button-clicked')}
onMouseEnter={action('mouse-enter')}
onMouseLeave={action('mouse-leave')}
type={type}
appearance={appearance}
size={size}
expanded={expanded}
Expand Down
24 changes: 24 additions & 0 deletions core/components/atoms/button/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@ import { testHelper, filterUndefined, valueHelper, testMessageHelper } from '@/u
import Button, { ButtonProps as Props } from '../Button';

const BooleanValue = [true, false];
const buttonType = ['button', 'submit', 'reset'];
const icon = 'events';
const iconAlign = ['left', 'right'];
const size = ['tiny', 'regular', 'large'];
const appearance = ['basic', 'primary', 'success', 'alert', 'transparent'];

describe('Button component', () => {
const mapper: Record<string, any> = {
type: valueHelper(buttonType, { required: true, iterate: true }),
};

const testFunc = (props: Record<string, any>): void => {
const attr = filterUndefined(props) as Props;

it(testMessageHelper(attr), () => {
const tree = shallow(
<Button
{...attr}
>
Button
</Button >
);
expect(tree).toMatchSnapshot();
});
};

testHelper(mapper, testFunc);
});

describe('Button component', () => {
const mapper: Record<string, any> = {
appearance: valueHelper(appearance, { required: true, iterate: true }),
Expand Down
1 change: 1 addition & 0 deletions core/components/atoms/dropdown/DropdownButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const DropdownButton = React.forwardRef<HTMLButtonElement, DropdownButtonProps>(
return (
<button
ref={ref}
type="button"
value={children}
className={buttonClass}
disabled={disabled}
Expand Down

0 comments on commit 7584eef

Please sign in to comment.