Skip to content

Commit

Permalink
fix: react-aria crashes when using withFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
sapkra committed Mar 25, 2024
1 parent 1a944e7 commit 8835280
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/components/table/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
import { withFragment } from '../../withFragment'

export const Table = withFragment(NextTable, 'table');
export const TableBody = NextTableBody;
export const TableCell = NextTableCell;
export const TableColumn = NextTableColumn;
export const TableHeader = NextTableHeader;
export const TableRow = NextTableRow;
export const TableBody = withFragment(NextTableBody, 'tableBody');
export const TableCell = withFragment(NextTableCell, 'tableCell');
export const TableColumn = withFragment(NextTableColumn, 'tableColumn');
export const TableHeader = withFragment(NextTableHeader, 'tableHeader');
export const TableRow = withFragment(NextTableRow, 'tableRow');
8 changes: 7 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { TableProps } from "@nextui-org/react";
import { TableBodyProps, TableCellProps, TableColumnProps, TableHeaderProps, TableProps } from "@nextui-org/react";
import { TableRowProps } from "@nextui-org/table/dist/base/table-row";
import { createContext } from "react";

export interface FragmentUIContext {
defaults: {
table?: Partial<TableProps>;
tableBody?: Partial<TableBodyProps<unknown>>;
tableCell?: Partial<TableCellProps>;
tableColumn?: Partial<TableColumnProps<unknown>>;
tableHeader?: Partial<TableHeaderProps<unknown>>;
tableRow?: Partial<TableRowProps>;
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/withFragment.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { useContext } from "react";
import { FragmentUIContext } from "./context";

export const withFragment = <P extends object>(Component: React.ComponentType<P>, configId: keyof FragmentUIContext['defaults']) => (props: P) => {
const context = useContext(FragmentUIContext);
return <Component {...context['defaults'][configId] || {}} {...props} />
export const withFragment = <P extends object>(Component: React.ComponentType<P>, configId: keyof FragmentUIContext['defaults']) => {
const ComponentWithContext = (props: P) => {
const context = useContext(FragmentUIContext);
return <Component {...context['defaults'][configId] || {}} {...props} />
};

// fix react-aria collection
if ('getCollectionNode' in Component) {
ComponentWithContext.getCollectionNode = Component.getCollectionNode;
}

return ComponentWithContext;
};

0 comments on commit 8835280

Please sign in to comment.