Skip to content

Pr05/migrate client common #3564

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bf21c46
RouterTab: update file type to tsx --no-verify
clairep94 Aug 1, 2025
7ff4aa3
RouterTab.tsx: add types and add router dom types and unit test
clairep94 Aug 1, 2025
551ded1
IconButton: update to tsx --no-verify
clairep94 Aug 1, 2025
835b464
add types to IconButton.tsx --no-verify due to Icon not migrated yet
clairep94 Aug 1, 2025
178b905
IconButton.tsx: add unit test and fix icon resolve
clairep94 Aug 1, 2025
adc04dd
update to use test utils
clairep94 Aug 1, 2025
b60cfdf
ButtonOrLink & test: update to ts, no-verify
clairep94 Aug 2, 2025
916819d
ButtonOrLink: add types
clairep94 Aug 2, 2025
8ac7b2e
export types from converted components
clairep94 Aug 2, 2025
0b05e4f
Button.jsx: update to tsx --no-verify
clairep94 Aug 2, 2025
07eb2b1
Button and IconButton add types --no-verify
clairep94 Aug 2, 2025
b1c90a6
Button.tsx: clean up integration with IconButton
clairep94 Aug 2, 2025
bd69bc7
IconBUtton.tsx: cleanup
clairep94 Aug 2, 2025
8068493
Button.test: add test wip
clairep94 Aug 2, 2025
59f66ac
Button: update unit test and add iconOnly test
clairep94 Aug 2, 2025
9ff1c04
cleanup test
clairep94 Aug 2, 2025
10d68d5
usePrevious: migrate to ts --no-verify
clairep94 Aug 2, 2025
fa64e37
usePrevious: add types
clairep94 Aug 2, 2025
55cc1db
fix warning for any
clairep94 Aug 2, 2025
ad6e8ff
useSyncFormTranslations: update to ts --no-verify
clairep94 Aug 2, 2025
db91425
useSyncTranslations: add types and jsdocs
clairep94 Aug 2, 2025
260277b
useModalClose: update to ts --no-verify
clairep94 Aug 2, 2025
1118d08
fix: remove hardcoded babel parser from prettier so prettier can dete…
clairep94 Aug 2, 2025
c9d9027
useModalClose: update with types
clairep94 Aug 2, 2025
00a91b2
useKeyDownHandler: update to ts --no-verify
clairep94 Aug 2, 2025
e71c25b
useKeyDownHandler: add types and remove unused class componnent helper
clairep94 Aug 2, 2025
1b008a8
device: update extension to ts
clairep94 Jul 26, 2025
e3d6a06
device: add test
clairep94 Jul 26, 2025
c1543fa
device.ts: add edgecase test and refactor
clairep94 Jul 26, 2025
2d9cd7e
useKeyDownHandler: update to use isMac() and cherrypick
clairep94 Jul 26, 2025
25c4516
useKeyDownHandler: clean jsdoc formatting
clairep94 Aug 2, 2025
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 .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"tsx": "never"
}
],
"import/prefer-default-export": "off",
"react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }],
"comma-dangle": 0, // not sure why airbnb turned this on. gross!
"default-param-last": 0,
Expand Down Expand Up @@ -131,7 +132,8 @@
"rules": {
"no-use-before-define": "off",
"import/no-extraneous-dependencies": "off",
"no-unused-vars": "off"
"no-unused-vars": "off",
"react/require-default-props": "off"
}
},
{
Expand Down
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"parser": "babel",
"printWidth": 80,
"proseWrap": "never",
"requirePragma": false,
Expand Down
91 changes: 91 additions & 0 deletions client/common/Button.test.tsx
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();
});
});
170 changes: 89 additions & 81 deletions client/common/Button.jsx → client/common/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,96 @@
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'
};
} as const;

type Kind = keyof typeof kinds;

const displays = {
block: 'block',
inline: 'inline'
} as const;

type Display = keyof typeof displays;

type StyledButtonProps = {
kind: Kind;
display: Display;
};

const buttonTypes = {
button: 'button',
submit: 'submit'
} as const;

type ButtonType = keyof typeof buttonTypes;

type 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?: Display;
/**
* 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?: Kind;
/**
* Specifying an href will use an <a> to link to the URL
*/
href?: string | null;
/**
* An ARIA Label used for accessibility
*/
'aria-label'?: string | null;
/**
* Specifying a to URL will use a react-router Link
*/
to?: string | null;
/**
* If using a button, then type is defines the type of button
*/
type?: ButtonType;
/**
* 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 }) =>
Expand Down Expand Up @@ -112,31 +184,29 @@ const StyledInlineButton = styled.button`
* 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 (
Expand Down Expand Up @@ -181,69 +251,7 @@ const Button = ({
);
};

Button.defaultProps = {
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'])
};

export default Button;
Loading