Skip to content

Commit

Permalink
Custom Attributes for Grid and Row (#491) (Fixes #490)
Browse files Browse the repository at this point in the history
Added function props `gridAttributesGetter` and `rowAttributesGetter`.

These functions can be used to return the grid/row HTML attributes. These will be directly passed to the DIV elements by FDT.

`rowAttributesGetter` is invoked for each visible row (including buffer) with the `rowIndex` as the first param.
  • Loading branch information
louiebert authored and pradeepnschrodinger committed Oct 29, 2019
1 parent dd871d8 commit 48a3118
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/FixedDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,12 @@ class FixedDataTable extends React.Component {
footerHeight: PropTypes.number,
groupHeaderHeight: PropTypes.number,
headerHeight: PropTypes.number,
})
}),

/**
* Callback that returns an object of html attributes to add to the grid element
*/
gridAttributesGetter: PropTypes.func,
}

static defaultProps = /*object*/ {
Expand Down Expand Up @@ -660,6 +665,7 @@ class FixedDataTable extends React.Component {
elementHeights,
isColumnReordering,
isColumnResizing,
gridAttributesGetter,
maxScrollX,
maxScrollY,
onColumnReorderEndCallback,
Expand All @@ -676,6 +682,7 @@ class FixedDataTable extends React.Component {
const { cellGroupWrapperHeight, footerHeight, groupHeaderHeight, headerHeight } = elementHeights;
const { scrollEnabledX, scrollEnabledY } = scrollbarsVisible(this.props);
const onColumnReorder = onColumnReorderEndCallback ? this._onColumnReorder : null;
const attributes = gridAttributesGetter && gridAttributesGetter();

let groupHeader;
if (groupHeaderHeight > 0) {
Expand Down Expand Up @@ -849,6 +856,7 @@ class FixedDataTable extends React.Component {
)}
role="grid"
aria-rowcount={ariaRowCount}
{...attributes}
tabIndex={tabIndex}
onKeyDown={this._onKeyDown}
onTouchStart={touchScrollEnabled ? this._touchHandler.onTouchStart : null}
Expand Down
2 changes: 2 additions & 0 deletions src/FixedDataTableBufferedRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class FixedDataTableBufferedRows extends React.Component {
rowOffsets: PropTypes.object.isRequired,
rowKeyGetter: PropTypes.func,
rowSettings: PropTypes.shape({
rowAttributesGetter: PropTypes.func,
rowHeightGetter: PropTypes.func,
rowsCount: PropTypes.number.isRequired,
subRowHeightGetter: PropTypes.func,
Expand Down Expand Up @@ -126,6 +127,7 @@ class FixedDataTableBufferedRows extends React.Component {
rowProps.subRowHeight = this.props.rowSettings.subRowHeightGetter(rowIndex);
rowProps.offsetTop = Math.round(baseOffsetTop + props.rowOffsets[rowIndex]);
rowProps.key = props.rowKeyGetter ? props.rowKeyGetter(rowIndex) : key;
rowProps.attributes = props.rowSettings.rowAttributesGetter && props.rowSettings.rowAttributesGetter(rowIndex);

const hasBottomBorder = (rowIndex === props.rowSettings.rowsCount - 1) && props.showLastRowBorder;
rowProps.className = joinClasses(
Expand Down
6 changes: 6 additions & 0 deletions src/FixedDataTableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class FixedDataTableRowImpl extends React.Component {
* The value of the aria-rowindex attribute.
*/
ariaRowIndex: PropTypes.number,

/**
* DOM attributes to be applied to the row.
*/
attributes: PropTypes.object,
};

render() /*object*/ {
Expand Down Expand Up @@ -281,6 +286,7 @@ class FixedDataTableRowImpl extends React.Component {
className={joinClasses(className, this.props.className)}
role={'row'}
aria-rowindex={this.props.ariaRowIndex}
{...this.props.attributes}
onClick={this.props.onClick ? this._onClick : null}
onContextMenu={this.props.onContextMenu ? this._onContextMenu : null}
onDoubleClick={this.props.onDoubleClick ? this._onDoubleClick : null}
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function getInitialState() {
},
rowSettings: {
bufferRowCount: undefined,
rowAttributesGetter: undefined,
rowHeight: 0,
rowHeightGetter: () => 0,
rowsCount: 0,
Expand Down Expand Up @@ -248,6 +249,7 @@ function setStateFromProps(state, props) {
props.rowHeightGetter || (() => rowHeight);
newState.rowSettings.subRowHeightGetter =
props.subRowHeightGetter || (() => subRowHeight || 0);
newState.rowSettings.rowAttributesGetter = props.rowAttributesGetter;

newState.scrollFlags = Object.assign({}, newState.scrollFlags,
pick(props, ['overflowX', 'overflowY', 'showScrollbarX', 'showScrollbarY']));
Expand Down

0 comments on commit 48a3118

Please sign in to comment.