-
Notifications
You must be signed in to change notification settings - Fork 14.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(time-series table): display null values in time-series table and sortable #19024
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { Row } from 'react-table'; | ||
|
||
const sortNumberWithMixedTypes = (rowA: Row, rowB: Row, columnId: string) => { | ||
const rowAVal = rowA.values[columnId].props['data-value']; | ||
const rowBVal = rowB.values[columnId].props['data-value']; | ||
if (typeof rowAVal === 'number' && typeof rowBVal === 'number') { | ||
return rowAVal - rowBVal; | ||
} | ||
if (typeof rowAVal === 'number') { | ||
return 1; | ||
} | ||
if (typeof rowBVal === 'number') { | ||
return -1; | ||
} | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wanna add the test case where both are not a number? also, can these ever be strings or objects or other types? or only ever number or null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return (
(rowAVal == null) - (rowBVal == null) ||
Number.isNaN(Number(rowAVal)) - Number.isNaN(Number(rowBVal)) ||
Number(rowAVal) - Number(rowBVal)
); This should handle all cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the time-series table viz type, i think the cell values could be string, number, null, number mixed with null.
any suggestion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I double checked, the first column There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can wrap them with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry...typescript doesn't like
|
||
}; | ||
|
||
export default sortNumberWithMixedTypes; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import sortFn from './SortNumberWithMixedTypes'; | ||
|
||
describe('sort Number and mixed types', () => { | ||
const columnId = 'mLnVxkc1g'; | ||
const rowA = { | ||
values: { | ||
metric: 'Albania', | ||
mLnVxkc1g: { | ||
props: { | ||
'data-value': null, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const rowB = { | ||
values: { | ||
metric: 'Afghanistan', | ||
mLnVxkc1g: { | ||
props: { | ||
'data-value': -0.6749999999999972, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const rowC = { | ||
values: { | ||
metric: 'Malawi', | ||
mLnVxkc1g: { | ||
props: { | ||
'data-value': 4.852999999999994, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('should treat null values as smallest', () => { | ||
// @ts-ignore | ||
expect(sortFn(rowA, rowB, columnId)).toBe(-1); | ||
// @ts-ignore | ||
expect(sortFn(rowA, rowC, columnId)).toBe(-1); | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to ts ignore here? if the rows are the improper types, then you can define them as the correct ones above instead of ignoring types There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Row type is very complicated object, i am not sure how to mock my input data:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow this trick works!! |
||
expect(sortFn(rowB, rowC, columnId)).toBe( | ||
rowB.values[columnId].props['data-value'] - | ||
rowC.values[columnId].props['data-value'], | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move
Row
andcolumnId
out of this utility function to make it more generic? I'd imagine it can be useful in other places as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sortType
is prop fromreact-table useSortBy
: https://github.com/TanStack/react-table/blob/alpha/docs/api/useSortBy.mdI am not sure I can change my function signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can always have wrapper function for
sortType
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this sortType must be memoized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it needs to be memorized, which is why I added useCallback.
You can either move the useCallback up a level or move the wrapper outside of the component as a simple one liner in the component file without using useCallback.
Point is, such utility functions should not depend on too specific input data types if we want to maximize reusability.