Skip to content

Conversation

@skeary
Copy link

@skeary skeary commented Mar 13, 2022

Updates the admin UI for the relation type fields so that the values in the select are ordered (ascending) by the label field. This makes it easier to find an option when dealing with larger select lists.

The behavior could be made configurable within schema.ts so that ordering could be done on another field or descending rather than ascending order. I wasn't sure of the value of this so I've not made it configurable but explore this if felt useful.

@changeset-bot

This comment was marked as resolved.

@vercel
Copy link

vercel bot commented Mar 13, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/keystonejs/keystone-next-docs/3drRmbNj8LW8puvxu1kmo18xG7um
✅ Preview: https://keystone-next-docs-git-fork-skeary-orderrelat-561fd4-keystonejs.vercel.app

@vercel vercel bot temporarily deployed to Preview March 13, 2022 10:11 Inactive
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 13, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@dcousens
Copy link
Member

Hi @skeary ,

This is nice to have functionality!
Unfortunately, we don't necessarily want to assume what might be a natural order for this field generally.
We might want to expand this functionality to include configuration in the schema to what the default ordering is, and use that instead.

If you want to try and do that, awesome! Otherwise we'll try and expand on this pull request when we have time 👍

@skeary
Copy link
Author

skeary commented Mar 15, 2022

Hi @dcousens,
Thanks for the feedback. My thinking was that this was a better default - while it may not solve all use cases it wouldn't be worse for anyone and in general would be what most people wanted? It's not a problem though to add in extra configuration, and it's definitely more flexible, so am happy to have a go at this!

I think the following might be a reasonable configuration approach but just wanted to checked what you thought before I go ahead:

    ui?: {
        orderBy: false | Array<{ labelField:  'asc' | 'desc' } | { field: string, order: 'asc' | 'desc' }> | undefined,
    } | undefined;

With examples of use in schema.ts:

      author: relationship({ ref: 'Author.posts', many: false }), // Sort by label field, ascending

      author: relationship({ ref: 'Author.posts', many: false,
        ui: {
          orderBy:  false // No ordering
        }
      }),


      author: relationship({ ref: 'Author.posts', many: false,
        ui: {
          orderBy:  [ { labelField: 'asc' }] // Sort by label field, ascending
        }
      }),

      author: relationship({ ref: 'Author.posts', many: false,
        ui: {
          orderBy:  [ { field: 'email', order: 'desc' }, { labelField: 'asc' }] // Sort by email field, descending and then label field (ascending)
        }
      }),

I've tried to make the configuration align with the GraphQL orderBy query clauses. In terms the different configuration of specifying the sort order on the label field vs named fields, I wanted to choose an approach where the TypeScript IDE help guides you on how to sort on label field.

In the future I think this could be expanded to allow custom/different sorts, since you could just union more choices onto 'asc' | 'desc'.

What do you think?

@vercel vercel bot temporarily deployed to Preview March 16, 2022 15:30 Inactive
@vercel vercel bot temporarily deployed to Preview March 17, 2022 00:32 Inactive
@vercel vercel bot temporarily deployed to Preview March 18, 2022 00:26 Inactive
@vercel vercel bot temporarily deployed to Preview March 18, 2022 00:38 Inactive
@vercel vercel bot temporarily deployed to Preview March 18, 2022 01:00 Inactive
@vercel vercel bot temporarily deployed to Preview March 18, 2022 10:18 Inactive
@skeary skeary closed this Mar 18, 2022
@skeary skeary reopened this Mar 18, 2022
@vercel vercel bot temporarily deployed to Preview March 18, 2022 10:22 Inactive
@skeary
Copy link
Author

skeary commented Mar 18, 2022

Note this changeset now (I think) includes a working fix for #6112. This issue was that the ui.labelField option under the relationship field config wasn't having any affect. I noticed this problem while testing my changes and have now hopefully fixed the issue. Unless I'm missing something, the support for this feature wasn't really there rather than a logic bug or similar. So I've now implemented support for this.

@skeary skeary force-pushed the order_relationship_select branch from 1f1bb48 to d7e5cbb Compare March 18, 2022 10:27
@vercel vercel bot temporarily deployed to Preview March 18, 2022 10:27 Inactive
@vercel vercel bot temporarily deployed to Preview March 20, 2022 00:53 Inactive
@skeary
Copy link
Author

skeary commented Mar 20, 2022

Hi @dcousens,
I've implemented the approached I outlined in a earlier comment where the configuration now supports:

 ui?: {
        orderBy: false | Array<{ labelField:  'asc' | 'desc' } | { field: string, order: 'asc' | 'desc' }> | undefined,
    } | undefined;

To me, this seems a reasonably simple way to configure the ordering and should hopefully cover the significant number of use cases. If someone doesn't want ordering they can turn off the ordering which will revert to the current behaviour. By default the ordering is still based on label field (ascending) and I think that is what is useful for most people?

I've updated the documentation to describe the new configuration options. See https://keystone-next-docs-ahungkcd8-keystonejs.vercel.app/docs/apis/fields#relationship for a rendered version.

As part of doing this changeset, I also think I've addressed #6112.

It would be great if you could review my overall approach and implementation and let me know what you think. I'm happy to tweak as needed.

Cheers,
Simon

@dcousens dcousens changed the title Order relationship select Ordered relationship select Apr 1, 2022
@dcousens dcousens self-requested a review August 4, 2022 06:34
@dcousens dcousens self-assigned this Aug 4, 2022
@dcousens dcousens removed their request for review August 4, 2022 06:34
@willemmulder
Copy link

Nice work @skeary ; let's hope this can be merged soon!

@dcousens dcousens removed their assignment Oct 4, 2022
@dcousens dcousens force-pushed the main branch 3 times, most recently from 26e4ead to f2ca4d1 Compare October 18, 2022 22:51
@dcousens dcousens self-assigned this Jan 23, 2023
@dcousens dcousens removed their assignment Apr 19, 2023
@dcousens dcousens self-assigned this Aug 15, 2023
@dcousens dcousens removed their assignment Nov 10, 2024
@stuible
Copy link

stuible commented Feb 21, 2025

Would love the ability to sort relationship select and cards. Excited to see this feature merged!

@dcousens
Copy link
Member

Replaced by #9648

@dcousens dcousens closed this Jul 15, 2025
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.

5 participants