mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
Add table and menu components
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { ReactNode, useContext } from 'react';
|
||||
|
||||
// Components
|
||||
import { StyledBody, StyledHeader, StyledRow, StyledTable } from './styled';
|
||||
|
||||
// Interfaces
|
||||
import { ITable, ITableBody } from '../../globals/interfaces';
|
||||
|
||||
// Contexts
|
||||
import TableContext from '../../contexts/TableContext';
|
||||
|
||||
type CallbackMapFunc<T> = (value: T, index: number, array: T[]) => ReactNode;
|
||||
|
||||
const Table = ({ columns, children }: ITable) => {
|
||||
return (
|
||||
<TableContext.Provider value={columns!}>
|
||||
<StyledTable>{children}</StyledTable>
|
||||
</TableContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
const Header = ({ children }: ITable) => {
|
||||
const columns = useContext(TableContext);
|
||||
return <StyledHeader columns={columns}>{children}</StyledHeader>;
|
||||
};
|
||||
|
||||
const Body = <T,>({ data, render }: ITableBody<T>) => {
|
||||
if (data && data.length > 0)
|
||||
return <StyledBody>{data?.map(render as CallbackMapFunc<T>)}</StyledBody>;
|
||||
};
|
||||
|
||||
const Row = ({ children }: ITable) => {
|
||||
const columns = useContext(TableContext);
|
||||
return <StyledRow columns={columns}>{children}</StyledRow>;
|
||||
};
|
||||
|
||||
Table.Header = Header;
|
||||
Table.Body = Body;
|
||||
Table.Row = Row;
|
||||
|
||||
export default Table;
|
||||
Reference in New Issue
Block a user