Skip to content

Commit 070b920

Browse files
authored
Merge pull request #11360 from marmelab/feat/migrate_useFormRootPath_to_ra-core
feat: Migrate useFormRootPath to ra-core
2 parents 062f3fd + 151b46d commit 070b920

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎packages/ra-core/src/form/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './validation';
1212
export * from './WarnWhenUnsavedChanges';
1313
export * from './FilterLiveForm';
1414
export * from './useFormIsDirty';
15+
export * from './useFormRootPath';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
4+
import { useFormRootPath } from './useFormRootPath';
5+
import { TestMemoryRouter } from '../routing';
6+
7+
describe('useFormRootPath', () => {
8+
const UseFormRootPath = () => <div>{`[${useFormRootPath()}]`}</div>;
9+
10+
const renderAt = (pathname: string) =>
11+
render(
12+
<TestMemoryRouter initialEntries={[pathname]}>
13+
<UseFormRootPath />
14+
</TestMemoryRouter>
15+
);
16+
17+
it('returns the create form root path', async () => {
18+
renderAt('/posts/create');
19+
20+
await screen.findByText('[/posts/create]');
21+
});
22+
23+
it('returns the create form root path when on a tab', async () => {
24+
renderAt('/posts/create/2');
25+
26+
await screen.findByText('[/posts/create]');
27+
});
28+
29+
it('returns the edit form root path', async () => {
30+
renderAt('/posts/123');
31+
32+
await screen.findByText('[/posts/123]');
33+
});
34+
35+
it('returns the edit form root path when on a tab', async () => {
36+
renderAt('/posts/123/2');
37+
38+
await screen.findByText('[/posts/123]');
39+
});
40+
41+
it('returns an empty string when the location matches no form', async () => {
42+
renderAt('/posts');
43+
44+
await screen.findByText('[]');
45+
});
46+
});

packages/ra-ui-materialui/src/form/useFormRootPath.ts renamed to packages/ra-core/src/form/useFormRootPath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useLocation, useMatchPath } from 'ra-core';
1+
import { useLocation, useMatchPath } from '../routing';
22

33
/**
44
* This hook infers the tabbed form root path from the current location.

‎packages/ra-ui-materialui/src/form/TabbedForm.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import {
66
ReactNode,
77
HtmlHTMLAttributes,
88
} from 'react';
9-
import { Form, FormProps, MutationMode, RaRecord } from 'ra-core';
9+
import {
10+
Form,
11+
FormProps,
12+
MutationMode,
13+
RaRecord,
14+
useFormRootPath,
15+
} from 'ra-core';
1016
import get from 'lodash/get.js';
1117

1218
import { TabbedFormView, TabbedFormViewProps } from './TabbedFormView';
13-
import { useFormRootPath } from './useFormRootPath';
1419
import { FormTab } from './FormTab';
1520

1621
/**

0 commit comments

Comments
 (0)