Material-table: [Docs & Types] Importing icons from @material-ui/icons in TypeScript

Created on 30 May 2019  路  9Comments  路  Source: mbrn/material-table

Importing icons with the way mentioned in tutorial will raise type errors.

import { Icons } from 'material-table';
const tableIcons: Icons = {
  Add: AddBox,
  Check: Check,
  Clear: Clear,
  Delete: DeleteOutline,
  DetailPanel: ChevronRight,
  Edit: Edit,
  Export: SaveAlt,
  Filter: FilterList,
  FirstPage: FirstPage,
  LastPage: LastPage,
  NextPage: ChevronRight,
  PreviousPage: ChevronLeft,
  ResetSearch: Clear,
  Search: Search,
  SortArrow: ArrowUpward,
  ThirdStateCheck: Remove,
  ViewColumn: ViewColumn
};
// Type 'ComponentType<SvgIconProps>' is not assignable to type '(() => ReactElement<any, string ...

The correct way should be

const tableIcons: Icons = {
  Add: () => <AddBox />,
  Check: () => <Check />,
  Clear: () => <Clear />,
  Delete: () => <DeleteOutline />,
  DetailPanel: () => <ChevronRight />,
  Edit: () => <Edit />,
  Export: () => <SaveAlt />,
  Filter: () => <FilterList />,
  FirstPage: () => <FirstPage />,
  LastPage: () => <LastPage />,
  NextPage: () => <ChevronRight />,
  PreviousPage: () => <ChevronLeft />,
  ResetSearch: () => <Clear />,
  Search: () => <Search />,
  SortArrow: () => <ArrowUpward />,
  ThirdStateCheck: () => <Remove />,
  ViewColumn: () => <ViewColumn />
};

It seems that the type of icons are declared as Add?: () => React.ReactElement<any>;, which forces imports to be arrow functions, but the icons are exported from @material-ui/icons as React.ComponentType<SvgIconProps>

bug

Most helpful comment

@robinlarsson Me too

All 9 comments

I can change them React.Type. Thanks.

I am also using TypeScript and been struggling with the icons. I have the same solution as @hinsxd and get issues with tooltip:

Warning: Failed prop type: Invalid propchildrensupplied toTooltip. Expected an element that can hold a ref. Did you accidentally use a plain function component for an element instead? For more information see https://material-ui.com/guides/composition/#caveat-with-refs

@robinlarsson Me too

@mbrn any update on this issue? I am having the same problem as @robinlarsson and @argentinaluiz. The only two icons which throw the error are the Filter and Search Icons:

const icons = {
  Filter: props => <FilterList {...props} />,
  Search: props => <Search {...props} />
}

I am also using TypeScript and been struggling with the icons. I have the same solution as @hinsxd and get issues with tooltip:

Warning: Failed prop type: Invalid propchildrensupplied toTooltip. Expected an element that can hold a ref. Did you accidentally use a plain function component for an element instead? For more information see https://material-ui.com/guides/composition/#caveat-with-refs

I would like to know if there is any resolution on this. We are trying to use the same thing with TypeScript.

I have imported the Icons correctly, and added them according to "Correct way it should be" but receiving an error that reads Generic type 'MaterialTable<RowData>' requires 1 type argument(s).

I got this working by declaring like this:

import ShowChartIconOutlined from '@material-ui/icons/ShowChartOutlined';
import { SvgIconProps } from '@material-ui/core';

type Menu = {
  id: number;
  icon: (props: SvgIconProps) => JSX.Element;
  label: string;
}[];

const NavBarMenus: Menu = [
  {
    id: 1,
    icon: ShowChartIconOutlined,
    label: 'Dashboard',
  },
...
];

See this comment in related issue mbrn/material-table#1004 for a workaround.

Closing due to updated docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

balibou picture balibou  路  3Comments

timrchavez picture timrchavez  路  3Comments

ModPhoenix picture ModPhoenix  路  3Comments

diegosps picture diegosps  路  3Comments

behrouz-s picture behrouz-s  路  3Comments