0.3.13 • Published 2 years ago

sui-react-table v0.3.13

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Semantic Table

DataTable made with styled components from React Semantic UI.

Installation:

npm i sui-react-table

Dependencies: This component depends on the following packages

Basic Example


const data = [
    {
        id: 9888,
        name: "Hartmann, Runolfsson and Stroman Inc",
        email: "Moshe67@gmail.com"
    },
    {
        id: 28451,
        name: "Jacobson Inc Inc",
        email: "Green19@hotmail.com"
    },
    {
        id: 72265,
        name: "Bahringer, Padberg and Upton Inc",
        email: "Casandra_Heaney97@gmail.com"
    },
]

<PaginatedTable
    title="Example"
    onCancel={true}
    rows={data}
    rowLimit={15}
    actionsActive={false}
    columns={[
        {
            width: 250,
            Header: 'Code',
            accessor: 'id',
        },
        {
            width: 225,
            Header: 'Name',
            accessor: 'name',
        },
        {
            width: 225,
            Header: 'Email',
            accessor: 'email',
        },
    ]}
/>

Props


PropTypeDescription
titlestringThe title used for exporting table data to CSV
rowsarrayArray of objects to be displayed in the table
columnsarrayArray of colums to be displayed, each column will access to the corresponding attribute from each row data (see structure here) it can control whether the column can be filtered, ordered and edited
rowLimitnumericThe number of rows to be displayed per page
loadingbooleanControls whether the table will show a "loading" message to indicate that data is being loaded into the table
actionsActivebooleanWhether each row will have a set of actions (see structure here)
actionsWidthnumericWidth in pixels of the first column that will display the actions buttons
onSavefunctionFunction to execute to save changes on rows
enableExternalSavebooleanControls whether table should save via external manipulation
onAddfunctionFunction to execute if the user wants to create a new item with an external component (form or modal form)
onAddRowboolean / functionIf set to true, it will create a new row on click, if a function is given, it will expect an object with custom data to match the given rows structure
onSelectfunctionFunction to execute if the user clicks on the "pencil" icon button on a specific row it as access to the selected row
onDeleteboolean / functionIf set to true, it will display a button to remove the selected row ("trash" icon button), if a function is given, it will allow a custom function to be executed after the deletion has been made, the function has access to the remaining rows, if the function returns a false boolean, then it will not trigger the inner deletion
onCancelboolean / functionIf set to true, it will cancel all changes made on the table, if a function is given, it will allow a custom function to be executed after the cancelation has been made
onEditCellfunctionFunction to execute after a cell has been edited, it will execute on blur, if the function returns a false boolean, then it will not trigger the inner edition
onInnerUpdatefunctionFunction to execute after any table rows change
enableExportToCSVbooleanIf set to true, it will display a button that will export all displayed data in the table (ignoring pagination, it will only export data that hasn't been filtered) in a Comma Separated Values (CSV) format, it uses the "title" prop and a timestamp to name the exported file
heightnumericThe table height, max varies depending on screen size
additionalActionButtonsarrayAdditional buttons to be displayed on each row, each button will have a custom action and icon to give it the desired look and functionality (see structure here)
additionalHeaderButtonsarrayAdditional buttons to be displayed on the header section, each button will have a custom action and icon to give it the desired look and functionality (see structure here)
saveButtonTextstringCustom text for save button
cancelButtonTextstringCustom text for cancel button
newRowButtonTextstringCustom text for newRow button
addButtonTextstringCustom text for add button
removeFiltersButtonTextstringCustom text for removeFilters button
hideRemoveFiltersButtonbooleanControl whether the removeFiltersButton will appear or not
editRowButtonTextstringCustom text for editRow ave button
deleteRowButtonTextstringCustom text for deleteRow button
exportToCSVButtonTextstringCustom text for exportToCSV button
actionsHeaderTextstringCustom text for actions header
numberOfRecordsTextstringCustom text for total records label
showRecordsbooleanControl whether the table will show total of records
numberOfRecordsPerPageTextstringCustom text for total records per page label
showRecordsPerPagebooleanControls whether the table will show total of records per page
paginatedbooleanControls whether the table is paginated or not, defaults to true

Loading


const data = [
    {
        id: 9888,
        name: "Hartmann, Runolfsson and Stroman Inc",
        email: "Moshe67@gmail.com"
    },
    {
        id: 28451,
        name: "Jacobson Inc Inc",
        email: "Green19@hotmail.com"
    },
    {
        id: 72265,
        name: "Bahringer, Padberg and Upton Inc",
        email: "Casandra_Heaney97@gmail.com"
    },
]

const [isLoading, setIsLoading] = useState(false)

useEffect(() => {
    setIsLoading(true)
    setTimeout(() => {
        setIsLoading(false)
    }, 1500)
}, [])


<PaginatedTable
    loading={isLoading}
    title="Example"
    onCancel={true}
    rows={data}
    rowLimit={15}
    actionsActive={false}
    columns={[
        {
            width: 250,
            Header: 'Code',
            accessor: 'id',
        },
        {
            width: 225,
            Header: 'Name',
            accessor: 'name',
        },
        {
            width: 225,
            Header: 'Email',
            accessor: 'email',
        },
    ]}
/>

Header Actions


PropTypeDescription
onSavefunctionFunction to execute to save changes on rows
onAddfunctionFunction to execute if the user wants to create a new item with an external component (form or modal form)
onAddRowboolean / functionIf set to true, it will create a new row on click, if a function is given, it will expect an object with custom data to match the given rows structure
onCancelboolean / functionIf set to true, it will cancel all changes made on the table, if a function is given, it will allow a custom function to be executed after the cancelation has been made
onEditCellfunctionFunction to execute after a cell has been edited, it will execute on blur
enableExportToCSVbooleanIf set to true, it will display a button that will export all displayed data in the table (ignoring pagination, it will only export data that hasn't been filtered) in a Comma Separated Values (CSV) format, it uses the "title" prop and a timestamp to name the exported file

Example

const data = [
    {
        id: 9888,
        name: "Hartmann, Runolfsson and Stroman Inc",
        email: "Moshe67@gmail.com"
    },
    {
        id: 28451,
        name: "Jacobson Inc Inc",
        email: "Green19@hotmail.com"
    },
    {
        id: 72265,
        name: "Bahringer, Padberg and Upton Inc",
        email: "Casandra_Heaney97@gmail.com"
    },
]


<PaginatedTable
    title="Example"
    onCancel={true}
    rows={data}
    rowLimit={15}
    actionsActive={false}
    onSave={({ deleteItems, updateItems, createItems, rows }) => {
        console.log(deleteItems)
        console.log(updateItems)
        console.log(createItems)
        console.log(rows)
    }}
    onAdd={() => {
        console.log('This can be used to display an external form or modal form! Control the rendered rows externally')
    }}
    onAddRow={(id) => {
        return {
            id: id,
            name: "Daugherty Inc LLC"
            email: "Connor.Bergstrom56@yahoo.com"
        }
    }}
    onCancel={true}
    onEditCell={(editedRow) => {
        console.log('This is the new data: ')
        console.log(editedRow)
    }}
    enableExportToCSV={true}
    columns={[
        {
            width: 250,
            Header: 'Code',
            accessor: 'id',
        },
        {
            width: 225,
            Header: 'Name',
            accessor: 'name',
        },
        {
            width: 225,
            Header: 'Email',
            accessor: 'email',
        },
    ]}
/>

Row Actions


PropTypeDescription
actionsActivebooleanWhether each row will have a set of actions (see structure here)
actionsWidthnumericWidth in pixels of the first column that will display the actions buttons
onSelectfunctionFunction to execute if the user clicks on the "pencil" icon button on a specific row it as access to the selected row
onDeleteboolean / functionIf set to true, it will display a button to remove the selected row ("trash" icon button), if a function is given, it will allow a custom function to be executed after the deletion has been made, the function has access to the remaining rows
additionalActionButtonsarrayAdditional buttons to be displayed on each row, each button will have a custom action and icon to give it the desired look and functionality, icons here

Example

const data = [
    {
        id: 9888,
        name: "Hartmann, Runolfsson and Stroman Inc",
        email: "Moshe67@gmail.com"
    },
    {
        id: 28451,
        name: "Jacobson Inc Inc",
        email: "Green19@hotmail.com"
    },
    {
        id: 72265,
        name: "Bahringer, Padberg and Upton Inc",
        email: "Casandra_Heaney97@gmail.com"
    },
]

const additionalActionButtons = [
    {
        name: 'View',
        icon: 'eye',
        action: (row) => {
            console.log(row)
        }
    },
     {
        name: 'Email',
        icon: 'mail',
        action: (row) => {
            console.log('Mail sent to ' + row.email)
        }
    }
]

<PaginatedTable
    title="Example"
    onCancel={true}
    rows={data}
    rowLimit={15}
    actionsActive={true}
    actionsWidth={125}
    onSelect={(selectedRow) => {
        console.log(selectedRow)
    }}
    onDelete={(remainingRows) => {
        console.log(remainingRows)
    }}
    additionalActionButtons={additionalActionButtons}
    columns={[
        {
            width: 250,
            Header: 'Code',
            accessor: 'id',
        },
        {
            width: 225,
            Header: 'Name',
            accessor: 'name',
        },
        {
            width: 225,
            Header: 'Email',
            accessor: 'email',
        },
    ]}
/>

Columns

Each column will have the following structure:

PropTypeDescription
widthnumericThe column's width, data exceeding this width will trigger an horizontal scrollbar
HeaderstringThe column's title to be displayed on the table header
accessorstringThe name of the row attribute that holds the value to be displayed
typestringFormat and input type that the column will have, can be one of: text, currency, boolean, select, textarea, number, color, date
optionsarrayIf the type is select, then an options array has to be provided (see the structure below)
editablebooleanWhether the column can be edited or not
filterablebooleanWhether the column can be filtered or not
permanentRenderbooleanWhether the column cell input will always be visible and not render the default label

Column Options

PropTypeDescription
keystringThe unique key identifier
textstringThe option text to display
valueanyThe actual value of each option
colorstring / functionThe column color, if a string is given, it will have that static color for every cell in the column, if a function is given, it will have access to the current row and condittionaly format an specific cell

Additional Header Actions


Each additional header button will have the following structure:

PropTypeDescription
labelstringThe text content of the button
iconstringThe button icon to be displayed
actionfunctionAction to be executed on click of button
disabledbooleanWhether the button is disabled or not

Example

const data = [
    {
        id: 9888,
        name: "Hartmann, Runolfsson and Stroman Inc",
        email: "Moshe67@gmail.com"
    },
    {
        id: 28451,
        name: "Jacobson Inc Inc",
        email: "Green19@hotmail.com"
    },
    {
        id: 72265,
        name: "Bahringer, Padberg and Upton Inc",
        email: "Casandra_Heaney97@gmail.com"
    },
]

<PaginatedTable
    title="Example"
    onCancel={true}
    rows={data}
    rowLimit={15}
    actionsActive={true}
    actionsWidth={125}
    onSelect={(selectedRow) => {
        console.log(selectedRow)
    }}
    onDelete={(remainingRows) => {
        console.log(remainingRows)
    }}
    additionalHeaderButtons={
            [
                {
                    label: 'Prueba',
                    icon: 'search',
                    action: (rows) => {
                        console.log(rows)
                    },
                },
            ]
        }
    columns={[
        {
            width: 250,
            Header: 'Code',
            accessor: 'id',
        },
        {
            width: 225,
            Header: 'Name',
            accessor: 'name',
        },
        {
            width: 225,
            Header: 'Email',
            accessor: 'email',
        },
    ]}
/>