Skip to content

Commit

Permalink
Merge pull request #4028 from marmelab/fix-ids-with-parentheses
Browse files Browse the repository at this point in the history
Fix TabbedForm and TabbedShowLayout do not support parenthesis in id
  • Loading branch information
djhi authored Nov 22, 2019
2 parents 83f2b4e + 0cfa1e6 commit 0b08e99
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/ra-core/src/util/escapePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Escape special characters in path so that react-router Route does not do any special treatment
*
* @see https://github.com/ReactTraining/react-router/blob/v3/docs/guides/RouteMatching.md#path-syntax
*
* @example
*
* escapePath('/foo(bar)') => 'foo\(bar\)'
*/
export default url => url.replace(/(\(|\))/g, '\\$1');
2 changes: 2 additions & 0 deletions packages/ra-core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import downloadCSV from './downloadCSV';
import escapePath from './escapePath';
import FieldTitle from './FieldTitle';
import getFetchedAt from './getFetchedAt';
import getFieldLabelTranslationArgs from './getFieldLabelTranslationArgs';
Expand All @@ -16,6 +17,7 @@ import { useSafeSetState, useTimeout } from './hooks';

export {
downloadCSV,
escapePath,
FieldTitle,
getFetchedAt,
getFieldLabelTranslationArgs,
Expand Down
7 changes: 5 additions & 2 deletions packages/ra-ui-materialui/src/detail/TabbedShowLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import PropTypes from 'prop-types';
import Divider from '@material-ui/core/Divider';
import { Route } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import { useRouteMatch } from 'react-router-dom';
import { escapePath } from 'ra-core';

import TabbedShowLayoutTabs, { getTabFullPath } from './TabbedShowLayoutTabs';
import { useRouteMatch } from 'react-router-dom';

const sanitizeRestProps = ({
children,
Expand Down Expand Up @@ -93,7 +94,9 @@ const TabbedShowLayout = ({
tab && isValidElement(tab) ? (
<Route
exact
path={getTabFullPath(tab, index, match.url)}
path={escapePath(
getTabFullPath(tab, index, match.url)
)}
render={() =>
cloneElement(tab, {
context: 'content',
Expand Down
8 changes: 7 additions & 1 deletion packages/ra-ui-materialui/src/form/TabbedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Route } from 'react-router-dom';
import Divider from '@material-ui/core/Divider';
import { makeStyles } from '@material-ui/core/styles';
import {
escapePath,
useTranslate,
useInitializeFormWithRecord,
sanitizeEmptyValues,
Expand Down Expand Up @@ -153,7 +154,12 @@ export const TabbedFormView = ({
children,
(tab, index) =>
tab && (
<Route exact path={getTabFullPath(tab, index, url)}>
<Route
exact
path={escapePath(
getTabFullPath(tab, index, url)
)}
>
{routeProps =>
isValidElement(tab)
? React.cloneElement(tab, {
Expand Down

0 comments on commit 0b08e99

Please sign in to comment.