-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
158 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,101 @@ | ||
import React from "react"; | ||
import { TailwindComponentProps, styled } from "@ailiyah-ui/factory"; | ||
import { Link } from "react-router-dom"; | ||
import { AbstractDataType } from "../../handlers"; | ||
|
||
interface TableObj { | ||
[key: string]: any; | ||
const TBody = styled("tbody"); | ||
const TFoot = styled("tfoot"); | ||
const Tr = styled("tr"); | ||
const Td = styled("td"); | ||
const Th = styled("th"); | ||
|
||
const Table = React.memo( | ||
React.forwardRef<HTMLDivElement, TailwindComponentProps<"div">>( | ||
(props, ref) => { | ||
const { children, ...rest } = props; | ||
return ( | ||
<styled.div themeName="TableRoot" {...rest} ref={ref}> | ||
<styled.table themeName="Table">{children}</styled.table> | ||
</styled.div> | ||
); | ||
} | ||
) | ||
); | ||
|
||
interface HeaderOwnProps<T extends AbstractDataType> { | ||
headers: Array<keyof T>; | ||
title?: keyof T; | ||
} | ||
|
||
interface BodyOwnProps<T extends AbstractDataType> { | ||
bodyData: Array<T>; | ||
title?: keyof T; | ||
} | ||
|
||
function Table<T extends TableObj>({ | ||
data, | ||
fields, | ||
}: { | ||
data: Array<T> | null; | ||
fields: Array<keyof T>; | ||
}) { | ||
return ( | ||
<table> | ||
<tbody> | ||
<tr> | ||
{fields ? ( | ||
fields.map((field) => ( | ||
<th key={field.toString()}>{field.toString()}</th> | ||
)) | ||
) : ( | ||
<></> | ||
)} | ||
</tr> | ||
{data && | ||
Object.entries(data).length > 0 && | ||
data.map((item) => ( | ||
<tr key={item.id}> | ||
{fields.map((field) => ( | ||
<td key={field.toString()} className="py-2 px-4"> | ||
{item[field] && item[field].toString()} | ||
</td> | ||
const createHeaders = <T extends AbstractDataType>( | ||
headerProps: HeaderOwnProps<T> | ||
) => { | ||
const THead = React.memo( | ||
React.forwardRef<HTMLTableRowElement, TailwindComponentProps<"thead">>( | ||
(props, ref) => { | ||
let { headers, title = "title" } = headerProps; | ||
headers = headers.filter((item) => item !== title); | ||
return ( | ||
<styled.thead themeName="TableHead" ref={ref} {...props}> | ||
<styled.tr themeName="TableRow"> | ||
<styled.th colSpan={1} themeName="TableHeadHeader"> | ||
{title.toString()} | ||
</styled.th> | ||
{headers && | ||
headers.map((header) => ( | ||
<styled.th colSpan={1} themeName="TableHeadHeader"> | ||
{header.toString()} | ||
</styled.th> | ||
))} | ||
</styled.tr> | ||
</styled.thead> | ||
); | ||
} | ||
) | ||
); | ||
return THead; | ||
}; | ||
|
||
const createTitle = <T extends AbstractDataType>( | ||
bodyProps: BodyOwnProps<T> | ||
) => { | ||
const TBody = React.memo( | ||
React.forwardRef<HTMLTableSectionElement, TailwindComponentProps<"tbody">>( | ||
(props, ref) => { | ||
let { bodyData, title = "title" } = bodyProps; | ||
return ( | ||
<styled.tbody themeName="TableBody" {...props} ref={ref}> | ||
{bodyData && | ||
bodyData && | ||
Object.entries(bodyData).length > 0 && | ||
Object.entries(bodyData).map((item) => ( | ||
<Tr key={item.id?.toString()} themeName="TableBodyRow"> | ||
{fields.map((field) => ( | ||
<Td | ||
key={field.toString()} | ||
themeName="TableBodyData" | ||
colSpan={1} | ||
> | ||
{item[field] && | ||
(field === title ? ( | ||
<Link to={item["id"]}>{item[field].toString()}</Link> | ||
) : ( | ||
item[field].toString() | ||
))} | ||
</Td> | ||
))} | ||
</Tr> | ||
))} | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</styled.tbody> | ||
); | ||
} | ||
) | ||
); | ||
} | ||
}; | ||
|
||
export { Table }; | ||
// export { Table }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { PresetTheme } from "@ailiyah-ui/utils"; | ||
|
||
const tableTheme: PresetTheme = { | ||
TableRoot: { | ||
twWidth: "w-full", | ||
twBorderRadius: "rounded-md", | ||
twBoxShadow: "shadow-md", | ||
twBorderCollapse: "border-collapse", | ||
twFontSize: "text-sm", | ||
twPosition: "relative", | ||
twOverflow: "overflow-x-auto", | ||
}, | ||
Table: { | ||
twWidth: "w-full", | ||
twTextAlign: "text-left", | ||
twTextColor: "text-neutral-500", | ||
twOverflow: "overflow-x-auto", | ||
twTableLayout: "table-auto md:table-fixed", | ||
}, | ||
TableHead: { | ||
twWidth: "w-full", | ||
twFontSize: "text-md", | ||
twBackgroundColor: "bg-neutral-400", | ||
}, | ||
TableHeadRow: { | ||
twTextColor: "text-neutral-700", | ||
twTextTransform: "capitalize", | ||
}, | ||
TableHeadHeader: { | ||
twPadding: "py-2 px-2", | ||
}, | ||
TableBodyRow: { | ||
twBackgroundColor: "hover:bg-neutral-100", | ||
}, | ||
TableBodyData: { | ||
twPadding: "py-2 px-2", | ||
}, | ||
}; | ||
|
||
export { tableTheme }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters