Skip to content

Commit

Permalink
fix: getFacetedMinMaxValues default implementation sometimes returnin…
Browse files Browse the repository at this point in the history
…g array of arrays (#5676)

* fix getFacetedMinMaxValues returning array bug

* rework for your consideration

* ci: apply automated fixes

* don't use array at

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Kevin Van Cott <kevinvandy656@gmail.com>
  • Loading branch information
3 people authored Aug 3, 2024
1 parent be8d0e7 commit 9d54b2e
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions packages/table-core/src/utils/getFacetedMinMaxValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,22 @@ export function getFacetedMinMaxValues<TData extends RowData>(): (
facetedRowModel => {
if (!facetedRowModel) return undefined

const firstValue =
facetedRowModel.flatRows[0]?.getUniqueValues(columnId)
const uniqueValues = facetedRowModel.flatRows
.flatMap(flatRow => flatRow.getUniqueValues(columnId) ?? [])
.map(Number)
.filter(value => !Number.isNaN(value))

if (typeof firstValue === 'undefined') {
return undefined
}

let facetedMinMaxValues: [any, any] = [firstValue, firstValue]

for (let i = 0; i < facetedRowModel.flatRows.length; i++) {
const values =
facetedRowModel.flatRows[i]!.getUniqueValues<number>(columnId)
if (!uniqueValues.length) return

for (let j = 0; j < values.length; j++) {
const value = values[j]!
let facetedMinValue = uniqueValues[0]!
let facetedMaxValue = uniqueValues[uniqueValues.length - 1]!

if (value < facetedMinMaxValues[0]) {
facetedMinMaxValues[0] = value
} else if (value > facetedMinMaxValues[1]) {
facetedMinMaxValues[1] = value
}
}
for (const value of uniqueValues) {
if (value < facetedMinValue) facetedMinValue = value
else if (value > facetedMaxValue) facetedMaxValue = value
}

return facetedMinMaxValues
return [facetedMinValue, facetedMaxValue]
},
getMemoOptions(table.options, 'debugTable', 'getFacetedMinMaxValues')
)
Expand Down

0 comments on commit 9d54b2e

Please sign in to comment.