Skip to content

Commit

Permalink
Merge branch 'feat/allow-selection-row-keys-by-props' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Sep 5, 2023
2 parents 0620473 + fe6bb18 commit c0d1d6a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
53 changes: 44 additions & 9 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/react-formiga-table",
"version": "0.8.4",
"version": "0.8.5-rc.0",
"scripts": {
"build": "tsc && vite build",
"prepublishOnly": "npm run build",
Expand All @@ -13,7 +13,8 @@
"dependencies": {
"react": "^16.8.0 || 17.x",
"react-dom": "^16.8.0 || 17.x",
"styled-components": "^5.0.0"
"styled-components": "^5.0.0",
"use-deep-compare-effect": "^1.8.1"
},
"devDependencies": {
"@babel/core": "^7.17.8",
Expand Down
8 changes: 6 additions & 2 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const Table = (props: TableProps) => {
sorter,
expandableOpts,
onCellRender,
readonly
readonly,
selectionRowKeys: selectionRowKeysProps,
} = props;

if (loading) {
Expand All @@ -35,7 +36,10 @@ export const Table = (props: TableProps) => {
toggleAllRowsSelected,
isRowSelected,
changeSelected,
} = useSelectable();
} = useSelectable({
selectionRowKeysProps
});

const onChange = useShiftSelected(dataSource.map(el => el.id), changeSelected);
const { localSorter, getColumnSorter, handleColumnClick } =
useSortable(sorter);
Expand Down
29 changes: 21 additions & 8 deletions src/hooks/useSelectable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { useState, useCallback } from "react";
import { useState, useCallback, useEffect } from "react";
import useDeepCompareEffect from "use-deep-compare-effect";

export const useSelectable = () => {
const [selectedRowKeys, setSelectedRowKeys] = useState<number[]>([]);
export const useSelectable = ({
selectionRowKeysProps = [],
}: {
selectionRowKeysProps?: number[];
}) => {
const [selectedRowKeys, setSelectedRowKeys] = useState<number[]>(
selectionRowKeysProps
);

useDeepCompareEffect(() => {
setSelectedRowKeys(selectionRowKeysProps);
}, [selectionRowKeysProps]);

const toggleAllRowsSelected = useCallback(
(allVisibleKeys: number[]) => {
Expand All @@ -24,11 +35,14 @@ export const useSelectable = () => {
return;
}
if (check) {
setSelectedRowKeys([...new Set([...selectedRowKeys, ...items])])
setSelectedRowKeys([...new Set([...selectedRowKeys, ...items])]);
} else {
setSelectedRowKeys(selectedRowKeys.filter((id: number) => !items.includes(id)))
setSelectedRowKeys(
selectedRowKeys.filter((id: number) => !items.includes(id))
);
}
}, [selectedRowKeys, setSelectedRowKeys]
},
[selectedRowKeys, setSelectedRowKeys]
);

const toggleRowSelected = useCallback(
Expand All @@ -49,7 +63,6 @@ export const useSelectable = () => {

const isRowSelected = useCallback(
(row: any) => {

const selectedFoundRow = selectedRowKeys.find(
(id: number) => row.id === id
);
Expand All @@ -66,4 +79,4 @@ export const useSelectable = () => {
toggleRowSelected,
changeSelected,
};
};
};
1 change: 1 addition & 0 deletions src/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export type TableProps = {
height?: number;
onCellRender?: (opts: OnCellRenderOpts) => React.ReactNode;
readonly?: boolean;
selectionRowKeys?: number[];
};
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
fileName: (format) => `react-formiga-table.${format}.js`,
},
rollupOptions: {
external: ["react", "react-dom", "styled-components"],
external: ["react", "react-dom", "styled-components", "use-deep-compare-effect"],
output: {
globals: {
react: "React",
Expand Down

0 comments on commit c0d1d6a

Please sign in to comment.