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(ra-ui-materialui): Fix <TabbedFormView/> to support string ids with spaces #10324

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion packages/ra-ui-materialui/src/form/TabbedForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { AdminContext } from '../AdminContext';
import { TabbedForm } from './TabbedForm';
import { TabbedFormClasses } from './TabbedFormView';
import { TextInput } from '../input';
import { EncodedPaths } from './TabbedForm.stories';
import { IDWithSpaces, EncodedPaths } from './TabbedForm.stories';

describe('<TabbedForm />', () => {
it('should display the tabs', () => {
Expand Down Expand Up @@ -49,6 +49,14 @@ describe('<TabbedForm />', () => {
await screen.findByLabelText('Title');
});

it('should display the tab contents correctly with IDs containing spaces', async () => {
render(<IDWithSpaces />);

const tabs = await screen.findAllByRole('tab');
expect(tabs.length).toEqual(2);
await screen.findByLabelText('Title');
});

it('should set the style of an inactive Tab button with errors', async () => {
render(
<TestMemoryRouter initialEntries={['/1']}>
Expand Down
23 changes: 23 additions & 0 deletions packages/ra-ui-materialui/src/form/TabbedForm.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,26 @@ export const EncodedPaths = () => (
</TabbedForm>
</Wrapper>
);

const dataWithSpacesInId = {
id: '1 prod resource1',
title: 'War and Peace',
author: 'Leo Tolstoy',
bio: 'Leo Tolstoy (1828-1910) was a Russian writer who is regarded as one of the greatest authors of all time. He received nominations for the Nobel Prize in Literature every year from 1902 to 1906 and for the Nobel Peace Prize in 1901, 1902, and 1909.',
year: 1869,
};

export const IDWithSpaces = () => (
<Wrapper record={dataWithSpacesInId}>
<TabbedForm>
<TabbedForm.Tab label="main">
<TextInput source="title" />
<TextInput source="author" />
<NumberInput source="year" />
</TabbedForm.Tab>
<TabbedForm.Tab label="details">
<TextInput multiline source="bio" />
</TabbedForm.Tab>
</TabbedForm>
</Wrapper>
);
7 changes: 5 additions & 2 deletions packages/ra-ui-materialui/src/form/TabbedFormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ export const TabbedFormView = (props: TabbedFormViewProps): ReactElement => {
};

/**
* Returns the pathname with each segment decoded
* Returns the pathname with each segment decoded and spaces removed
*/
const getDecodedPathname = (pathname: string) =>
pathname.split('/').map(decodeURIComponent).join('/');
pathname
.split('/')
.map(segment => decodeURIComponent(segment).replace(/\s+/g, ' '))
.join('/');

const DefaultTabs = <TabbedFormTabs />;
const DefaultComponent = ({ children }) => (
Expand Down
Loading