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 issue with withQueryModels outside of react router #1376

Merged
merged 3 commits into from
Dec 21, 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
4 changes: 2 additions & 2 deletions packages/components/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@labkey/components",
"version": "3.1.0",
"version": "3.1.1",
"description": "Components, models, actions, and utility functions for LabKey applications and pages",
"sideEffects": false,
"files": [
Expand Down
4 changes: 4 additions & 0 deletions packages/components/releaseNotes/components.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# @labkey/components
Components, models, actions, and utility functions for LabKey applications and pages.

### version 3.1.1
*Released*: 21 December 2023
* withQueryModels: fix issue when operating outside of a React Router context

### version 3.1.0
*Released*: 21 December 2023
- Change `domain` and `security` API wrappers to use `selectRows`
Expand Down
19 changes: 15 additions & 4 deletions packages/components/src/public/QueryModel/withQueryModels.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentType, FC, PureComponent, ReactNode } from 'react';
import React, { ComponentType, FC, PureComponent, ReactNode, useMemo, useCallback } from 'react';
import { Filter } from '@labkey/api';
// eslint cannot find Draft for some reason, but Intellij can.
// eslint-disable-next-line import/named
Expand Down Expand Up @@ -32,9 +32,20 @@ export interface SearchParamsProps {

type WithSearchParamsComponent<T> = ComponentType<T & SearchParamsProps>;

const DEFAULT_SEARCH_PARAMS = new URLSearchParams();
const DEFAULT_SET_SEARCH_PARAMS = () => {};

export function withSearchParams<T>(Component: WithSearchParamsComponent<T>): ComponentType<T> {
const Wrapped: FC<T & SearchParamsProps> = (props: T) => {
const [searchParams, setSearchParams] = useSearchParams();
let searchParams;
let setSearchParams;
try {
[searchParams, setSearchParams] = useSearchParams();
} catch (error) {
// We are not in a react router context, so we revert to injecting a default set of these props
searchParams = DEFAULT_SEARCH_PARAMS;
setSearchParams = DEFAULT_SET_SEARCH_PARAMS;
}
return <Component searchParams={searchParams} setSearchParams={setSearchParams} {...props} />;
};

Expand Down Expand Up @@ -315,8 +326,8 @@ export function withQueryModels<Props>(
bindURL = (id: string): void => {
const { setSearchParams } = this.props;

if (setSearchParams === undefined) {
// This happens when we're rendering a component outside a react-router context.
if (setSearchParams === DEFAULT_SET_SEARCH_PARAMS) {
// We're rendering a component outside a react-router context, so we can't bind to the URL
return;
}

Expand Down