select row added

master
mahdi 3 years ago
parent 4dc6f2f540
commit 0d664a559d
  1. 236
      src/components/ReactTable3/BasicTable.js
  2. 16
      src/components/ReactTable3/Checkbox.js

@ -1,18 +1,26 @@
import React, {useMemo} from 'react'; import React, { useMemo } from "react";
import {useTable, useSortBy, useGlobalFilter, useFilters, usePagination} from 'react-table'; import {
useTable,
useSortBy,
useGlobalFilter,
useFilters,
usePagination,
useRowSelect,
useColumnOrder,
} from "react-table";
import {COLUMNS} from './columns'; import { COLUMNS } from "./columns";
import {GlobalFilter} from './GlobalFilter'; import { GlobalFilter } from "./GlobalFilter";
import MOCK_DATA from './MOCK_DATA.json'; import { Checkbox } from "./Checkbox";
const BasicTable = () => { import MOCK_DATA from "./MOCK_DATA.json";
const BasicTable = () => {
const columns = React.useMemo(() => COLUMNS, []) const columns = React.useMemo(() => COLUMNS, []);
const data = useMemo(() => MOCK_DATA,[]) const data = useMemo(() => MOCK_DATA, []);
const { const {
getTableProps, getTableProps,
@ -30,9 +38,11 @@ const BasicTable = () => {
pageCount, pageCount,
setPageSize, setPageSize,
prepareRow, prepareRow,
selectedFlatRows,
state, state,
setGlobalFilter, setGlobalFilter,
} = useTable({ } = useTable(
{
columns, columns,
data, data,
// initialState: { pageIndex: 2 } // initialState: { pageIndex: 2 }
@ -41,27 +51,53 @@ const BasicTable = () => {
useGlobalFilter, useGlobalFilter,
useSortBy, useSortBy,
usePagination, usePagination,
) useRowSelect,
(hooks) => {
hooks.visibleColumns.push((columns) => {
return [
{
id: "selection",
Header: ({ getToggleAllRowsSelectedProps }) => (
<Checkbox {...getToggleAllRowsSelectedProps()} />
),
Cell: ({ row }) => (
<Checkbox {...row.getToggleRowSelectedProps()} />
),
},
...columns,
];
});
}
);
const { globalFilter, pageIndex, pageSize } = state const { globalFilter, pageIndex, pageSize } = state;
const firstPageRows = rows.slice(0, 10);
return ( return (
<> <>
<GlobalFilter filter={globalFilter} setFilter={setGlobalFilter} /> <GlobalFilter filter={globalFilter} setFilter={setGlobalFilter} />
<table {...getTableProps()} className="min-w-full divide-y divide-gray-200"> <table
{...getTableProps()}
className="min-w-full divide-y divide-gray-200"
>
<thead className="bg-gray-300"> <thead className="bg-gray-300">
{headerGroups.map((headerGroup) => ( {headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()}> <tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => ( {headerGroup.headers.map((column) => (
<th {...column.getHeaderProps(column.getSortByToggleProps())} scope="col" className="cursor-pointer px-4 py-2 text-right text-xs" style={{color: '#3287B9'}}> <th
{column.render('Header')} {...column.getHeaderProps(column.getSortByToggleProps())}
scope="col"
className="cursor-pointer px-4 py-2 text-right text-xs"
style={{ color: "#3287B9" }}
>
{column.render("Header")}
<span> <span>
{column.isSorted ? (column.isSortedDesc ? ' 🔽' : ' 🔼') : ''} {column.isSorted
? column.isSortedDesc
? " 🔽"
: " 🔼"
: ""}
</span> </span>
{/* <div> {/* <div>
{column.canFilter ? column.render('Filter') : null } {column.canFilter ? column.render('Filter') : null }
@ -72,7 +108,10 @@ const BasicTable = () => {
))} ))}
</thead> </thead>
<tbody {...getTableBodyProps()} className="bg-white divide-y divide-gray-200"> <tbody
{...getTableBodyProps()}
className="bg-white divide-y divide-gray-200"
>
{/* {rows.map((row) => { {/* {rows.map((row) => {
prepareRow(row) prepareRow(row)
return( return(
@ -86,96 +125,139 @@ const BasicTable = () => {
) )
})} */} })} */}
{page.map((row) => { {page.map((row) => {
prepareRow(row) prepareRow(row);
return( return (
<tr {...row.getRowProps()}> <tr {...row.getRowProps()}>
{row.cells.map((cell) => { {row.cells.map((cell) => {
return <td {...cell.getCellProps()} className="px-4 py-4 whitespace-nowrap text-sm" role="cell" style={{color:'#00253A'}}> return (
{cell.render('Cell')} <td
{...cell.getCellProps()}
className="px-4 py-4 whitespace-nowrap text-sm"
role="cell"
style={{ color: "#00253A" }}
>
{cell.render("Cell")}
</td> </td>
);
})} })}
</tr> </tr>
) );
})} })}
{/* {firstPageRows.map((row) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<td
{...cell.getCellProps()}
className="px-4 py-4 whitespace-nowrap text-sm"
role="cell"
style={{ color: "#00253A" }}
>
{cell.render("Cell")}
</td>
);
})}
</tr>
);
})} */}
</tbody> </tbody>
<tfoot className={`bg-gray-300 ${rows.length > 10 ? 'w-full' : 'hidden'}`} > <tfoot
{ className={`bg-gray-300 ${rows.length > 10 ? "w-full" : "hidden"}`}
footerGroups.map(footerGroup => ( >
{footerGroups.map((footerGroup) => (
<tr {...footerGroup.getFooterGroupProps()}> <tr {...footerGroup.getFooterGroupProps()}>
{ {footerGroup.headers.map((column) => (
footerGroup.headers.map(column => ( <td
<td {...column.getFooterProps} className="px-4 py-2 text-right text-xs" style={{color: '#3287B9'}}> {...column.getFooterProps}
{ className="px-4 py-2 text-right text-xs"
column.render('Footer') style={{ color: "#3287B9" }}
} >
{column.render("Footer")}
</td> </td>
)) ))}
}
</tr> </tr>
)) ))}
}
</tfoot> </tfoot>
</table> </table>
<div> <pre>
<code>
{JSON.stringify(
{
selectedFlatRows: selectedFlatRows.map((row) => row.original),
},
null,
2
)}
</code>
</pre>
<div>
<span> <span>
Page{" "}
Page{' '}
<strong> <strong>
{pageIndex + 1} of {pageOptions.length} {pageIndex + 1} of {pageOptions.length}
</strong>{''} </strong>
{""}
</span> </span>
<span> <span>
| Go to Page:{" "}
| Go to Page: {' '} <input
type="number"
<input type='number' defaultValue={pageIndex + 1}
defaultValue={ pageIndex + 1 } onChange={(e) => {
onChange={ (e) => { const pageNumber = e.target.value
const pageNumber = e.target.value ? Number(e.target.value) - 1 : 0 ? Number(e.target.value) - 1
gotoPage(pageNumber) : 0;
} } gotoPage(pageNumber);
style={{ width: '50px' }} }}
style={{ width: "50px" }}
/> />
</span> </span>
<select <select
value={pageSize} value={pageSize}
onChange={ (e) => setPageSize(Number(e.target.value)) } onChange={(e) => setPageSize(Number(e.target.value))}
> >
{[7, 10, 25, 50].map((pageSize) => (
{ <option key={pageSize} value={pageSize}>
[7, 10, 25, 50].map(pageSize => ( Show {pageSize}
<option key={ pageSize } value={ pageSize }>
Show { pageSize }
</option> </option>
))}
))
}
</select> </select>
<button onClick={() => gotoPage(0)} disabled={!canPreviousPage}> { '<<' } </button> <button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{" "}
<button className="bg-blue-400 m-2 rounded text-white p-2" onClick={() => previousPage() } disabled={!canPreviousPage} >Previous</button> {"<<"}{" "}
<button className="bg-blue-400 m-2 rounded text-white p-2" onClick={() => nextPage() } disabled={!canNextPage}>Next</button> </button>
<button onClick={() => gotoPage( pageCount - 1 )} disabled={!canNextPage}> { '>>' } </button> <button
className="bg-blue-400 m-2 rounded text-white p-2"
onClick={() => previousPage()}
disabled={!canPreviousPage}
>
Previous
</button>
<button
className="bg-blue-400 m-2 rounded text-white p-2"
onClick={() => nextPage()}
disabled={!canNextPage}
>
Next
</button>
<button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
{" "}
{">>"}{" "}
</button>
</div> </div>
</> </>
);
)
}; };
export default BasicTable; export default BasicTable;

@ -0,0 +1,16 @@
import React from "react";
export const Checkbox = React.forwardRef(({ indeterminate, ...rest }, ref) => {
const defaultRef = React.useRef();
const resolvedRef = ref || defaultRef;
React.useEffect(() => {
resolvedRef.current.indeterminate = indeterminate;
}, [resolvedRef, indeterminate]);
return (
<>
<input type="checkbox" ref={resolvedRef} {...rest} />
</>
);
});
Loading…
Cancel
Save