Skip to content

Repository files navigation

@impedans/table

This package hasn't been published yet and is in progress.

Preview of table component

Usage

importTable,{useSortTable}from"@impedans/table";// not available yetexportdefaultfunctionApp(){const[loading,setLoading]=useState(true);const[list,setList]=useState(null);const[sortedList,Th]=useSortTable(list);useEffect(()=>{letmounted=true;fetch("https://jsonplaceholder.typicode.com/todos").then((res)=>res.json()).then((data)=>{if(mounted){setLoading(false);setItems(data);}}).catch((e)=>console.error(e));return()=>{mounted=false;}},[]);return(<TablecolSizes={[1,10,1]}loading={loading}><Table.Header><Table.Row><ThobjectKey="id">ID</Th><ThobjectKey="title">Title</Th><ThobjectKey="completed"sortFunc={(k)=>(a,b)=>(a[k] ? -1 : 1)}>
Done
</Th></Table.Row></Table.Header><Table.Body>{sortedItems.map((item)=>(<Table.Row><Table.Cell>{item.id}</Table.Cell><Table.Cellellipsistitle={item.title}style={{minWidth: "300px"}}><strong>{item.id}</strong></Table.Cell><Table.Cell><i>{item.completed ? "✔" : "✗"}</i></Table.Cell></Table.Row>)}</Table.Body></Table>)}

Exports

  • Table (default): Table wrapper component
    • Table.Header: Table header (<thead>)
    • Table.Body: Table body (<tbody>)
    • Table.Row: Table row (<tr>)
    • Table.SkeletonRow: Table skeleton row (used for loading state)
    • Table.Cell: Table cell (<td>)
    • Table.HeadCell: Table header cell (<th>)
  • useSortTable: Hook for sorting by clicking the headcell.

API

Table

Wrapper component. It's very important that you keep the structure and hierarchy of the components. This component deals with setting up the grid, removing extra columns, etc.

Props

KeyDescriptionType
loadingLoading state of the tableboolean
loadingRowsAmount of rows to display as skeletons (default: 3)number
colSizesArray of column sizes (as fractions) (default 1 for each heading) (left to right)Array<number>

Table.Header

Container for header row. The amount of HeadCells present will determine the amount of columns in the table. You can put empty <th>s in there if you need to.

Table.Row

Container for tablecells. Won't render more cells than present in the tableheader.

Props

KeyDescriptionType
clickableGives the cell a hover effect, as well as keydown events.boolean
onClickFunction that runs on click or on keydown (space / enter)() => void

Table.SkeletonRow

Renders a skeleton row

Props

KeyDescriptionType
colsThe amount of columns in the rownumber

Table.Cell

A tablecell (<td>) with some helper props

Props

KeyDescriptionType
ellipsisDetermines if the cell should allow ellipsis (requires min-width style)boolean
fullWidthDetermines if the cell should take up the entire rowboolean
focusableDetermines if the cell should have a tabIndexboolean
onClickRuns on click and on keydown (space / enter) if focusable(event) => void
onKeyDownRuns on keydown(event) => void

Table.HeadCell

A table headcell (<th>).

useSortTable

A hook that allows you to automatically sort on header click. // TODO: add more docs

About

A nice & reusable table library.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages