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

Fix server side checked items getter and fix useRequest when paginati… #1693

Merged
merged 1 commit into from
Feb 2, 2023
Merged
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
25 changes: 16 additions & 9 deletions src/components/Table/TableBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,22 @@ export class TableBase<T extends {}> extends React.Component<
);

private getSelectedRows = (selectedRows: T[]) => {
const { rowKey, data } = this.props;

const selectedKeys = this.$getSelectedIdentifiersSet(selectedRows, rowKey);
const checkedItems = this.$getValidatedCheckedItems(
data,
selectedKeys,
rowKey,
selectedRows,
);
const { rowKey, data, pagination } = this.props;
let checkedItems;
if (!pagination?.serverSide) {
const selectedKeys = this.$getSelectedIdentifiersSet(
selectedRows,
rowKey,
);
checkedItems = this.$getValidatedCheckedItems(
data,
selectedKeys,
rowKey,
selectedRows,
);
} else {
checkedItems = selectedRows;
}
const checkedState = this.howManyRowsChecked(checkedItems);

return { checkedItems, checkedState };
Expand Down
22 changes: 19 additions & 3 deletions src/hooks/useRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const useRequest = <
TResult extends ResolvableReturnType<TFn>,
>(
action: TFn,
deps: React.DependencyList,
deps:
| React.DependencyList
| Partial<Record<'persist' | 'reset', React.DependencyList>>,
options: UseRequestOptions,
) => {
const { polling, pollInterval, stopExecution } = options;
Expand All @@ -44,6 +46,11 @@ export const useRequest = <
const [data, setData] = React.useState<TResult>();
const [isVisible, setIsVisible] = React.useState(true);
const pollRef = React.useRef<Poll>();
// Array.isArray does not let TS infer properly for readonly arrays
const loadDataDeps =
'persist' in deps || 'reset' in deps
? [...(deps.persist ?? []), ...(deps.reset ?? [])]
: (deps as React.DependencyList);

React.useEffect(() => {
const handleVisibilityChange = () => setIsVisible(!document.hidden);
Expand All @@ -66,7 +73,7 @@ export const useRequest = <
} finally {
setLoading(false);
}
}, deps);
}, loadDataDeps);

const forcePoll = React.useCallback<ForcePoll>(() => {
setLoading(true);
Expand Down Expand Up @@ -103,8 +110,17 @@ export const useRequest = <
return;
}
setData(undefined);
}, [
...('reset' in deps ? deps.reset ?? [] : (deps as React.DependencyList)),
polling,
]);

React.useEffect(() => {
if (!polling) {
return;
}
forcePoll();
}, [...deps, polling]);
}, loadDataDeps);

const result = [data, loading, error, forcePoll] as UseRequestResult<TResult>;
result.data = data;
Expand Down