@openmrs/esm-react-utils 5.3.3-pre.1345 → 5.3.3-pre.1352

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-react-utils",
3
- "version": "5.3.3-pre.1345",
3
+ "version": "5.3.3-pre.1352",
4
4
  "license": "MPL-2.0",
5
5
  "description": "React utilities for OpenMRS.",
6
6
  "browser": "dist/openmrs-esm-react-utils.js",
@@ -57,12 +57,12 @@
57
57
  "swr": "2.x"
58
58
  },
59
59
  "devDependencies": {
60
- "@openmrs/esm-api": "5.3.3-pre.1345",
61
- "@openmrs/esm-config": "5.3.3-pre.1345",
62
- "@openmrs/esm-error-handling": "5.3.3-pre.1345",
63
- "@openmrs/esm-extensions": "5.3.3-pre.1345",
64
- "@openmrs/esm-feature-flags": "5.3.3-pre.1345",
65
- "@openmrs/esm-globals": "5.3.3-pre.1345",
60
+ "@openmrs/esm-api": "5.3.3-pre.1352",
61
+ "@openmrs/esm-config": "5.3.3-pre.1352",
62
+ "@openmrs/esm-error-handling": "5.3.3-pre.1352",
63
+ "@openmrs/esm-extensions": "5.3.3-pre.1352",
64
+ "@openmrs/esm-feature-flags": "5.3.3-pre.1352",
65
+ "@openmrs/esm-globals": "5.3.3-pre.1352",
66
66
  "dayjs": "^1.10.8",
67
67
  "i18next": "^21.10.0",
68
68
  "react": "^18.1.0",
@@ -18,14 +18,14 @@ describe(`ConfigurableLink`, () => {
18
18
  const path = '${openmrsSpaBase}/home';
19
19
  beforeEach(() => {
20
20
  mockNavigate.mockClear();
21
+ });
22
+
23
+ it(`interpolates the link`, async () => {
21
24
  render(
22
25
  <ConfigurableLink to={path} className="fancy-link">
23
26
  SPA Home
24
27
  </ConfigurableLink>,
25
28
  );
26
- });
27
-
28
- it(`interpolates the link`, async () => {
29
29
  const link = screen.getByRole('link', { name: /spa home/i });
30
30
  expect(link).toBeTruthy();
31
31
  expect(link.closest('a')).toHaveClass('fancy-link');
@@ -33,6 +33,11 @@ describe(`ConfigurableLink`, () => {
33
33
  });
34
34
 
35
35
  it(`calls navigate on normal click but not special clicks`, async () => {
36
+ render(
37
+ <ConfigurableLink to={path} className="fancy-link">
38
+ SPA Home
39
+ </ConfigurableLink>,
40
+ );
36
41
  const user = userEvent.setup();
37
42
 
38
43
  const link = screen.getByRole('link', { name: /spa home/i });
@@ -43,6 +48,11 @@ describe(`ConfigurableLink`, () => {
43
48
  });
44
49
 
45
50
  it(`calls navigate on enter`, async () => {
51
+ render(
52
+ <ConfigurableLink to={path} className="fancy-link">
53
+ SPA Home
54
+ </ConfigurableLink>,
55
+ );
46
56
  const user = userEvent.setup();
47
57
 
48
58
  expect(navigate).not.toHaveBeenCalled();
@@ -50,4 +60,19 @@ describe(`ConfigurableLink`, () => {
50
60
  await user.type(link, '{enter}');
51
61
  expect(navigate).toHaveBeenCalledWith({ to: path });
52
62
  });
63
+
64
+ it('executes onBeforeNavigate callback if provided', async () => {
65
+ const onBeforeNavigate = jest.fn();
66
+ render(
67
+ <ConfigurableLink to={path} onBeforeNavigate={onBeforeNavigate}>
68
+ SPA Home
69
+ </ConfigurableLink>,
70
+ );
71
+
72
+ const user = userEvent.setup();
73
+ const link = screen.getByRole('link', { name: /spa home/i });
74
+ await user.click(link);
75
+ expect(onBeforeNavigate).toHaveBeenCalled();
76
+ expect(navigate).toHaveBeenCalledWith({ to: path });
77
+ });
53
78
  });
@@ -3,9 +3,10 @@ import React, { type MouseEvent, type AnchorHTMLAttributes, type PropsWithChildr
3
3
  import type { TemplateParams } from '@openmrs/esm-config';
4
4
  import { navigate, interpolateUrl } from '@openmrs/esm-config';
5
5
 
6
- function handleClick(event: MouseEvent, to: string, templateParams?: TemplateParams) {
6
+ function handleClick(event: MouseEvent, to: string, templateParams?: TemplateParams, onBeforeNavigate?: () => void) {
7
7
  if (!event.metaKey && !event.ctrlKey && !event.shiftKey && event.button == 0) {
8
8
  event.preventDefault();
9
+ onBeforeNavigate?.();
9
10
  navigate({ to, templateParams });
10
11
  }
11
12
  }
@@ -16,25 +17,28 @@ function handleClick(event: MouseEvent, to: string, templateParams?: TemplatePar
16
17
  export interface ConfigurableLinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
17
18
  to: string;
18
19
  templateParams?: TemplateParams;
20
+ onBeforeNavigate?: () => void;
19
21
  }
20
22
 
21
23
  /**
22
24
  * A React link component which calls [[navigate]] when clicked
23
25
  *
24
26
  * @param to The target path or URL. Supports interpolation. See [[navigate]]
25
- * @param urlParams: A dictionary of values to interpolate into the URL, in addition to the default keys `openmrsBase` and `openmrsSpaBase`.
27
+ * @param templateParams: A dictionary of values to interpolate into the URL, in addition to the default keys `openmrsBase` and `openmrsSpaBase`.
28
+ * @param onBeforeNavigate A callback to be called just before navigation occurs
26
29
  * @param children Inline elements within the link
27
30
  * @param otherProps Any other valid props for an <a> tag except `href` and `onClick`
28
31
  */
29
32
  export function ConfigurableLink({
30
33
  to,
31
34
  templateParams,
35
+ onBeforeNavigate,
32
36
  children,
33
37
  ...otherProps
34
38
  }: PropsWithChildren<ConfigurableLinkProps>) {
35
39
  return (
36
40
  <a
37
- onClick={(event) => handleClick(event, to, templateParams)}
41
+ onClick={(event) => handleClick(event, to, templateParams, onBeforeNavigate)}
38
42
  href={interpolateUrl(to, templateParams)}
39
43
  {...otherProps}
40
44
  >