mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
refactor: move component into a single package (#898)
This commit is contained in:
30
packages/component/src/ui/table/Table.tsx
Normal file
30
packages/component/src/ui/table/Table.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { PropsWithChildren, Children, ReactNode, HTMLAttributes } from 'react';
|
||||
import { StyledTable } from './styles';
|
||||
|
||||
const childrenHasEllipsis = (children: ReactNode | ReactNode[]): boolean => {
|
||||
return Children.toArray(children).some(child => {
|
||||
if (typeof child === 'object' && 'props' in child) {
|
||||
if (!child.props.ellipsis && child.props.children) {
|
||||
return childrenHasEllipsis(child.props.children);
|
||||
}
|
||||
return child.props.ellipsis ?? false;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
export const Table = ({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<HTMLAttributes<HTMLTableElement>>) => {
|
||||
const tableLayout = childrenHasEllipsis(children) ? 'fixed' : 'auto';
|
||||
|
||||
return (
|
||||
<StyledTable tableLayout={tableLayout} {...props}>
|
||||
{children}
|
||||
</StyledTable>
|
||||
);
|
||||
};
|
||||
|
||||
export default Table;
|
||||
11
packages/component/src/ui/table/TableBody.tsx
Normal file
11
packages/component/src/ui/table/TableBody.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { HTMLAttributes, PropsWithChildren } from 'react';
|
||||
import { StyledTableBody } from './styles';
|
||||
|
||||
export const TableBody = ({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<HTMLAttributes<HTMLTableSectionElement>>) => {
|
||||
return <StyledTableBody {...props}>{children}</StyledTableBody>;
|
||||
};
|
||||
|
||||
export default TableBody;
|
||||
14
packages/component/src/ui/table/TableCell.tsx
Normal file
14
packages/component/src/ui/table/TableCell.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import { HTMLAttributes, PropsWithChildren } from 'react';
|
||||
import { TableCellProps } from './interface';
|
||||
import { StyledTableCell } from './styles';
|
||||
|
||||
export const TableCell = ({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<
|
||||
TableCellProps & HTMLAttributes<HTMLTableCellElement>
|
||||
>) => {
|
||||
return <StyledTableCell {...props}>{children}</StyledTableCell>;
|
||||
};
|
||||
|
||||
export default TableCell;
|
||||
11
packages/component/src/ui/table/TableHead.tsx
Normal file
11
packages/component/src/ui/table/TableHead.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { HTMLAttributes, PropsWithChildren } from 'react';
|
||||
import { StyledTableHead } from './styles';
|
||||
|
||||
export const TableHead = ({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<HTMLAttributes<HTMLTableSectionElement>>) => {
|
||||
return <StyledTableHead {...props}>{children}</StyledTableHead>;
|
||||
};
|
||||
|
||||
export default TableHead;
|
||||
11
packages/component/src/ui/table/TableRow.tsx
Normal file
11
packages/component/src/ui/table/TableRow.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { HTMLAttributes, PropsWithChildren } from 'react';
|
||||
import { StyledTableRow } from './styles';
|
||||
|
||||
export const TableRow = ({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<HTMLAttributes<HTMLTableRowElement>>) => {
|
||||
return <StyledTableRow {...props}>{children}</StyledTableRow>;
|
||||
};
|
||||
|
||||
export default TableRow;
|
||||
12
packages/component/src/ui/table/index.ts
Normal file
12
packages/component/src/ui/table/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
// import Table from '@mui/material/Table';
|
||||
// import TableBody from '@mui/material/TableBody';
|
||||
// import TableCell from '@mui/material/TableCell';
|
||||
// import TableHead from '@mui/material/TableHead';
|
||||
// import TableRow from '@mui/material/TableRow';
|
||||
//
|
||||
|
||||
export * from './Table';
|
||||
export * from './TableRow';
|
||||
export * from './TableHead';
|
||||
export * from './TableCell';
|
||||
export * from './TableBody';
|
||||
8
packages/component/src/ui/table/interface.ts
Normal file
8
packages/component/src/ui/table/interface.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { CSSProperties } from 'react';
|
||||
|
||||
export type TableCellProps = {
|
||||
align?: 'left' | 'right' | 'center';
|
||||
ellipsis?: boolean;
|
||||
proportion?: number;
|
||||
style?: CSSProperties;
|
||||
};
|
||||
73
packages/component/src/ui/table/styles.ts
Normal file
73
packages/component/src/ui/table/styles.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { styled, textEllipsis } from '../../styles';
|
||||
import { TableCellProps } from './interface';
|
||||
|
||||
export const StyledTable = styled.table<{ tableLayout: 'auto' | 'fixed' }>(
|
||||
({ theme, tableLayout }) => {
|
||||
return {
|
||||
fontSize: theme.font.base,
|
||||
color: theme.colors.textColor,
|
||||
tableLayout,
|
||||
width: '100%',
|
||||
borderCollapse: 'separate',
|
||||
borderSpacing: '0',
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export const StyledTableBody = styled.tbody(() => {
|
||||
return {
|
||||
fontWeight: 400,
|
||||
};
|
||||
});
|
||||
|
||||
export const StyledTableCell = styled.td<
|
||||
Pick<TableCellProps, 'ellipsis' | 'align' | 'proportion'>
|
||||
>(({ align = 'left', ellipsis = false, proportion }) => {
|
||||
const width = proportion ? `${proportion * 100}%` : 'auto';
|
||||
return {
|
||||
width,
|
||||
height: '52px',
|
||||
lineHeight: '52px',
|
||||
padding: '0 30px',
|
||||
boxSizing: 'border-box',
|
||||
textAlign: align,
|
||||
verticalAlign: 'middle',
|
||||
...(ellipsis ? textEllipsis(1) : {}),
|
||||
overflowWrap: 'break-word',
|
||||
};
|
||||
});
|
||||
|
||||
export const StyledTableHead = styled.thead(() => {
|
||||
return {
|
||||
fontWeight: 500,
|
||||
tr: {
|
||||
':hover': {
|
||||
td: {
|
||||
background: 'unset',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const StyledTableRow = styled.tr(({ theme }) => {
|
||||
return {
|
||||
td: {
|
||||
transition: 'background .15s',
|
||||
},
|
||||
'td:first-of-type': {
|
||||
borderTopLeftRadius: '10px',
|
||||
borderBottomLeftRadius: '10px',
|
||||
},
|
||||
'td:last-of-type': {
|
||||
borderTopRightRadius: '10px',
|
||||
borderBottomRightRadius: '10px',
|
||||
},
|
||||
|
||||
':hover': {
|
||||
td: {
|
||||
background: theme.colors.hoverBackground,
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user