Skip to content

Commit 4ba305d

Browse files
WesSouzaarturbien
authored andcommitted
feat(counter): convert to TypeScript and export types
1 parent cd40930 commit 4ba305d

File tree

7 files changed

+73
-81
lines changed

7 files changed

+73
-81
lines changed

src/Counter/Counter.js

-51
This file was deleted.

src/Counter/Counter.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Bar
33
menu: Components
44
---
55

6-
import Counter from '../Counter/Counter';
6+
import { Counter } from '../Counter/Counter';
77

88
# Counter
99

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import { renderWithTheme } from '../../test/utils';
33

4-
import Counter from './Counter';
4+
import { Counter } from './Counter';
55

66
describe('<Counter />', () => {
77
it('should render', () => {
88
const { container } = renderWithTheme(<Counter />);
9-
const counter = container.firstChild;
9+
const counter = container.firstElementChild;
1010

1111
expect(counter).toBeInTheDocument();
1212
});
@@ -15,15 +15,17 @@ describe('<Counter />', () => {
1515
const { container } = renderWithTheme(
1616
<Counter style={{ backgroundColor: 'papayawhip' }} />
1717
);
18-
const counter = container.firstChild;
18+
const counter = container.firstElementChild;
1919

2020
expect(counter).toHaveAttribute('style', 'background-color: papayawhip;');
2121
});
2222

2323
it('should handle custom props', () => {
24-
const customProps = { title: 'potatoe' };
24+
const customProps: React.HTMLAttributes<HTMLDivElement> = {
25+
title: 'potatoe'
26+
};
2527
const { container } = renderWithTheme(<Counter {...customProps} />);
26-
const counter = container.firstChild;
28+
const counter = container.firstElementChild;
2729

2830
expect(counter).toHaveAttribute('title', 'potatoe');
2931
});
@@ -33,18 +35,18 @@ describe('<Counter />', () => {
3335
const { container } = renderWithTheme(
3436
<Counter value={32} minLength={7} />
3537
);
36-
const counter = container.firstChild;
38+
const counter = container.firstElementChild;
3739

38-
expect(counter.childElementCount).toBe(7);
40+
expect(counter && counter.childElementCount).toBe(7);
3941
});
4042

4143
it('value length takes priority if bigger than minLength', () => {
4244
const { container } = renderWithTheme(
4345
<Counter value={1234} minLength={2} />
4446
);
45-
const counter = container.firstChild;
47+
const counter = container.firstElementChild;
4648

47-
expect(counter.childElementCount).toBe(4);
49+
expect(counter && counter.childElementCount).toBe(4);
4850
});
4951
});
5052
});

src/Counter/Counter.stories.js renamed to src/Counter/Counter.stories.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { useState } from 'react';
1+
import { ComponentMeta } from '@storybook/react';
2+
import { useState } from 'react';
3+
import { Button, Counter, Panel } from 'react95';
24
import styled from 'styled-components';
35

4-
import { Counter, Panel, Button } from 'react95';
5-
66
const Wrapper = styled.div`
77
padding: 5rem;
88
background: ${({ theme }) => theme.desktopBackground};
@@ -23,7 +23,7 @@ export default {
2323
title: 'Counter',
2424
component: Counter,
2525
decorators: [story => <Wrapper>{story()}</Wrapper>]
26-
};
26+
} as ComponentMeta<typeof Counter>;
2727

2828
export function Default() {
2929
const [count, setCount] = useState(13);

src/Counter/Counter.tsx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { forwardRef, useMemo } from 'react';
2+
import styled from 'styled-components';
3+
4+
import { createWellBorderStyles } from '../common';
5+
import { CommonStyledProps, Sizes } from '../types';
6+
import { Digit } from './Digit';
7+
8+
type CounterProps = {
9+
minLength?: number;
10+
size?: Sizes | 'xl';
11+
value?: number;
12+
} & React.HTMLAttributes<HTMLDivElement> &
13+
CommonStyledProps;
14+
15+
const CounterWrapper = styled.div`
16+
${createWellBorderStyles(true)}
17+
display: inline-flex;
18+
background: #000000;
19+
`;
20+
21+
const pixelSizes = {
22+
sm: 1,
23+
md: 2,
24+
lg: 3,
25+
xl: 4
26+
};
27+
28+
const Counter = forwardRef<HTMLDivElement, CounterProps>(function Counter(
29+
{ value = 0, minLength = 3, size = 'md', ...otherProps },
30+
ref
31+
) {
32+
const digits = useMemo(
33+
() => value.toString().padStart(minLength, '0').split(''),
34+
[minLength, value]
35+
);
36+
return (
37+
<CounterWrapper ref={ref} {...otherProps}>
38+
{digits.map((digit, i) => (
39+
<Digit digit={digit} pixelSize={pixelSizes[size]} key={i} />
40+
))}
41+
</CounterWrapper>
42+
);
43+
});
44+
45+
export { Counter, CounterProps };

src/Counter/Digit.js renamed to src/Counter/Digit.tsx

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import React from 'react';
2-
import propTypes from 'prop-types';
32
import styled, { css } from 'styled-components';
43

54
import { createHatchedBackground } from '../common';
5+
import { CommonStyledProps } from '../types';
66

7-
const DigitWrapper = styled.div`
7+
type DigitProps = {
8+
pixelSize?: number;
9+
digit?: number | string;
10+
} & React.HTMLAttributes<HTMLDivElement> &
11+
CommonStyledProps;
12+
13+
const DigitWrapper = styled.div<Required<Pick<DigitProps, 'pixelSize'>>>`
814
position: relative;
915
--react95-digit-primary-color: #ff0102;
1016
--react95-digit-secondary-color: #740201;
@@ -167,8 +173,8 @@ const digitActiveSegments = [
167173
[1, 1, 1, 1, 1, 0, 1] // 9
168174
];
169175

170-
function Digit({ digit, pixelSize, ...otherProps }) {
171-
const segmentClasses = digitActiveSegments[digit].map((isActive, i) =>
176+
function Digit({ digit = 0, pixelSize = 2, ...otherProps }: DigitProps) {
177+
const segmentClasses = digitActiveSegments[Number(digit)].map((isActive, i) =>
172178
isActive ? `${segments[i]} active` : segments[i]
173179
);
174180
return (
@@ -180,14 +186,4 @@ function Digit({ digit, pixelSize, ...otherProps }) {
180186
);
181187
}
182188

183-
Digit.defaultProps = {
184-
pixelSize: 2,
185-
digit: 0
186-
};
187-
188-
Digit.propTypes = {
189-
pixelSize: propTypes.number,
190-
digit: propTypes.oneOfType([propTypes.number, propTypes.string])
191-
};
192-
193-
export default Digit;
189+
export { Digit, DigitProps };

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export * from './Bar/Bar';
1010
export { default as Button } from './Button/Button';
1111
export { default as Checkbox } from './Checkbox/Checkbox';
1212
export { default as ColorInput } from './ColorInput/ColorInput';
13-
export { default as Counter } from './Counter/Counter';
13+
export * from './Counter/Counter';
1414
export { default as Cutout } from './Cutout/Cutout';
1515
export { default as Desktop } from './Desktop/Desktop';
1616
export * from './Divider/Divider';

0 commit comments

Comments
 (0)