Skip to content
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

feat: new paginated table component #39

Open
wants to merge 23 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@storybook/react": "^7.6.3",
"@storybook/react-vite": "^7.6.3",
"@storybook/test": "^7.6.3",
"@types/deep-equal": "^1.0.4",
"@types/react": "18.2.18",
"@types/react-dom": "18.2.7",
"@types/rollup-plugin-peer-deps-external": "^2.2.2",
Expand Down
7 changes: 6 additions & 1 deletion src/components/InfiniteTable/useColumnState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ export const useColumnState = ({
persistedColumnState: onGetColumnsState?.(),
});

if (columnsPersistedStateRef.current) {
if (
columnsPersistedStateRef.current &&
columnsPersistedStateRef.current.length > 0
) {
applyPersistedState();
} else {
applyAutoFitState();
}
}, [applyPersistedState, columns, onGetColumnsState]);

Expand Down
125 changes: 125 additions & 0 deletions src/components/PaginatedTable/PaginatedHeaderCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import React, {
forwardRef,
memo,
useCallback,
useEffect,
useImperativeHandle,
useRef,
} from "react";

type CheckboxState = "checked" | "unchecked" | "indeterminate";
export type { CheckboxState };

interface PaginatedHeaderCheckboxCompProps {
onClick: () => void;
}

export interface PaginatedHeaderCheckboxRef {
setState: (state: CheckboxState) => void;
}

// Custom hook to manage checkbox state and click handling
export function usePaginatedHeaderCheckbox(
headerCheckboxState: CheckboxState,
onHeaderCheckboxClick: () => void,
) {
const headerCheckboxRef = useRef<PaginatedHeaderCheckboxRef>(null);
const onHeaderCheckboxClickRef = useRef(onHeaderCheckboxClick);

// Keep the click handler ref up to date
useEffect(() => {
onHeaderCheckboxClickRef.current = onHeaderCheckboxClick;
}, [onHeaderCheckboxClick]);

// Update checkbox state through ref when headerCheckboxState changes
useEffect(() => {
if (headerCheckboxRef.current) {
headerCheckboxRef.current.setState(headerCheckboxState);
}
}, [headerCheckboxState]);

// Memoized header component that uses refs to avoid rerenders
const HeaderComponent = useCallback(
() => (
<PaginatedHeaderCheckbox
ref={headerCheckboxRef}
onClick={() => {
onHeaderCheckboxClickRef.current();
}}
/>
),
[], // No dependencies needed since we're using refs
);

return { HeaderComponent };
}

const PaginatedHeaderCheckboxComp = memo(
forwardRef<PaginatedHeaderCheckboxRef, PaginatedHeaderCheckboxCompProps>(
({ onClick }, ref) => {
const checkboxRef = useRef<HTMLInputElement>(null);

useImperativeHandle(ref, () => ({
setState: (state: CheckboxState) => {
const cbRef = checkboxRef.current;
if (cbRef) {
if (state === "checked") {
cbRef.checked = true;
cbRef.indeterminate = false;
} else if (state === "unchecked") {
cbRef.checked = false;
cbRef.indeterminate = false;
} else if (state === "indeterminate") {
cbRef.checked = false;
cbRef.indeterminate = true;
}
}
},
}));

const handleClick = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
e.stopPropagation();
onClick();
},
[onClick],
);

return (
<input
style={{
width: "13px",
height: "13px",
border: "1px solid grey",
cursor: "pointer",
margin: 0,
}}
onDoubleClick={(e) => {
e.stopPropagation();
}}
ref={checkboxRef}
type="checkbox"
onChange={handleClick}
tabIndex={-1}
/>
);
},
),
);

PaginatedHeaderCheckboxComp.displayName = "PaginatedHeaderCheckboxComp";

interface PaginatedHeaderCheckboxProps {
onClick: () => void;
}

export const PaginatedHeaderCheckbox = memo(
forwardRef<PaginatedHeaderCheckboxRef, PaginatedHeaderCheckboxProps>(
// eslint-disable-next-line react/prop-types
({ onClick }, ref) => {
return <PaginatedHeaderCheckboxComp ref={ref} onClick={onClick} />;
},
),
);

PaginatedHeaderCheckbox.displayName = "PaginatedHeaderCheckbox";
Loading