tinloof / sanity-kit

83 stars 11 forks source link

Pathname field cannot type a dash "-" #24

Closed Jamiewarb closed 6 months ago

Jamiewarb commented 6 months ago

When editing pathnames, it's not possible to type a dash at the end of the pathname due to the validation.

Instead, you have to type the whole pathname, and then go back and fill in where you want the dashes.

I believe this is because the pathname validates immediately on change, and as a dash at the end of the pathname is invalid, it is immediately removed on typing.

I can think of two solutions to this:

  1. Implement slightly weaker validation on Change, and add the current validation on Blur - this would allow some invalid characters at the end of the pathname in expectation the user will keep typing, and removes them on Blur
  2. Move validation only to on Blur

Do we want to change this behaviour, and what would be the preference for implementation?

tamaccount commented 6 months ago

I looked into this issue too, and discovered more about it.

For some reason, two different validation methods are used, depending on this check: https://github.com/tinloof/sanity-kit/blob/9c8a80b3afcfb8c0f1a5494e3a38ca3102a020d0/packages/sanity-studio/src/components/PathnameFieldComponent.tsx#L101

If the condition results true to true, updateFinalSegment https://github.com/tinloof/sanity-kit/blob/9c8a80b3afcfb8c0f1a5494e3a38ca3102a020d0/packages/sanity-studio/src/components/PathnameFieldComponent.tsx#L55 is used as the onChange handler. In this handler, the package speakingurl is used to edit the pathname on every change event. The problem with this method is that speakingurl never allows for trailing dashes; The function always strips them away.

If the folderLocked && folder condition resolves to false, then a less strict method is used as the onChange event. updateFullPath https://github.com/tinloof/sanity-kit/blob/9c8a80b3afcfb8c0f1a5494e3a38ca3102a020d0/packages/sanity-studio/src/components/PathnameFieldComponent.tsx#L68 just ends up calling stringToPathname.stringToPathname does allow dashes ("-") to be used as the final character in the pathname.


Please make a change to updateFinalSegment. In my opinion, a strict package like speakingurl is not required. stringToPathname or something like it is plenty sufficient. It is not that much of an issue if a user is able to enter in a pathname ending with a dash. Once again, stringToPathname already allows this to occur.

It is very annoying that currently, if I want to append an existing pathname with a folder, I cannot add any dashes to the end of the string

thomasKn commented 6 months ago

Can you try v1.1.0 and let me know if solves the issue?