-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
pr05 Typescript Migration #5: Migrate client/common folder #3565
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
Open
clairep94
wants to merge
38
commits into
processing:develop
Choose a base branch
from
clairep94:pr05/migrate_client_common_rebuild
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+808
−273
Open
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
f1af0a6
.prettierrc: remove hardcoded babel as parser setting to allow typesc…
clairep94 8a0f302
SkipLink: lint correctly to add ; to end of type definition lines
clairep94 1cb8f5b
RouterTab: unit test
clairep94 d376207
RouterTab: update to tsx --no-verify
clairep94 29e1e6d
RouterTab: add typescript & install @types/react-router-dom
clairep94 1efe93d
Button: update to tsx --no-verify
clairep94 6042638
Button.tsx: migrate to typescript, add unit test, add @types/styled-c…
clairep94 c1883c5
ButtonOrLink: update to tsx files --no-verify
clairep94 eb69138
ButtonOrLink: update to typescript
clairep94 5dd03d1
IconButton: update to tsx --no-verify
clairep94 197fd67
IconButton: migrate to typescript & add unit test
clairep94 cfb4b82
usePrevious: update to ts --no-verify
clairep94 d80961c
usePrevious: migrate to typescript & add unit test
clairep94 24368cc
isMac: migrate to typescript and add test
clairep94 e67e015
useKeyDownHandlers: update to ts --no-verify
clairep94 1b5373c
useKeyDownHandler: migrate to typescript and add unit test
clairep94 b5eea95
useModalClose: update to ts --no-verify
clairep94 7ca1e6d
useModalClose: update to typescript & add test
clairep94 7075ffb
useSyncFormTranslation: update to ts --no-verify
clairep94 6bc9369
useSyncFormTranslations: update to typescript and add unit test
clairep94 94ea59a
rename test utility rerender to mountComponent
clairep94 1b72ea4
Merge branch 'develop' into pr05/migrate_client_common_rebuild
clairep94 7ce4f54
.eslintrc: update to fix no-shadow eslint error on enums
clairep94 7188c1b
usePrevious: make type unknown to make more generic
clairep94 bee3211
RouterTab: update TabProps to interface
clairep94 6148c33
ButtonOrLink: update to interface
clairep94 3e8faea
IconButton: remove null
clairep94 4bd9443
Button: WIP update
clairep94 7718e02
SkipLink: update type to interface
clairep94 3428dac
.eslintrc: cherry-pick ts overrids from utils pr
clairep94 1a7cfaa
useSyncFormTranslations: update to be named export
clairep94 3676534
eslintrc: fix double config
clairep94 366aa7e
usePrevious: update to be named export
clairep94 2d8fe06
useModalClose: update to named export
clairep94 b1788d5
useKeyDownHandler: update to named export
clairep94 bc82be5
RouterTab: update to named export
clairep94 7b67549
ButtonOrLink: update to named export
clairep94 379e563
IconButton: update to interface and named export
clairep94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent } from '../test-utils'; | ||
import Button from './Button'; | ||
|
||
const MockIcon = (props: React.SVGProps<SVGSVGElement>) => ( | ||
<svg data-testid="mock-icon" {...props} /> | ||
); | ||
|
||
describe('Button', () => { | ||
// Tag | ||
it('renders as an anchor when href is provided', () => { | ||
render(<Button href="https://example.com">Link</Button>); | ||
const anchor = screen.getByRole('link'); | ||
expect(anchor.tagName.toLowerCase()).toBe('a'); | ||
expect(anchor).toHaveAttribute('href', 'https://example.com'); | ||
}); | ||
|
||
it('renders as a React Router <Link> when `to` is provided', () => { | ||
render(<Button to="/dashboard">Go</Button>); | ||
const link = screen.getByRole('link'); | ||
expect(link.tagName.toLowerCase()).toBe('a'); // Link renders as <a> | ||
expect(link).toHaveAttribute('href', '/dashboard'); | ||
}); | ||
|
||
it('renders as a <button> with a type of "button" by default', () => { | ||
render(<Button>Click Me</Button>); | ||
const el = screen.getByRole('button'); | ||
expect(el.tagName.toLowerCase()).toBe('button'); | ||
expect(el).toHaveAttribute('type', 'button'); | ||
}); | ||
|
||
// Children & Icons | ||
it('renders children', () => { | ||
render(<Button>Click Me</Button>); | ||
expect(screen.getByText('Click Me')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders an iconBefore and button text', () => { | ||
render( | ||
<Button iconBefore={<MockIcon aria-label="iconbefore" />}> | ||
This has a before icon | ||
</Button> | ||
); | ||
expect(screen.getByLabelText('iconbefore')).toBeInTheDocument(); | ||
expect(screen.getByRole('button')).toHaveTextContent( | ||
'This has a before icon' | ||
); | ||
}); | ||
|
||
it('renders with iconAfter', () => { | ||
render( | ||
<Button iconAfter={<MockIcon aria-label="iconafter" />}> | ||
This has an after icon | ||
</Button> | ||
); | ||
expect(screen.getByLabelText('iconafter')).toBeInTheDocument(); | ||
expect(screen.getByRole('button')).toHaveTextContent( | ||
'This has an after icon' | ||
); | ||
}); | ||
|
||
it('renders only the icon if iconOnly', () => { | ||
render( | ||
<Button iconAfter={<MockIcon aria-label="iconafter" />} iconOnly> | ||
This has an after icon | ||
</Button> | ||
); | ||
expect(screen.getByLabelText('iconafter')).toBeInTheDocument(); | ||
expect(screen.getByRole('button')).not.toHaveTextContent( | ||
'This has an after icon' | ||
); | ||
}); | ||
|
||
// HTML attributes | ||
it('calls onClick handler when clicked', () => { | ||
const handleClick = jest.fn(); | ||
render(<Button onClick={handleClick}>Click</Button>); | ||
fireEvent.click(screen.getByText('Click')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('renders disabled state', () => { | ||
render(<Button disabled>Disabled</Button>); | ||
expect(screen.getByRole('button')).toBeDisabled(); | ||
}); | ||
|
||
it('uses aria-label when provided', () => { | ||
render(<Button aria-label="Upload" iconOnly />); | ||
expect(screen.getByLabelText('Upload')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -1,28 +1,95 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styled from 'styled-components'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import { Link, LinkProps } from 'react-router-dom'; | ||
import { remSize, prop } from '../theme'; | ||
|
||
const kinds = { | ||
primary: 'primary', | ||
secondary: 'secondary' | ||
}; | ||
enum Kinds { | ||
primary = 'primary', | ||
secondary = 'secondary' | ||
} | ||
|
||
const displays = { | ||
block: 'block', | ||
inline: 'inline' | ||
}; | ||
enum Displays { | ||
block = 'block', | ||
inline = 'inline' | ||
} | ||
|
||
enum ButtonTypes { | ||
button = 'button', | ||
submit = 'submit' | ||
} | ||
|
||
type StyledButtonProps = { | ||
kind: ButtonTypes; | ||
display: Displays; | ||
type?: ButtonTypes; | ||
} & React.ButtonHTMLAttributes<HTMLButtonElement>; | ||
|
||
interface SharedButtonProps { | ||
/** | ||
* The visible part of the button, telling the user what | ||
* the action is | ||
*/ | ||
children?: React.ReactNode; | ||
/** | ||
If the button can be activated or not | ||
*/ | ||
disabled?: boolean; | ||
/** | ||
* The display type of the button—inline or block | ||
*/ | ||
display?: Displays; | ||
/** | ||
* SVG icon to place after child content | ||
*/ | ||
iconAfter?: React.ReactNode; | ||
/** | ||
* SVG icon to place before child content | ||
*/ | ||
iconBefore?: React.ReactNode; | ||
/** | ||
* If the button content is only an SVG icon | ||
*/ | ||
iconOnly?: boolean; | ||
/** | ||
* The kind of button - determines how it appears visually | ||
*/ | ||
kind?: Kinds; | ||
/** | ||
* Specifying an href will use an <a> to link to the URL | ||
*/ | ||
href?: string; | ||
/** | ||
* An ARIA Label used for accessibility | ||
*/ | ||
'aria-label'?: string; | ||
/** | ||
* Specifying a to URL will use a react-router Link | ||
*/ | ||
to?: string; | ||
/** | ||
* If using a button, then type is defines the type of button | ||
*/ | ||
type?: ButtonTypes; | ||
/** | ||
* Allows for IconButton to pass `focusable="false"` as a prop for SVGs. | ||
* See @types/react > interface SVGAttributes<T> extends AriaAttributes, DOMAttributes<T> | ||
*/ | ||
focusable?: boolean | 'true' | 'false'; | ||
} | ||
|
||
export type ButtonProps = SharedButtonProps & | ||
React.ButtonHTMLAttributes<HTMLButtonElement> & | ||
React.AnchorHTMLAttributes<HTMLAnchorElement> & | ||
Partial<LinkProps>; | ||
|
||
// The '&&&' will increase the specificity of the | ||
// component's CSS so that it overrides the more | ||
// general global styles | ||
const StyledButton = styled.button` | ||
const StyledButton = styled.button<StyledButtonProps>` | ||
&&& { | ||
font-weight: bold; | ||
display: ${({ display }) => | ||
display === displays.inline ? 'inline-flex' : 'flex'}; | ||
display === Displays.inline ? 'inline-flex' : 'flex'}; | ||
justify-content: center; | ||
align-items: center; | ||
|
||
|
@@ -112,31 +179,29 @@ | |
* A Button performs an primary action | ||
*/ | ||
const Button = ({ | ||
children, | ||
display, | ||
children = null, | ||
display = Displays.block, | ||
href, | ||
kind, | ||
iconBefore, | ||
iconAfter, | ||
iconOnly, | ||
kind = Kinds.primary, | ||
iconBefore = null, | ||
iconAfter = null, | ||
iconOnly = false, | ||
'aria-label': ariaLabel, | ||
to, | ||
type, | ||
type = ButtonTypes.button, | ||
...props | ||
}) => { | ||
}: ButtonProps) => { | ||
const hasChildren = React.Children.count(children) > 0; | ||
const content = ( | ||
<> | ||
{iconBefore} | ||
{hasChildren && <span>{children}</span>} | ||
{hasChildren && !iconOnly && <span>{children}</span>} | ||
{iconAfter} | ||
</> | ||
); | ||
let StyledComponent = StyledButton; | ||
|
||
if (iconOnly) { | ||
StyledComponent = StyledInlineButton; | ||
} | ||
const StyledComponent: React.ElementType = iconOnly | ||
? StyledInlineButton | ||
: StyledButton; | ||
|
||
if (href) { | ||
return ( | ||
|
@@ -181,69 +246,7 @@ | |
); | ||
}; | ||
|
||
Button.defaultProps = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's so great that we can get rid of all of this extra code at the bottom of the React files now haha, thanks for doing this conversion!! |
||
children: null, | ||
disabled: false, | ||
display: displays.block, | ||
iconAfter: null, | ||
iconBefore: null, | ||
iconOnly: false, | ||
kind: kinds.primary, | ||
href: null, | ||
'aria-label': null, | ||
to: null, | ||
type: 'button' | ||
}; | ||
|
||
Button.kinds = kinds; | ||
Button.displays = displays; | ||
|
||
Button.propTypes = { | ||
/** | ||
* The visible part of the button, telling the user what | ||
* the action is | ||
*/ | ||
children: PropTypes.oneOfType([PropTypes.element, PropTypes.string]), | ||
/** | ||
If the button can be activated or not | ||
*/ | ||
disabled: PropTypes.bool, | ||
/** | ||
* The display type of the button—inline or block | ||
*/ | ||
display: PropTypes.oneOf(Object.values(displays)), | ||
/** | ||
* SVG icon to place after child content | ||
*/ | ||
iconAfter: PropTypes.element, | ||
/** | ||
* SVG icon to place before child content | ||
*/ | ||
iconBefore: PropTypes.element, | ||
/** | ||
* If the button content is only an SVG icon | ||
*/ | ||
iconOnly: PropTypes.bool, | ||
/** | ||
* The kind of button - determines how it appears visually | ||
*/ | ||
kind: PropTypes.oneOf(Object.values(kinds)), | ||
/** | ||
* Specifying an href will use an <a> to link to the URL | ||
*/ | ||
href: PropTypes.string, | ||
/** | ||
* An ARIA Label used for accessibility | ||
*/ | ||
'aria-label': PropTypes.string, | ||
/** | ||
* Specifying a to URL will use a react-router Link | ||
*/ | ||
to: PropTypes.string, | ||
/** | ||
* If using a button, then type is defines the type of button | ||
*/ | ||
type: PropTypes.oneOf(['button', 'submit']) | ||
}; | ||
Button.kinds = Kinds; | ||
Button.displays = Displays; | ||
|
||
export default Button; | ||
2 changes: 1 addition & 1 deletion
2
client/common/ButtonOrLink.test.jsx → client/common/ButtonOrLink.test.tsx
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have it just be the boolean type? if the parent that is using this component has the information stored as a string we should have it be the parents' responsibility to cast it to a boolean type before it's passed in