-
Notifications
You must be signed in to change notification settings - Fork 1
port DataTable functionality from Pro #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"@zenml-io/react-component-library": minor | ||
--- | ||
|
||
port full functionality of DataTable component from pro |
This file contains hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
import React, { useState, createContext, ReactNode, SetStateAction, Dispatch } from "react"; | ||
import { RowSelectionState } from "@tanstack/react-table"; | ||
|
||
export function createDataTableConsumerContext<CtxProviderProps extends { children: ReactNode }>() { | ||
type CtxValue = { | ||
rowSelection: RowSelectionState; | ||
setRowSelection: Dispatch<SetStateAction<RowSelectionState>>; | ||
|
||
selectedRowIDs: string[]; | ||
selectedRowCount: number; | ||
}; | ||
|
||
const Context = createContext<CtxValue | null>(null); | ||
|
||
function useContext() { | ||
const ctx = React.useContext(Context); | ||
if (!ctx) throw new Error("DataTableConsumerContext must be used within its Provider"); | ||
return ctx; | ||
} | ||
|
||
function ContextProvider({ children }: CtxProviderProps) { | ||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({}); | ||
|
||
const selectedRowIDs = getSelectedRowIDs(rowSelection); | ||
const selectedRowCount = selectedRowIDs.length; | ||
|
||
return ( | ||
<Context.Provider value={{ rowSelection, setRowSelection, selectedRowIDs, selectedRowCount }}> | ||
{children} | ||
</Context.Provider> | ||
); | ||
} | ||
|
||
return { | ||
Context, | ||
ContextProvider, | ||
useContext | ||
}; | ||
} | ||
|
||
export function countSelectedRows(rowSelection: RowSelectionState): number { | ||
return Object.values(rowSelection).reduce((acc, curr) => acc + Number(curr), 0); | ||
} | ||
|
||
export function getSelectedRowIDs(rowSelection: RowSelectionState): string[] { | ||
return Object.entries(rowSelection) | ||
.filter(([, selected]) => selected) | ||
.map(([id]) => id); | ||
} |
This file contains hidden or 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,16 @@ | ||
import React from "react"; | ||
|
||
export type SortingArrowIcon = React.FC<{ | ||
Cahllagerfeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
className?: string; | ||
}>; | ||
|
||
export let ArrowUp: SortingArrowIcon = () => <></>; | ||
export let ArrowDown: SortingArrowIcon = () => <></>; | ||
|
||
export function injectSortingArrowIcons(icons: { | ||
ArrowUp: SortingArrowIcon; | ||
ArrowDown: SortingArrowIcon; | ||
}) { | ||
ArrowUp = icons.ArrowUp; | ||
ArrowDown = icons.ArrowDown; | ||
} |
This file contains hidden or 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,37 @@ | ||
import React from "react"; | ||
import { Header, RowData, SortDirection } from "@tanstack/react-table"; | ||
import { PropsWithChildren } from "react"; | ||
import { ArrowUp, ArrowDown } from "./Icons"; | ||
|
||
type Props = { | ||
direction: SortDirection | false; | ||
}; | ||
|
||
function SortingArrow({ direction }: Props) { | ||
if (direction === false) return null; | ||
|
||
const Comp = direction === "asc" ? ArrowUp : ArrowDown; | ||
|
||
return <Comp className="h-4 w-4 shrink-0" />; | ||
Cahllagerfeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
interface HeaderProps<TData extends RowData> { | ||
header: Header<TData, unknown>; | ||
} | ||
|
||
export function SortableHeader<TData extends RowData>({ | ||
header, | ||
children | ||
}: PropsWithChildren<HeaderProps<TData>>) { | ||
return ( | ||
<button | ||
className={`${ | ||
header.column.getIsSorted() ? "text-theme-text-primary" : "" | ||
} flex h-full w-full items-center gap-1 px-4 py-2 text-text-sm transition-all duration-100 hover:bg-gray-200`} | ||
onClick={header.column.getToggleSortingHandler()} | ||
> | ||
<span>{children}</span> | ||
<SortingArrow direction={header.column.getIsSorted()} /> | ||
</button> | ||
); | ||
} |
This file contains hidden or 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,2 +1,5 @@ | ||
export * from "./Table"; | ||
export * from "./DataTable"; | ||
export * from "./DataTableConsumerContext"; | ||
export * from "./Sorting"; | ||
export * from "./Icons"; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think sorting here actually works. any idea what's missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also a
useSorting
hook involved, that is missing here I think 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but that hook in pro is only for syncing the sorting state to the router/url. i don't think it's needed here because it's specific for our use case. or did i miss something and it has logic that makes the sorting work?
i checked it again, i see that the state gets set in the url and then taken in & applied. but can't we have the sorting work w/ local state first, instead of putting it in the url? at least for the example here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I'm not sure tbh. The difference might be that in Pro were doing server-side sorting, while here you want to rely on client-only sorting https://tanstack.com/table/v8/docs/guide/sorting#client-side-vs-server-side-sorting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm that's tough. because in the storybook demo it's not working at all. i'll take a look at how to fix this, but in the meantime i think we can merge, since it'll work fine in our repos?