reactabular-easy - Easy, opinionated wrapper for Reactabular
Given Reactabular is flexible by design, it's not the easiest to use and you may have to do quite a bit of wiring to make it work the way you want. reactabular-easy has been designed to make using it easier. It is opinionated and takes away some power. But on the plus side it allows you to render a fully featured table faster.
How to Use?
To make the drag and drop functionality work, you have to set up react-dnd-html5-backend or some other React DnD backend.
You can find suggested default styling for the package at
style.css
in the package root.
If you want to use the drag and drop functionality, you have to set up React DnD!
/*import React from 'react';import * as resolve from 'table-resolver';import * as dnd from 'reactabular-dnd';import * as easy from 'reactabular-easy';import VisibilityToggles from 'react-visibility-toggles';import * as resizable from 'reactabular-resizable';import * as tree from 'treetabular';import * as search from 'searchtabular';import HTML5Backend from 'react-dnd-html5-backend';import { DragDropContext } from 'react-dnd';import { compose } from 'redux';import uuid from 'uuid';import cloneDeep from 'lodash/cloneDeep';import findIndex from 'lodash/findIndex'; import { generateParents, generateRows} from './helpers';*/ const schema = type: 'object' properties: Id: type: 'string' fullName: $ref: '#/definitions/fullName' company: type: 'string' age: type: 'integer' boss: $ref: '#/definitions/boss' required: 'Id' 'fullName' 'company' 'age' 'boss' definitions: boss: type: 'object' properties: name: type: 'string' required: 'name' fullName: type: 'object' properties: first: type: 'string' last: type: 'string' required: 'first' 'last' ;const rows = ; Component { ; thisstate = rows columns: this sortingColumns: query: hiddenColumns: // <id>: <hidden> - show all by default ; thistable = null; thisonMoveRow = thisonMoveRow; thisonDragColumn = thisonDragColumn; thisonMoveColumns = thisonMoveColumns; thisonToggleColumn = thisonToggleColumn; thisonSelectRow = thisonSelectRow; thisonRemove = thisonRemove; thisonSort = thisonSort; thisonToggleShowingChildren = thisonToggleShowingChildren; } { thisresizableHelper = resizable; } { thisresizableHelper; } { return id: 'demo' // Unique ids for handling visibility checks header: label: 'Demo' draggable: true resizable: true formatters: <span>Testing</span> cell: formatters: <span>Demo</span> toggleChildren: true width: 100 id: 'name' header: label: 'Name' // Disabled for now as this is a difficult case in React DnD //draggable: true children: id: 'firstName' property: 'fullName.first' header: label: 'First Name' draggable: true sortable: true resizable: true cell: highlight: true width: 125 id: 'lastName' property: 'fullName.last' header: label: 'Last Name' draggable: true sortable: true resizable: true cell: highlight: true width: 125 id: 'age' property: 'age' header: label: 'Age' draggable: true sortable: true resizable: true cell: highlight: true width: 150 id: 'company' property: 'company' header: label: 'Company' draggable: true sortable: true resizable: true cell: highlight: true width: 250 id: 'bossName' property: 'boss.name' header: label: 'Boss' draggable: true sortable: true resizable: true cell: highlight: true width: 200 cell: formatters: value rowData <div> <input ="button" ="Click me" = /> <span ="remove" = = > ✗ </span> </div> width: 200 ; } { const searchColumn columns sortingColumns rows query hiddenColumns } = thisstate; const idField = 'Id'; const parentField = 'parent'; const visibleColumns = columns; const columnChildren = visibleColumns; const headerRows = resolve; return <div> <VisibilityToggles = = = /> <div ="scroll-container"> <label>Scroll to index: </label> <div> <input ="text" = /> </div> </div> <div ="search-container"> <span>Search</span> <search.Field = = = = = = /> </div> <easy.Table = = = ="Id" = = = = = = = = = = = /> </div> ; { const rows = treethisstaterows; rows && this; } { thisresizableHelper; } { const columns = treethisstatecolumns; const sourceIndex = ; const targetIndex = ; if sourceIndex < 0 || targetIndex < 0 return null; let source = columnssourceIndex; let target = columnstargetIndex; if source_isParent return console; // If source doesn't have a parent, make sure we are dragging to // target parent by modifying the original structure. if !sourceparent const targetParents = treecolumns; // If trying to drag to a child, drag to its root // parent instead. if targetParentslength return console; const nestedSourceIndex = ; source = thisstatecolumnsnestedSourceIndex; const nestedTargetIndex = ; target = thisstatecolumnsnestedTargetIndex; if nestedSourceIndex < 0 || nestedTargetIndex < 0 return null; // We are operating at root level now so move accordingly. const movedColumns = dnd; // Retain widths while moving movedColumnsnestedSourceIndexwidth = sourcewidth; movedColumnsnestedTargetIndexwidth = targetwidth; this; else if sourceparent === targetparent // Dragging within children now. This has to be against flattened data. const movedColumns = dnd; // Retain widths while moving. // XXX: This works only for single level nesting. const sourceWidth = sourcewidth; const targetWidth = targetwidth; sourcewidth = targetWidth; targetwidth = sourceWidth; this; // If trying to drag from children to other children or so, do nothing. } { console; } { console; this; } { const hiddenColumns = thisstate; this; } { const rows = ; const idx = ; // this could go through flux etc. rows; this; } { const rows = ; rowsrowIndexshowingChildren = !rowsrowIndexshowingChildren; this; //}, }} // Set up drag and drop context// const DragAndDropDemo = DragDropContext(HTML5Backend)(EasyDemo); <EasyDemo /> /*export default EasyDemo*/
Styling
It is possible to pass custom classNames
and props
as listed below:
classNames: table: null header: wrapper: null // TODO /* row: null, cell: null */ body: wrapper: null // TODO /* row: null, cell: null */ props: resize: container: style: color: 'red' value: {} handle: {} sort: container: {} value: {} order: {}
For more control, you can also override components
and also inject styling and class names through the column definition and the onRow
handler.
Customization
If you want to customize resize/sort controls further (add className
s etc.), pass props
like this:
props: resize: container: {} value: {} handle: {} sort: container: {} value: {} order: {}
License
MIT.