Skip to content

Commit c6f5a36

Browse files
authored
chore(ui-react): lint primitives/utils (#2334)
* chore(ui-react): lint primitives/utils
1 parent c8f6300 commit c6f5a36

File tree

7 files changed

+23
-24
lines changed

7 files changed

+23
-24
lines changed

packages/react/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = {
6969
'src/components/**/*',
7070
'src/helpers/**/*',
7171
'src/hooks/**/*',
72-
'src/primitives/shared/**/*',
72+
'src/primitives/+(shared|utils)/**/*',
7373
// 'src/primitives/**/*',
7474
],
7575
extends: [

packages/react/src/primitives/Checkbox/Checkbox.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { splitPrimitiveProps } from '../shared/styleUtils';
1212
import { Text } from '../Text';
1313
import { useCheckbox } from './useCheckbox';
1414
import { VisuallyHidden } from '../VisuallyHidden';
15-
import { useTestId } from '../utils/testUtils';
15+
import { getTestId } from '../utils/testUtils';
1616

1717
const CheckboxPrimitive: Primitive<CheckboxProps, 'input'> = (
1818
{
@@ -47,9 +47,9 @@ const CheckboxPrimitive: Primitive<CheckboxProps, 'input'> = (
4747
}
4848
}, [checked, dataChecked, setDataChecked]);
4949

50-
const buttonTestId = useTestId(testId, ComponentClassNames.CheckboxButton);
51-
const iconTestId = useTestId(testId, ComponentClassNames.CheckboxIcon);
52-
const labelTestId = useTestId(testId, ComponentClassNames.CheckboxLabel);
50+
const buttonTestId = getTestId(testId, ComponentClassNames.CheckboxButton);
51+
const iconTestId = getTestId(testId, ComponentClassNames.CheckboxIcon);
52+
const labelTestId = getTestId(testId, ComponentClassNames.CheckboxLabel);
5353
const flexClasses = classNames(
5454
ComponentClassNames.CheckboxButton,
5555
classNameModifierByFlag(

packages/react/src/primitives/Checkbox/__tests__/Checkbox.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
testFlexProps,
1111
expectFlexContainerStyleProps,
1212
} from '../../Flex/__tests__/Flex.test';
13-
import { useTestId } from '../../utils/testUtils';
1413

1514
describe('Checkbox test suite', () => {
1615
const basicProps = {

packages/react/src/primitives/CheckboxField/CheckboxField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CheckboxFieldProps, Primitive } from '../types';
77
import { ComponentClassNames } from '../shared';
88
import { FieldErrorMessage } from '../Field';
99
import { Flex } from '../Flex';
10-
import { useTestId } from '../utils/testUtils';
10+
import { getTestId } from '../utils/testUtils';
1111

1212
const CheckboxFieldPrimitive: Primitive<CheckboxFieldProps, 'input'> = (
1313
{
@@ -22,7 +22,7 @@ const CheckboxFieldPrimitive: Primitive<CheckboxFieldProps, 'input'> = (
2222
},
2323
ref
2424
) => {
25-
const checkboxTestId = useTestId(testId, ComponentClassNames.Checkbox);
25+
const checkboxTestId = getTestId(testId, ComponentClassNames.Checkbox);
2626
return (
2727
<Flex
2828
className={classNames(
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
import * as React from 'react';
2-
export const useTestId = (testId: string, component: string) => {
3-
const newTestId = React.useMemo(
4-
() => (testId && component ? `${testId}-${component}` : undefined),
5-
[testId, component]
6-
);
7-
return newTestId;
8-
};
1+
export const getTestId = (testId: string, component: string): string =>
2+
testId && component ? `${testId}-${component}` : undefined;
93

10-
export const errorMessageWrapper = (fn: () => void, message: string) => {
4+
export const errorMessageWrapper = (
5+
fn: () => void,
6+
message: string
7+
): void | Error => {
118
try {
129
fn();
1310
} catch (error) {
1411
// Formatting below is intentional
1512
// and displays below Jest error message
16-
error.message += `
13+
(error as Error).message += `
1714
1815
-- Custom Error Message --
1916
${message}
2017
2118
`;
19+
// eslint-disable-next-line no-console
2220
console.error(error);
23-
throw new Error(error);
21+
throw new Error((error as Error).message);
2422
}
2523
};

packages/react/src/primitives/utils/useLayoutEffect.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import * as React from 'react';
88
*
99
* See: https://reactjs.org/docs/hooks-reference.html#uselayouteffect
1010
*/
11-
const useLayoutEffect = Boolean(globalThis?.document)
12-
? React.useLayoutEffect
13-
: () => {};
11+
const useLayoutEffect = globalThis?.document ? React.useLayoutEffect : () => {};
1412

1513
export { useLayoutEffect };

packages/react/src/primitives/utils/useStableId.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export type UseStableId = (id?: string) => string;
99

1010
// Create a local version of React.useId which will reference React.useId for React 18
1111
// and fallback to noop for React 17 and below
12-
// Note: We `toString()` to prevent bundlers from trying to `import { useId } from 'react';`
12+
// Note: We use `toString()` to prevent bundlers from trying to `import { useId } from 'react';`
1313
// since it doesn't exist in React 17 and below (prevents https://github.com/aws-amplify/amplify-ui/issues/1154)
1414
const useReactId: () => string | (() => undefined) =
15-
(React as any)['useId'.toString()] || (() => undefined);
15+
// disable eslint below to allow usage of casting React to `any`, which ensures that TS
16+
// does not get confused about the existence of `useId` in React 17 and below
17+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
18+
((React as any)['useId'.toString()] as () => string) || (() => undefined);
19+
1620
let count = 0;
1721

1822
/**

0 commit comments

Comments
 (0)