Skip to content

Commit e645ec6

Browse files
authored
Merge pull request #5342 from marmelab/tab-typescript
Convert Tab component to TypeScript
2 parents 55cc1e1 + a466e70 commit e645ec6

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

packages/ra-ui-materialui/src/detail/Tab.js packages/ra-ui-materialui/src/detail/Tab.tsx

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import * as React from 'react';
2-
import { isValidElement } from 'react';
2+
import { isValidElement, ReactElement, ReactNode } from 'react';
33
import PropTypes from 'prop-types';
44
import { Link } from 'react-router-dom';
5-
import MuiTab from '@material-ui/core/Tab';
6-
import { useTranslate } from 'ra-core';
5+
import MuiTab, { TabProps as MuiTabProps } from '@material-ui/core/Tab';
6+
import { useTranslate, Record } from 'ra-core';
77
import classnames from 'classnames';
88

99
import Labeled from '../input/Labeled';
1010

11-
const sanitizeRestProps = ({
12-
contentClassName,
13-
label,
14-
icon,
15-
value,
16-
translate,
17-
...rest
18-
}) => rest;
19-
2011
/**
2112
* Tab element for the SimpleShowLayout.
2213
*
@@ -71,7 +62,7 @@ const Tab = ({
7162
resource,
7263
value,
7364
...rest
74-
}) => {
65+
}: TabProps) => {
7566
const translate = useTranslate();
7667

7768
const renderHeader = () => (
@@ -81,16 +72,15 @@ const Tab = ({
8172
value={value}
8273
icon={icon}
8374
className={classnames('show-tab', className)}
84-
component={Link}
85-
to={value}
86-
{...sanitizeRestProps(rest)}
75+
{...({ component: Link, to: value } as any)} // to avoid TypeScript screams, see https://github.com/mui-org/material-ui/issues/9106#issuecomment-451270521
76+
{...rest}
8777
/>
8878
);
8979

9080
const renderContent = () => (
9181
<span className={contentClassName}>
9282
{React.Children.map(children, field =>
93-
field && isValidElement(field) ? (
83+
field && isValidElement<any>(field) ? (
9484
<div
9585
key={field.props.source}
9686
className={classnames(
@@ -137,4 +127,17 @@ Tab.propTypes = {
137127
value: PropTypes.string,
138128
};
139129

130+
export interface TabProps extends MuiTabProps {
131+
basePath?: string;
132+
children: ReactNode;
133+
contentClassName?: string;
134+
context?: 'header' | 'content';
135+
className?: string;
136+
icon?: ReactElement;
137+
label: string;
138+
record?: Record;
139+
resource?: string;
140+
value?: string;
141+
}
142+
140143
export default Tab;

packages/ra-ui-materialui/src/detail/index.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import Create from './Create';
2-
import { CreateView } from './Create';
1+
import Create, { CreateView } from './Create';
32
import CreateActions from './CreateActions';
4-
import Edit from './Edit';
5-
import { EditView } from './Edit';
3+
import Edit, { EditView } from './Edit';
64
import EditActions from './EditActions';
75
import EditGuesser from './EditGuesser';
8-
import Show from './Show';
9-
import { ShowView } from './Show';
10-
import ShowActions from './ShowActions';
6+
import Show, { ShowView } from './Show';
7+
import ShowActions, { ShowActionsProps } from './ShowActions';
118
import ShowGuesser from './ShowGuesser';
12-
import SimpleShowLayout from './SimpleShowLayout';
13-
import TabbedShowLayout from './TabbedShowLayout';
14-
import Tab from './Tab';
9+
import SimpleShowLayout, { SimpleShowLayoutProps } from './SimpleShowLayout';
10+
import TabbedShowLayout, { TabbedShowLayoutProps } from './TabbedShowLayout';
11+
import Tab, { TabProps } from './Tab';
1512
import TabbedShowLayoutTabs from './TabbedShowLayoutTabs';
1613

1714
export {
@@ -31,3 +28,10 @@ export {
3128
Tab,
3229
TabbedShowLayoutTabs,
3330
};
31+
32+
export type {
33+
SimpleShowLayoutProps,
34+
ShowActionsProps,
35+
TabProps,
36+
TabbedShowLayoutProps,
37+
};

0 commit comments

Comments
 (0)