-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Jest with Typescript and convert Button to Typescript. (#1797)
- Loading branch information
1 parent
0ea54e4
commit fedf008
Showing
10 changed files
with
246 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ module.exports = { | |
}, | ||
}, | ||
], | ||
'@babel/preset-typescript', | ||
], | ||
}, | ||
}, | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import noop from 'lodash/noop'; | ||
import classNames from 'classnames'; | ||
import { BUTTON } from 'common/constants/testIDs'; | ||
import { gtag } from 'common/utils/thirdParty/gtag'; | ||
import { getDataAttributes, getAriaAttributes } from 'common/utils/prop-utils'; | ||
import styles from './Button.module.css'; | ||
|
||
type GoogleAnalyticsEventPropType = { | ||
/** | ||
* A description of the behaviour. E.g. 'Clicked Delete', 'Added a component', 'Deleted account' | ||
*/ | ||
action: string; | ||
/** | ||
* A top level category for these events. E.g. 'User', 'Navigation', 'App Editing', etc. | ||
*/ | ||
category: string; | ||
/** | ||
* More precise labeling of the related action. E.g. alongside the 'Added a component' action, | ||
* we could add the name of a component as the label. E.g. 'Survey', 'Heading', 'Button', etc. | ||
*/ | ||
label?: string; | ||
/** | ||
* A means of recording a numerical value against an event. E.g. a rating, a score, etc. | ||
*/ | ||
value?: number; | ||
/** | ||
* If an event is not triggered by a user interaction, but instead by our code (e.g. on page | ||
* load), it should be flagged as a nonInteraction event to avoid skewing bounce rate data. | ||
*/ | ||
nonInteraction?: boolean; | ||
/** | ||
* This specifies the transport mechanism with which hits will be sent. Valid values include | ||
* 'beacon', 'xhr', or 'image'. | ||
*/ | ||
transport?: 'beacon' | 'xhr' | 'image'; | ||
}; | ||
|
||
type ButtonProps = { | ||
/** | ||
* Helps track in-page `event` interactions. | ||
*/ | ||
analyticsObject?: GoogleAnalyticsEventPropType; | ||
/** | ||
* Forces the component's width as wide as its parent container's width. | ||
*/ | ||
fullWidth?: boolean; | ||
/** | ||
* Sets the button color theme. | ||
*/ | ||
theme?: 'primary' | 'secondary'; | ||
} & React.ButtonHTMLAttributes<HTMLButtonElement>; | ||
|
||
export default function Button({ | ||
analyticsObject = { | ||
action: 'Button Selected', | ||
category: 'Interactions', | ||
}, | ||
children, | ||
className = undefined, | ||
disabled = false, | ||
fullWidth = false, | ||
onClick = noop, | ||
tabIndex = 0, | ||
theme = 'primary', | ||
type = 'button', | ||
...rest | ||
}: ButtonProps) { | ||
const customDataAttributes = getDataAttributes(rest); | ||
const ariaAttributes = getAriaAttributes(rest); | ||
|
||
const eventConfig = { | ||
...analyticsObject, | ||
label: typeof children === 'string' ? children : undefined, | ||
}; | ||
|
||
return ( | ||
<button | ||
className={classNames(styles.Button, className, styles[theme], { | ||
[styles.disabled]: disabled, | ||
[styles.fullWidth]: fullWidth, | ||
})} | ||
data-testid={BUTTON} | ||
disabled={disabled} | ||
onClick={e => { | ||
gtag.event(eventConfig); | ||
onClick(e); | ||
}} | ||
tabIndex={tabIndex} | ||
type={type} | ||
{...customDataAttributes} | ||
{...ariaAttributes} | ||
> | ||
{children} | ||
</button> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import Button from '../Button'; | ||
|
||
type ButtonStoryType = StoryObj<typeof Button>; | ||
|
||
export const Default: ButtonStoryType = { | ||
render: args => <Button {...args} />, | ||
args: { | ||
children: 'Button', | ||
}, | ||
}; | ||
|
||
const meta: Meta<typeof Button> = { | ||
title: 'Buttons/Button', | ||
component: Button, | ||
parameters: { | ||
actions: { | ||
handles: ['click'], | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.