import React, { useCallback, useMemo, useRef, useState } from 'react'
import _ from "lodash";
import { Table, getTableUtils, saveModules, flagUtils, setUtils } from 'react-select-table';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCheck, faXmark } from '@fortawesome/free-solid-svg-icons'
import Checkbox from './common/Checkbox'
import Input from './common/Input'
import useSessionState from '../hooks/useSessionState'
import useSessionValue from '../hooks/useSessionValue'
import { tableNamespace } from '../store'
import { applyOptions } from '../utils/customOptionsUtils'
import { eventToast } from '../utils/toastUtils'
import todos from "../data/todos.json";
const columns = [
{
title: "A/I",
defaultWidth: 10
//A column without a 'path' property will display the number of the row
},
{
title: "Id",
path: "id",
isHeader: true,
defaultWidth: 10
},
{
title: "Title",
path: "title",
defaultWidth: 50
},
{
title: "Completed",
path: "completed",
defaultWidth: 20,
render: (completed, todo, options) => {
options.className = completed ? "text-green" : "text-red"
return <>
>
}
}
];
const { hooks, options, selectors } = getTableUtils(tableNamespace);
function FullDemo() {
const tableRef = useRef();
const getState = hooks.useGetState()
const actions = hooks.useActions()
const [savedModules, setSavedModules] = useSessionState('savedModules',
saveModules.Items | saveModules.SortOrder)
const [columnVisibility, setColumnVisibility] = useSessionState('columnVisibility', {})
const [restoredColWidths, saveColWidths] = useSessionValue('columnWidths', {})
const visibleColumns = useMemo(() =>
columns.filter(c => columnVisibility[c.title] !== false),
[columnVisibility])
const [pageSize, setPageSize] = useState(10)
const [chunkSize, setChunkSize] = useState(options.chunkSize)
const handleTableKeyDown = useCallback((e, selection) => {
switch(e.keyCode) {
case 46: //Delete
//Event argument for onKeyDown, onSelectionChange, onItemsOpen and onContextMenu
//is a Set when the multiSelect option in enabled, or just the key of the row when it is disabled
if (options.multiSelect)
actions.deleteItems(...selection)
else
actions.deleteItems(selection)
break;
default:
break;
}
}, [actions]);
return <>
eventToast("Context menu", { target })}
onSelectionChange={selection =>
eventToast("Selection changed", { selection })}
onItemsOpen={(selection, fromKeyboard) =>
eventToast("Items opened", { selection, fromKeyboard })}
onColumnResizeEnd={widths => {
eventToast("Columns resized and widths saved")
saveColWidths(widths)
}}
onActionDispatched={internal => {
if (internal) return
tableRef.current.focus()
window.scrollTo(0, 0)
}}
/>
Actions
Items
Filtering
Pagination
Save state
{_.map(saveModules, (flag, name) =>
setSavedModules(
flagUtils.toggleFlag(savedModules, flag, checked, saveModules))}
/>)}
Columns
{_.map(columns, ({title}) =>
setColumnVisibility(
visibility => ({ ...visibility, [title]: checked }))}
/>
)}
Options
Note: Changing these will cause the page to reload
General
>
}
export default FullDemo;