Skip to content

Commit 33f52e9

Browse files
WesSouzaarturbien
authored andcommitted
feat(table,tablebody,tabledatacell,tablehead,tableheadcell,tablerow): convert to TypeScript and export types
1 parent 64e4279 commit 33f52e9

19 files changed

+241
-262
lines changed

src/Table/Table.mdx

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ name: Table
33
menu: Components
44
---
55

6-
import Table from './Table'
7-
import TableBody from '../TableBody/TableBody'
8-
import TableHead from '../TableHead/TableHead'
9-
import TableRow from '../TableRow/TableRow'
10-
import TableHeadCell from '../TableHeadCell/TableHeadCell'
11-
import TableDataCell from '../TableDataCell/TableDataCell'
12-
import Window from '../Window/Window'
13-
import WindowHeader from '../WindowHeader/WindowHeader'
14-
import WindowContent from '../WindowContent/WindowContent'
6+
import { Table } from './Table';
7+
import { TableBody } from '../TableBody/TableBody';
8+
import { TableHead } from '../TableHead/TableHead';
9+
import { TableRow } from '../TableRow/TableRow';
10+
import { TableHeadCell } from '../TableHeadCell/TableHeadCell';
11+
import { TableDataCell } from '../TableDataCell/TableDataCell';
12+
import Window from '../Window/Window';
13+
import WindowHeader from '../WindowHeader/WindowHeader';
14+
import WindowContent from '../WindowContent/WindowContent';
1515

1616
# Table
1717

1818
## Usage
1919

2020
<Playground>
21-
<Window style={{ width: 320 }}>
21+
<Window style={{ width: 320 }}>
2222
<WindowHeader>Pokedex.exe</WindowHeader>
2323
<WindowContent>
2424
<Table>

src/Table/Table.spec.js renamed to src/Table/Table.spec.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import React from 'react';
2-
31
import { renderWithTheme } from '../../test/utils';
42

5-
import Table from './Table';
3+
import { Table } from './Table';
64

75
describe('<Table />', () => {
86
it('renders Table', () => {
97
const { container } = renderWithTheme(<Table />);
10-
const list = container.firstChild;
8+
const table = container.firstElementChild;
119

12-
expect(list).toBeInTheDocument();
10+
expect(table).toBeInTheDocument();
1311
});
1412
it('renders table element', () => {
1513
const { getByRole } = renderWithTheme(<Table />);

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import React from 'react';
2-
import styled from 'styled-components';
3-
1+
import { ComponentMeta } from '@storybook/react';
42
import {
53
Table,
64
TableBody,
5+
TableDataCell,
76
TableHead,
8-
TableRow,
97
TableHeadCell,
10-
TableDataCell,
8+
TableRow,
119
Window,
12-
WindowHeader,
13-
WindowContent
10+
WindowContent,
11+
WindowHeader
1412
} from 'react95';
13+
import styled from 'styled-components';
1514

1615
const Wrapper = styled.div`
1716
padding: 5rem;
@@ -30,7 +29,7 @@ export default {
3029
TableDataCell
3130
},
3231
decorators: [story => <Wrapper>{story()}</Wrapper>]
33-
};
32+
} as ComponentMeta<typeof Table>;
3433

3534
export function Default() {
3635
return (
@@ -39,7 +38,7 @@ export function Default() {
3938
<WindowContent>
4039
<Table>
4140
<TableHead>
42-
<TableRow head>
41+
<TableRow>
4342
<TableHeadCell>Type</TableHeadCell>
4443
<TableHeadCell>Name</TableHeadCell>
4544
<TableHeadCell disabled>Level</TableHeadCell>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import React from 'react';
2-
import propTypes from 'prop-types';
3-
1+
import React, { forwardRef } from 'react';
42
import styled from 'styled-components';
53
import { StyledCutout } from '../Cutout/Cutout';
4+
import { CommonStyledProps } from '../types';
5+
6+
type TableProps = {
7+
children?: React.ReactNode;
8+
} & React.TableHTMLAttributes<HTMLTableElement> &
9+
CommonStyledProps;
610

711
const StyledTable = styled.table`
812
display: table;
@@ -18,9 +22,10 @@ const Wrapper = styled(StyledCutout)`
1822
}
1923
`;
2024

21-
const Table = React.forwardRef(function Table(props, ref) {
22-
const { children, ...otherProps } = props;
23-
25+
const Table = forwardRef<HTMLTableElement, TableProps>(function Table(
26+
{ children, ...otherProps },
27+
ref
28+
) {
2429
return (
2530
<Wrapper>
2631
<StyledTable ref={ref} {...otherProps}>
@@ -30,12 +35,4 @@ const Table = React.forwardRef(function Table(props, ref) {
3035
);
3136
});
3237

33-
Table.defaultProps = {
34-
children: null
35-
};
36-
37-
Table.propTypes = {
38-
children: propTypes.node
39-
};
40-
41-
export default Table;
38+
export { Table, TableProps };

src/TableBody/TableBody.js

-32
This file was deleted.

src/TableBody/TableBody.spec.js renamed to src/TableBody/TableBody.spec.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22

33
import { renderWithTheme } from '../../test/utils';
44

5-
import TableBody from './TableBody';
5+
import { TableBody } from './TableBody';
66

77
describe('<TableBody />', () => {
8-
function mountInTable(node) {
8+
function mountInTable(node: React.ReactNode) {
99
const { container, getByTestId } = renderWithTheme(<table>{node}</table>);
1010
return {
11-
tbody: container.querySelector('table').firstChild,
11+
tbody: container.querySelector('table')?.firstElementChild,
1212
getByTestId
1313
};
1414
}
@@ -17,7 +17,7 @@ describe('<TableBody />', () => {
1717
const { tbody } = mountInTable(<TableBody />);
1818

1919
expect(tbody).toBeInTheDocument();
20-
expect(tbody.tagName).toBe('TBODY');
20+
expect(tbody?.tagName).toBe('TBODY');
2121
});
2222

2323
it('renders children', () => {

src/TableBody/TableBody.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { forwardRef } from 'react';
2+
import styled from 'styled-components';
3+
import { insetShadow } from '../common';
4+
import { CommonStyledProps } from '../types';
5+
6+
type TableBodyProps = {
7+
children?: React.ReactNode;
8+
} & React.HTMLAttributes<HTMLTableSectionElement> &
9+
CommonStyledProps;
10+
11+
const StyledTableBody = styled.tbody`
12+
background: ${({ theme }) => theme.canvas};
13+
display: table-row-group;
14+
box-shadow: ${insetShadow};
15+
overflow-y: auto;
16+
`;
17+
18+
const TableBody = forwardRef<HTMLTableSectionElement, TableBodyProps>(
19+
function TableBody({ children, ...otherProps }, ref) {
20+
return (
21+
<StyledTableBody ref={ref} {...otherProps}>
22+
{children}
23+
</StyledTableBody>
24+
);
25+
}
26+
);
27+
28+
export { TableBody, TableBodyProps };

src/TableDataCell/TableDataCell.js

-26
This file was deleted.

src/TableDataCell/TableDataCell.spec.js renamed to src/TableDataCell/TableDataCell.spec.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React from 'react';
22

33
import { renderWithTheme } from '../../test/utils';
44

5-
import TableDataCell from './TableDataCell';
5+
import { TableDataCell } from './TableDataCell';
66

77
describe('<TableDataCell />', () => {
8-
function mountInTable(node) {
8+
function mountInTable(node: React.ReactNode) {
99
const { container, getByText } = renderWithTheme(
1010
<table>
1111
<tbody>
@@ -14,14 +14,14 @@ describe('<TableDataCell />', () => {
1414
</table>
1515
);
1616
return {
17-
td: container.querySelector('tr').firstChild,
17+
td: container.querySelector('tr')?.firstElementChild,
1818
getByText
1919
};
2020
}
2121

2222
it('renders TableDataCell', () => {
2323
const { td } = mountInTable(<TableDataCell />);
24-
expect(td.tagName).toBe('TD');
24+
expect(td?.tagName).toBe('TD');
2525
});
2626

2727
it('renders children', () => {

src/TableDataCell/TableDataCell.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { forwardRef } from 'react';
2+
import styled from 'styled-components';
3+
import { CommonStyledProps } from '../types';
4+
5+
type TableDataCellProps = {
6+
children?: React.ReactNode;
7+
} & React.HTMLAttributes<HTMLTableCellElement> &
8+
CommonStyledProps;
9+
10+
const StyledTd = styled.td`
11+
padding: 0 8px;
12+
`;
13+
14+
const TableDataCell = forwardRef<HTMLTableCellElement, TableDataCellProps>(
15+
function TableDataCell({ children, ...otherProps }, ref) {
16+
return (
17+
<StyledTd ref={ref} {...otherProps}>
18+
{children}
19+
</StyledTd>
20+
);
21+
}
22+
);
23+
24+
export { TableDataCell, TableDataCellProps };

src/TableHead/TableHead.js

-25
This file was deleted.

src/TableHead/TableHead.spec.js renamed to src/TableHead/TableHead.spec.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22

33
import { renderWithTheme } from '../../test/utils';
44

5-
import TableHead from './TableHead';
5+
import { TableHead } from './TableHead';
66

77
describe('<TableHead />', () => {
8-
function mountInTable(node) {
8+
function mountInTable(node: React.ReactNode) {
99
const { container, getByTestId } = renderWithTheme(<table>{node}</table>);
1010
return {
11-
tbody: container.querySelector('table').firstChild,
11+
tbody: container.querySelector('table')?.firstElementChild,
1212
getByTestId
1313
};
1414
}
@@ -17,7 +17,7 @@ describe('<TableHead />', () => {
1717
const { tbody } = mountInTable(<TableHead />);
1818

1919
expect(tbody).toBeInTheDocument();
20-
expect(tbody.tagName).toBe('THEAD');
20+
expect(tbody?.tagName).toBe('THEAD');
2121
});
2222

2323
it('renders children', () => {

src/TableHead/TableHead.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { forwardRef } from 'react';
2+
import styled from 'styled-components';
3+
import { CommonStyledProps } from '../types';
4+
5+
type TableHeadProps = {
6+
children?: React.ReactNode;
7+
} & React.HTMLAttributes<HTMLTableSectionElement> &
8+
CommonStyledProps;
9+
10+
const StyledTableHead = styled.thead`
11+
display: table-header-group;
12+
`;
13+
const TableHead = forwardRef<HTMLTableSectionElement, TableHeadProps>(
14+
function TableHead({ children, ...otherProps }, ref) {
15+
return (
16+
<StyledTableHead ref={ref} {...otherProps}>
17+
{children}
18+
</StyledTableHead>
19+
);
20+
}
21+
);
22+
23+
export { TableHead, TableHeadProps };

0 commit comments

Comments
 (0)