Skip to content

Instantly share code, notes, and snippets.

@Jon20111
Created July 17, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jon20111/f35a65e8575a18fef0a58cd2b400f880 to your computer and use it in GitHub Desktop.
Save Jon20111/f35a65e8575a18fef0a58cd2b400f880 to your computer and use it in GitHub Desktop.
Material-Table example
import React, { FC, useState } from "react";
import MaterialTable from "material-table";
import DeleteIcon from "@material-ui/icons/Delete";
import SearchIcon from "@material-ui/icons/Search";
import SaveIcon from "@material-ui/icons/Save";
import { Button } from "@material-ui/core";
import { useStyles } from './material-table-wrapper.styles';
interface Column {
name: string;
occupation: string;
age: number;
}
export const MaterialTableWrapper: FC = () => {
const classes = useStyles();
const [dataStore, setDataStore] = useState([
{ name: "Jon", job: "Software Dev", age: 29 }
]);
return (
<div style={{ maxWidth: '100%' }}>
<MaterialTable
columns={[
{
title: "Name",
field: "name"
},
{
title: "Occupation",
field: "job"
},
{
title: "Age",
field: "age",
type: "numeric"
}
]}
data={dataStore}
title="Material-Table Demo"
icons={{
Clear: (() => <DeleteIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>>,
Search: (() => <SearchIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>>,
ResetSearch: (() => <DeleteIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>>
}}
actions={[
{
icon: () => <SaveIcon />,
tooltip: "Save User",
onClick: (event, rowData) => alert("You saved " + (rowData as unknown as Column).name)
}
]}
components={{
Action: props => (
<Button
onClick={event => props.action.onClick(event, props.data)}
color="primary"
variant="text"
style={{ textTransform: "none" }}
size="small"
>
Save
</Button>
)
}}
options={{
headerStyle: {
backgroundColor: "#01579b",
color: "#FFF"
}
}}
/>
</div>
)
}
@Jon20111
Copy link
Author

See a video of coding a material-table for react here:
https://youtu.be/3bg5w8JNXII

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment