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

Convert Tab component to TypeScript #5342

Merged
merged 1 commit into from
Oct 2, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import * as React from 'react';
import { isValidElement } from 'react';
import { isValidElement, ReactElement, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import MuiTab from '@material-ui/core/Tab';
import { useTranslate } from 'ra-core';
import MuiTab, { TabProps as MuiTabProps } from '@material-ui/core/Tab';
import { useTranslate, Record } from 'ra-core';
import classnames from 'classnames';

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

const sanitizeRestProps = ({
contentClassName,
label,
icon,
value,
translate,
...rest
}) => rest;

/**
* Tab element for the SimpleShowLayout.
*
Expand Down Expand Up @@ -71,7 +62,7 @@ const Tab = ({
resource,
value,
...rest
}) => {
}: TabProps) => {
const translate = useTranslate();

const renderHeader = () => (
Expand All @@ -81,16 +72,15 @@ const Tab = ({
value={value}
icon={icon}
className={classnames('show-tab', className)}
component={Link}
to={value}
{...sanitizeRestProps(rest)}
{...({ component: Link, to: value } as any)} // to avoid TypeScript screams, see https://github.com/mui-org/material-ui/issues/9106#issuecomment-451270521
{...rest}
/>
);

const renderContent = () => (
<span className={contentClassName}>
{React.Children.map(children, field =>
field && isValidElement(field) ? (
field && isValidElement<any>(field) ? (
<div
key={field.props.source}
className={classnames(
Expand Down Expand Up @@ -137,4 +127,17 @@ Tab.propTypes = {
value: PropTypes.string,
};

export interface TabProps extends MuiTabProps {
basePath?: string;
children: ReactNode;
contentClassName?: string;
context?: 'header' | 'content';
className?: string;
icon?: ReactElement;
label: string;
record?: Record;
resource?: string;
value?: string;
}

export default Tab;
24 changes: 14 additions & 10 deletions packages/ra-ui-materialui/src/detail/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Create from './Create';
import { CreateView } from './Create';
import Create, { CreateView } from './Create';
import CreateActions from './CreateActions';
import Edit from './Edit';
import { EditView } from './Edit';
import Edit, { EditView } from './Edit';
import EditActions from './EditActions';
import EditGuesser from './EditGuesser';
import Show from './Show';
import { ShowView } from './Show';
import ShowActions from './ShowActions';
import Show, { ShowView } from './Show';
import ShowActions, { ShowActionsProps } from './ShowActions';
import ShowGuesser from './ShowGuesser';
import SimpleShowLayout from './SimpleShowLayout';
import TabbedShowLayout from './TabbedShowLayout';
import Tab from './Tab';
import SimpleShowLayout, { SimpleShowLayoutProps } from './SimpleShowLayout';
import TabbedShowLayout, { TabbedShowLayoutProps } from './TabbedShowLayout';
import Tab, { TabProps } from './Tab';
import TabbedShowLayoutTabs from './TabbedShowLayoutTabs';

export {
Expand All @@ -31,3 +28,10 @@ export {
Tab,
TabbedShowLayoutTabs,
};

export type {
SimpleShowLayoutProps,
ShowActionsProps,
TabProps,
TabbedShowLayoutProps,
};