diff --git a/src/methods.js b/src/methods.js index c6a29bf..c473ec1 100644 --- a/src/methods.js +++ b/src/methods.js @@ -42,7 +42,7 @@ export default Base => let expanderColumn = columns.find( col => col.expander || - (col.columns && col.columns.some(col2 => col2.expander)) + (col.columns && col.columns.some(col2 => col2.expander)), ) // The actual expander might be in the columns field of a group column if (expanderColumn && !expanderColumn.expander) { @@ -91,48 +91,48 @@ export default Base => if (dcol.accessor && !dcol.id) { console.warn(dcol) throw new Error( - 'A column id is required if using a non-string accessor for column above.' + 'A column id is required if using a non-string accessor for column above.', ) } // Fall back to an undefined accessor if (!dcol.accessor) { - dcol.accessor = d => undefined + dcol.accessor = () => undefined } return dcol } + const allDecoratedColumns = [] + // Decorate the columns const decorateAndAddToAll = (column, parentColumn) => { const decoratedColumn = makeDecoratedColumn(column, parentColumn) allDecoratedColumns.push(decoratedColumn) return decoratedColumn } - const allDecoratedColumns = [] - const decoratedColumns = columnsWithExpander.map((column, i) => { + + const decoratedColumns = columnsWithExpander.map(column => { if (column.columns) { return { ...column, columns: column.columns.map(d => decorateAndAddToAll(d, column)), } - } else { - return decorateAndAddToAll(column) } + return decorateAndAddToAll(column) }) // Build the visible columns, headers and flat column list let visibleColumns = decoratedColumns.slice() let allVisibleColumns = [] - visibleColumns = visibleColumns.map((column, i) => { + visibleColumns = visibleColumns.map(column => { if (column.columns) { - const visibleSubColumns = column.columns.filter( - d => - pivotBy.indexOf(d.id) > -1 - ? false - : _.getFirstDefined(d.show, true) - ) + const visibleSubColumns = column.columns.filter(d => ( + pivotBy.indexOf(d.id) > -1 + ? false + : _.getFirstDefined(d.show, true) + )) return { ...column, columns: visibleSubColumns, @@ -141,13 +141,13 @@ export default Base => return column }) - visibleColumns = visibleColumns.filter(column => { - return column.columns + visibleColumns = visibleColumns.filter(column => ( + column.columns ? column.columns.length : pivotBy.indexOf(column.id) > -1 ? false : _.getFirstDefined(column.show, true) - }) + )) // Find any custom pivot location const pivotIndex = visibleColumns.findIndex(col => col.pivot) @@ -163,10 +163,10 @@ export default Base => } }) - let PivotParentColumn = pivotColumns.reduce( + const PivotParentColumn = pivotColumns.reduce( (prev, current) => prev && prev === current.parentColumn && current.parentColumn, - pivotColumns[0].parentColumn + pivotColumns[0].parentColumn, ) let PivotGroupHeader = hasHeaderGroups && PivotParentColumn.Header @@ -202,13 +202,13 @@ export default Base => headerGroups.push({ ...this.props.column, ...column, - columns: columns, + columns, }) currentSpan = [] } // Build flast list of allVisibleColumns and HeaderGroups - visibleColumns.forEach((column, i) => { + visibleColumns.forEach(column => { if (column.columns) { allVisibleColumns = allVisibleColumns.concat(column.columns) if (currentSpan.length > 0) { @@ -238,13 +238,18 @@ export default Base => }) if (row[subRowsKey]) { row[subRowsKey] = row[subRowsKey].map((d, i) => - accessRow(d, i, level + 1) + accessRow(d, i, level + 1), ) } return row } let resolvedData = data.map((d, i) => accessRow(d, i)) + // TODO: Make it possible to fabricate nested rows without pivoting + const aggregatingColumns = allVisibleColumns.filter( + d => !d.expander && d.aggregate, + ) + // If pivoting, recursively group the data const aggregate = rows => { const aggregationValues = {} @@ -254,11 +259,6 @@ export default Base => }) return aggregationValues } - - // TODO: Make it possible to fabricate nested rows without pivoting - const aggregatingColumns = allVisibleColumns.filter( - d => !d.expander && d.aggregate - ) if (pivotBy.length) { const groupRecursively = (rows, keys, i = 0) => { // This is the last level, just return the rows @@ -267,20 +267,18 @@ export default Base => } // Group the rows together for this level let groupedRows = Object.entries( - _.groupBy(rows, keys[i]) - ).map(([key, value]) => { - return { - [pivotIDKey]: keys[i], - [pivotValKey]: key, - [keys[i]]: key, - [subRowsKey]: value, - [nestingLevelKey]: i, - [groupedByPivotKey]: true, - } - }) + _.groupBy(rows, keys[i]), + ).map(([key, value]) => ({ + [pivotIDKey]: keys[i], + [pivotValKey]: key, + [keys[i]]: key, + [subRowsKey]: value, + [nestingLevelKey]: i, + [groupedByPivotKey]: true, + })) // Recurse into the subRows groupedRows = groupedRows.map(rowGroup => { - let subRows = groupRecursively(rowGroup[subRowsKey], keys, i + 1) + const subRows = groupRecursively(rowGroup[subRowsKey], keys, i + 1) return { ...rowGroup, [subRowsKey]: subRows, @@ -329,10 +327,10 @@ export default Base => resolvedData, filtered, defaultFilterMethod, - allVisibleColumns + allVisibleColumns, ), sorted, - sortMethodsByColumnID + sortMethodsByColumnID, ), } } @@ -366,11 +364,10 @@ export default Base => // If 'filterAll' is set to true, pass the entire dataset to the filter method if (column.filterAll) { return filterMethod(nextFilter, filteredSoFar, column) - } else { - return filteredSoFar.filter(row => { - return filterMethod(nextFilter, row, column) - }) } + return filteredSoFar.filter(row => ( + filterMethod(nextFilter, row, column) + )) }, filteredData) // Apply the filter to the subrows if we are pivoting, and then @@ -386,7 +383,7 @@ export default Base => row[this.props.subRowsKey], filtered, defaultFilterMethod, - allVisibleColumns + allVisibleColumns, ), } }) @@ -411,16 +408,16 @@ export default Base => sorted.map(sort => { // Support custom sorting methods for each column if (sortMethodsByColumnID[sort.id]) { - return (a, b) => { - return sortMethodsByColumnID[sort.id](a[sort.id], b[sort.id], sort.desc) - } - } - return (a, b) => { - return this.props.defaultSortMethod(a[sort.id], b[sort.id], sort.desc) + return (a, b) => ( + sortMethodsByColumnID[sort.id](a[sort.id], b[sort.id], sort.desc) + ) } + return (a, b) => ( + this.props.defaultSortMethod(a[sort.id], b[sort.id], sort.desc) + ) }), sorted.map(d => !d.desc), - this.props.indexKey + this.props.indexKey, ) sortedData.forEach(row => { @@ -430,7 +427,7 @@ export default Base => row[this.props.subRowsKey] = this.sortData( row[this.props.subRowsKey], sorted, - sortMethodsByColumnID + sortMethodsByColumnID, ) }) @@ -440,7 +437,7 @@ export default Base => getMinRows () { return _.getFirstDefined( this.props.minRows, - this.getStateOrProp('pageSize') + this.getStateOrProp('pageSize'), ) } @@ -452,9 +449,9 @@ export default Base => if (collapseOnPageChange) { newState.expanded = {} } - this.setStateWithData(newState, () => { + this.setStateWithData(newState, () => ( onPageChange && onPageChange(page) - }) + )) } onPageSizeChange (newPageSize) { @@ -470,16 +467,16 @@ export default Base => pageSize: newPageSize, page: newPage, }, - () => { + () => ( onPageSizeChange && onPageSizeChange(newPageSize, newPage) - } + ), ) } sortColumn (column, additive) { const { sorted, skipNextSort, defaultSortDesc } = this.getResolvedState() - const firstSortDirection = column.hasOwnProperty('defaultSortDesc') + const firstSortDirection = Object.prototype.hasOwnProperty.call(column, 'defaultSortDesc') ? column.defaultSortDesc : defaultSortDesc const secondSortDirection = !firstSortDirection @@ -519,20 +516,18 @@ export default Base => newSorted = [existing] } } + } else if (additive) { + newSorted.push({ + id: column.id, + desc: firstSortDirection, + }) } else { - if (additive) { - newSorted.push({ + newSorted = [ + { id: column.id, desc: firstSortDirection, - }) - } else { - newSorted = [ - { - id: column.id, - desc: firstSortDirection, - }, - ] - } + }, + ] } } else { // Multi-Sort @@ -556,21 +551,19 @@ export default Base => if (!additive) { newSorted = newSorted.slice(existingIndex, column.length) } - } else { - // New Sort Column - if (additive) { - newSorted = newSorted.concat( - column.map(d => ({ - id: d.id, - desc: firstSortDirection, - })) - ) - } else { - newSorted = column.map(d => ({ + // New Sort Column + } else if (additive) { + newSorted = newSorted.concat( + column.map(d => ({ id: d.id, desc: firstSortDirection, - })) - } + })), + ) + } else { + newSorted = column.map(d => ({ + id: d.id, + desc: firstSortDirection, + })) } } @@ -582,9 +575,9 @@ export default Base => : this.state.page, sorted: newSorted, }, - () => { + () => ( onSortedChange && onSortedChange(newSorted, column, additive) - } + ), ) } @@ -593,16 +586,14 @@ export default Base => const { onFilteredChange } = this.props // Remove old filter first if it exists - const newFiltering = (filtered || []).filter(x => { - if (x.id !== column.id) { - return true - } - }) + const newFiltering = (filtered || []).filter(x => ( + x.id !== column.id + )) if (value !== '') { newFiltering.push({ id: column.id, - value: value, + value, }) } @@ -610,9 +601,9 @@ export default Base => { filtered: newFiltering, }, - () => { + () => ( onFilteredChange && onFilteredChange(newFiltering, column, value) - } + ), ) } @@ -634,7 +625,7 @@ export default Base => currentlyResizing: { id: column.id, startX: pageX, - parentWidth: parentWidth, + parentWidth, }, }, () => { @@ -647,7 +638,7 @@ export default Base => document.addEventListener('mouseup', this.resizeColumnEnd) document.addEventListener('mouseleave', this.resizeColumnEnd) } - } + }, ) } @@ -667,10 +658,11 @@ export default Base => pageX = event.pageX } - // Set the min size to 10 to account for margin and border or else the group headers don't line up correctly + // Set the min size to 10 to account for margin and border or else the + // group headers don't line up correctly const newWidth = Math.max( currentlyResizing.parentWidth + pageX - currentlyResizing.startX, - 11 + 11, ) newResized.push({ @@ -682,15 +674,15 @@ export default Base => { resized: newResized, }, - () => { + () => ( onResizedChange && onResizedChange(newResized, event) - } + ), ) } resizeColumnEnd (event) { event.stopPropagation() - let isTouch = event.type === 'touchend' || event.type === 'touchcancel' + const isTouch = event.type === 'touchend' || event.type === 'touchcancel' if (isTouch) { document.removeEventListener('touchmove', this.resizeColumnMoving)