Skip to content

Commit b41b51e

Browse files
WesSouzaarturbien
authored andcommitted
feat(common): convert to TypeScript and export types
1 parent 334cf5e commit b41b51e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+265
-132
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/* eslint-disable react/require-default-props */
2-
/* eslint-disable react/forbid-prop-types */
3-
import React from 'react';
41
import { render } from '@testing-library/react';
5-
import propTypes from 'prop-types';
2+
import React from 'react';
63

74
import useForkRef from './useForkRef';
85

@@ -18,10 +15,10 @@ afterEach(() => {
1815

1916
describe('useForkRef', () => {
2017
it('returns a single ref-setter function that forks the ref to its inputs', () => {
21-
function Component(props) {
18+
function Component(props: { innerRef: React.RefObject<HTMLDivElement> }) {
2219
const { innerRef } = props;
23-
const ownRef = React.useRef(null);
24-
const [, forceUpdate] = React.useState(0);
20+
const ownRef = React.useRef<HTMLDivElement>();
21+
const [, forceUpdate] = React.useState(true);
2522
React.useEffect(() => forceUpdate(n => !n), []);
2623

2724
const handleRef = useForkRef(innerRef, ownRef);
@@ -31,19 +28,18 @@ describe('useForkRef', () => {
3128
);
3229
}
3330

34-
Component.propTypes = {
35-
innerRef: propTypes.any
36-
};
37-
38-
const outerRef = React.createRef();
31+
const outerRef = React.createRef<HTMLDivElement>();
3932
render(<Component innerRef={outerRef} />);
4033

41-
expect(outerRef.current.textContent).toBe('has a ref');
34+
expect(outerRef.current?.textContent).toBe('has a ref');
4235
expect(console.error).not.toHaveBeenCalled();
4336
});
4437

4538
it('forks if only one of the branches requires a ref', () => {
46-
const Component = React.forwardRef(function Component(props, ref) {
39+
const Component = React.forwardRef<HTMLDivElement>(function Component(
40+
_,
41+
ref
42+
) {
4743
const [hasRef, setHasRef] = React.useState(false);
4844
const handleOwnRef = React.useCallback(() => setHasRef(true), []);
4945
const handleRef = useForkRef(handleOwnRef, ref);
@@ -58,16 +54,26 @@ describe('useForkRef', () => {
5854
});
5955

6056
it('does nothing if none of the forked branches requires a ref', () => {
61-
const Outer = React.forwardRef(function Outer(props, ref) {
57+
const setRef = jest.fn();
58+
59+
type OuterProps = {
60+
children: React.ReactElement;
61+
};
62+
63+
const Outer = React.forwardRef<null, OuterProps>(function Outer(
64+
props,
65+
ref
66+
) {
6267
const { children } = props;
63-
const handleRef = useForkRef(children.ref, ref);
6468

65-
return React.cloneElement(children, { ref: handleRef });
66-
});
69+
// TODO: Fix this test as reading ref from children is not allowed so not available on React types
70+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
71+
// @ts-ignore
72+
const handleRef = useForkRef(children?.ref, ref);
73+
setRef(handleRef);
6774

68-
// TODO: Figure out how to make react/no-unused-prop-types happy with the children
69-
// eslint-disable-next-line react/no-unused-prop-types
70-
Outer.propTypes = { children: propTypes.element.isRequired };
75+
return children ? React.cloneElement(children, { ref: handleRef }) : null;
76+
});
7177

7278
function Inner() {
7379
return <div />;
@@ -79,55 +85,56 @@ describe('useForkRef', () => {
7985
</Outer>
8086
);
8187
expect(console.error).not.toHaveBeenCalled();
88+
expect(setRef).toHaveBeenCalledWith(null);
8289
});
8390

8491
describe('changing refs', () => {
85-
function Div(props) {
86-
const { leftRef, rightRef, ...other } = props;
92+
function Div(
93+
props: {
94+
leftRef?: React.Ref<HTMLDivElement>;
95+
rightRef?: React.Ref<HTMLDivElement>;
96+
} & React.HTMLAttributes<HTMLDivElement>
97+
) {
98+
const { leftRef = null, rightRef = null, ...other } = props;
8799
const handleRef = useForkRef(leftRef, rightRef);
88100

89101
return <div {...other} ref={handleRef} />;
90102
}
91103

92-
Div.propTypes = {
93-
leftRef: propTypes.oneOfType([propTypes.func, propTypes.object]),
94-
rightRef: propTypes.oneOfType([propTypes.func, propTypes.object])
95-
};
96-
97104
it('handles changing from no ref to some ref', () => {
98105
const { rerender } = render(<Div id='test' />);
99106

100107
expect(console.error).not.toHaveBeenCalled();
101108

102-
const ref = React.createRef();
109+
const ref = React.createRef<HTMLDivElement>();
103110
rerender(<Div id='test' leftRef={ref} />);
104111

105-
expect(ref.current.id).toBe('test');
112+
expect(ref.current?.id).toBe('test');
106113
expect(console.error).not.toHaveBeenCalled();
107114
});
108115

109116
it('cleans up detached refs', () => {
110-
const firstLeftRef = React.createRef();
111-
const firstRightRef = React.createRef();
112-
const secondRightRef = React.createRef();
117+
const firstLeftRef = React.createRef<HTMLDivElement>();
118+
const firstRightRef = React.createRef<HTMLDivElement>();
119+
const secondRightRef = React.createRef<HTMLDivElement>();
113120

114121
const { rerender } = render(
115122
<Div leftRef={firstLeftRef} rightRef={firstRightRef} id='test' />
116123
);
117124

118125
expect(console.error).not.toHaveBeenCalled();
119126

120-
expect(firstLeftRef.current.id).toBe('test');
121-
expect(firstRightRef.current.id).toBe('test');
127+
expect(firstLeftRef.current?.id).toBe('test');
128+
expect(firstRightRef.current?.id).toBe('test');
122129
expect(secondRightRef.current).toBe(null);
123130

124131
rerender(
125132
<Div leftRef={firstLeftRef} rightRef={secondRightRef} id='test' />
126133
);
127134

128-
expect(firstLeftRef.current.id).toBe('test');
135+
expect(firstLeftRef.current?.id).toBe('test');
129136
expect(firstRightRef.current).toBe(null);
130-
expect(secondRightRef.current.id).toBe('test');
137+
expect(secondRightRef.current?.id).toBe('test');
131138
});
132139
});
133140
});

src/common/hooks/useForkRef.js renamed to src/common/hooks/useForkRef.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Straight out copied from https://github.com./mui-org/material-ui 😂
22
import * as React from 'react';
33

4-
function setRef(ref, value) {
4+
function setRef<T>(
5+
ref: React.RefCallback<T> | React.MutableRefObject<T> | null,
6+
value: T
7+
) {
58
if (typeof ref === 'function') {
69
ref(value);
710
} else if (ref) {
@@ -10,7 +13,10 @@ function setRef(ref, value) {
1013
}
1114
}
1215

13-
export default function useForkRef(refA, refB) {
16+
export default function useForkRef<T>(
17+
refA: React.RefCallback<T> | React.MutableRefObject<T> | null,
18+
refB: React.RefCallback<T> | React.MutableRefObject<T> | null
19+
): React.RefCallback<T> | null {
1420
/**
1521
* This will create a new function if the ref props change and are defined.
1622
* This means react will call the old forkRef with `null` and the new forkRef

src/common/index.js renamed to src/common/index.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { css } from 'styled-components';
1+
import { css, ThemedStyledProps } from 'styled-components';
2+
import { Color, CommonThemeProps, Theme } from '../types';
3+
4+
type CSSProps = ThemedStyledProps<CommonThemeProps, Theme>;
25

36
export const shadow = '4px 4px 10px 0 rgba(0, 0, 0, 0.35)';
47
export const insetShadow = 'inset 2px 2px 3px rgba(0,0,0,0.2)';
@@ -9,12 +12,14 @@ export const createDisabledTextStyles = () => css`
912
text-shadow: 1px 1px ${({ theme }) => theme.materialTextDisabledShadow};
1013
/* filter: grayscale(100%); */
1114
`;
15+
1216
export const createBoxStyles = () => css`
1317
box-sizing: border-box;
1418
display: inline-block;
1519
background: ${({ theme }) => theme.material};
1620
color: ${({ theme }) => theme.materialText};
1721
`;
22+
1823
// TODO for flat box styles add checkered background when disabled (not solid color)
1924
export const createHatchedBackground = ({
2025
mainColor = 'black',
@@ -41,17 +46,19 @@ export const createHatchedBackground = ({
4146
background-size: ${`${pixelSize * 2}px ${pixelSize * 2}px`};
4247
background-position: 0 0, ${`${pixelSize}px ${pixelSize}px`};
4348
`;
49+
4450
export const createFlatBoxStyles = () => css`
4551
position: relative;
4652
box-sizing: border-box;
4753
display: inline-block;
4854
color: ${({ theme }) => theme.materialText};
49-
background: ${({ theme, isDisabled }) =>
55+
background: ${({ theme, isDisabled }: CSSProps) =>
5056
isDisabled ? theme.flatLight : theme.canvas};
5157
border: 2px solid ${({ theme }) => theme.canvas};
5258
outline: 2px solid ${({ theme }) => theme.flatDark};
5359
outline-offset: -4px;
5460
`;
61+
5562
export const createBorderStyles = ({
5663
invert = false,
5764
windowBorders = false
@@ -64,8 +71,8 @@ export const createBorderStyles = ({
6471
border-top-color: ${({ theme }) => theme.borderDarkest};
6572
border-right-color: ${({ theme }) => theme.borderLightest};
6673
border-bottom-color: ${({ theme }) => theme.borderLightest};
67-
box-shadow: ${props => props.shadow && `${shadow}, `} inset 1px 1px 0px
68-
1px ${({ theme }) => theme.borderDark},
74+
box-shadow: ${(props: CSSProps) => props.shadow && `${shadow}, `} inset
75+
1px 1px 0px 1px ${({ theme }) => theme.borderDark},
6976
inset -1px -1px 0 1px ${({ theme }) => theme.borderLight};
7077
`
7178
: css`
@@ -77,12 +84,13 @@ export const createBorderStyles = ({
7784
windowBorders ? theme.borderLight : theme.borderLightest};
7885
border-right-color: ${({ theme }) => theme.borderDarkest};
7986
border-bottom-color: ${({ theme }) => theme.borderDarkest};
80-
box-shadow: ${props => props.shadow && `${shadow}, `} inset 1px 1px 0px
81-
1px
87+
box-shadow: ${(props: CSSProps) => props.shadow && `${shadow}, `} inset
88+
1px 1px 0px 1px
8289
${({ theme }) =>
8390
windowBorders ? theme.borderLightest : theme.borderLight},
8491
inset -1px -1px 0 1px ${({ theme }) => theme.borderDark};
8592
`;
93+
8694
export const createWellBorderStyles = (invert = false) =>
8795
invert
8896
? css`
@@ -106,10 +114,10 @@ export const focusOutline = () => css`
106114
outline: 2px dotted ${({ theme }) => theme.materialText};
107115
`;
108116

109-
const nodeBtoa = b => Buffer.from(b).toString('base64');
117+
const nodeBtoa = (string: string) => Buffer.from(string).toString('base64');
110118
const base64encode = typeof btoa !== 'undefined' ? btoa : nodeBtoa;
111119

112-
const createTriangleSVG = (color, angle = 0) => {
120+
const createTriangleSVG = (color: Color, angle = 0) => {
113121
const svg = `<svg height="26" width="26" viewBox="0 0 26 26" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
114122
<g transform="rotate(${angle} 13 13)">
115123
<polygon fill="${color}" points="6,10 20,10 13,17"/>
File renamed without changes.

src/common/system.js

-8
This file was deleted.

src/common/system.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// TODO - implement styled-system
2+
3+
import { Sizes } from '../types';
4+
5+
export const blockSizes: Record<Sizes, string> = {
6+
sm: '28px',
7+
md: '36px',
8+
lg: '44px'
9+
};

src/common/themes/aiee.js renamed to src/common/themes/aiee.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* https://www.deviantart.com/tpenguinltg/art/Aiee-668092636
33
*/
44

5+
import { Theme } from '../../types';
6+
57
export default {
68
name: 'aiee',
79
anchor: 'rgb(0,0,128)',
@@ -36,4 +38,4 @@ export default {
3638
materialTextInvert: 'rgb(0,62,109)',
3739
progress: 'rgb(251,211,61)',
3840
tooltip: 'rgb(255,243,185)'
39-
};
41+
} as Theme;

src/common/themes/ash.js renamed to src/common/themes/ash.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* "Ash" by tPenguinLTG
22
* https://www.deviantart.com/tpenguinltg/art/Ash-575566643
33
*/
4+
import { Theme } from '../../types';
5+
46
export default {
57
name: 'ash',
68
anchor: 'rgb(192, 192, 192)',
@@ -34,4 +36,4 @@ export default {
3436
materialTextInvert: 'rgb(255, 255, 255)',
3537
progress: 'rgb(0, 0, 0)',
3638
tooltip: 'rgb(0, 0, 0)'
37-
};
39+
} as Theme;

src/common/themes/azureOrange.js renamed to src/common/themes/azureOrange.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '../../types';
2+
13
export default {
24
name: 'azureOrange',
35

@@ -31,4 +33,4 @@ export default {
3133
materialTextInvert: '#000000',
3234
progress: '#F46036',
3335
tooltip: '#fefbcc'
34-
};
36+
} as Theme;

src/common/themes/bee.js renamed to src/common/themes/bee.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '../../types';
2+
13
export default {
24
name: 'bee',
35

@@ -31,4 +33,4 @@ export default {
3133
materialTextInvert: '#ffffff',
3234
progress: '#0C1618',
3335
tooltip: '#fefbcc'
34-
};
36+
} as Theme;

src/common/themes/blackAndWhite.js renamed to src/common/themes/blackAndWhite.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '../../types';
2+
13
export default {
24
name: 'blackAndWhite',
35

@@ -31,4 +33,4 @@ export default {
3133
materialTextInvert: '#ffffff',
3234
progress: '#000000',
3335
tooltip: '#fefbcc'
34-
};
36+
} as Theme;

src/common/themes/blue.js renamed to src/common/themes/blue.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* https://www.deviantart.com/tpenguinltg/art/Blue-525167751
33
*/
44

5+
import { Theme } from '../../types';
6+
57
export default {
68
name: 'blue',
79
anchor: 'rgb(0, 0, 128)',
@@ -36,4 +38,4 @@ export default {
3638
materialTextInvert: 'rgb(255, 255, 255)',
3739
progress: 'rgb(51, 153, 255)',
3840
tooltip: 'rgb(225, 225, 255)'
39-
};
41+
} as Theme;

src/common/themes/brick.js renamed to src/common/themes/brick.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '../../types';
2+
13
export default {
24
name: 'brick',
35

@@ -31,4 +33,4 @@ export default {
3133
materialTextInvert: '#ffffff',
3234
progress: '#8e0101',
3335
tooltip: '#fefbcc'
34-
};
36+
} as Theme;

src/common/themes/candy.js renamed to src/common/themes/candy.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '../../types';
2+
13
export default {
24
name: 'candy',
35

@@ -31,4 +33,4 @@ export default {
3133
materialTextInvert: '#EFF1F3',
3234
progress: '#256EFF',
3335
tooltip: '#fefbcc'
34-
};
36+
} as Theme;

0 commit comments

Comments
 (0)