Skip to content
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

Allow useOrderedRows to put all null elements at the bottom #1587

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Jun 19, 2024

This PR changes useOrderedRows so that it can make null/undefined values be put always at the bottom of the list, and makes this the default behavior.

This is done by adding a new nullsLast option to the order object, which is true by default.

Callers will need to explicitly switch from nullsLast: true to nullsLast: false if they want this to change as the order direction changes:

const direction = 'descending';
const order = {
  field: 'display_name',
  direction,
  nullFirst: direction === 'descending',
}

Previously we were comparing nulls normally with other non-null values, which produced unintuitive results, because in JS both 'some string' > null and null > 'some string' are false.

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (ea267fe) to head (0d300bc).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1587   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           62        62           
  Lines         1041      1046    +5     
  Branches       397       402    +5     
=========================================
+ Hits          1041      1046    +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acelaya acelaya requested a review from robertknight June 19, 2024 14:01
@acelaya acelaya marked this pull request as draft June 19, 2024 14:20
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implements what we discussed, but on reflection I wonder if perhaps it would be less surprising if we kept the hook stateless and instead gave the caller an option to determine whether null values sort before or after non-null values.

src/hooks/use-ordered-rows.ts Outdated Show resolved Hide resolved
@acelaya acelaya marked this pull request as ready for review June 19, 2024 14:33
@acelaya acelaya changed the title Make useOrderedRows initially put all null elements at the bottom Allow useOrderedRows to put all null elements at the bottom Jun 19, 2024
src/types.ts Outdated

/**
* Indicates entries where the value for `field` is null/undefined should go
* last. Otherwise, they will get compared with non-null values normally.
Copy link
Member

@robertknight robertknight Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. My intention was that nulls would always sort either before or after non-null values. So we would never use </> comparisons between null and non-null values. I think it makes sense to default to true, assuming strings are the most common data type in rows and we'd probably want to sort them last.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/types.ts Outdated Show resolved Hide resolved
@acelaya acelaya merged commit 73a0529 into main Jun 19, 2024
4 checks passed
@acelaya acelaya deleted the order-nulls branch June 19, 2024 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants