Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Validate new categories on input #198

Conversation

mike-day
Copy link
Contributor

Tasks

Summary of changes

Previously, typing certain characters could cause issues (up to a whitescreen) when new categories were created in the sidebar UI. This PR adds strict validation as the user types, then strips disallowed characters on Enter.

Related Issues

How to test

  1. Open or create a pattern
  2. Try adding a category with non-alphanumeric characters
  3. Expected: the disallowed characters should be stripped on Enter

Notes & Screenshots

A hint is provided by a red border that only appears when disallowed chars are detected. Additionally, the Create "" label shown below the input as the new category title is typed has the disallowed characters stripped:

Screenshot 2023-06-15 at 2 24 14 PM

@mike-day mike-day self-assigned this Jun 15, 2023
@kienstra
Copy link
Contributor

Looks good, I'll review this late today if that's alright.

Comment on lines +28 to +29
const [ categoryTitleIsInvalid, setCategoryTitleIsInvalid ] =
useState( false );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't think of a simple way to show the modified style (the styles prop in Creatable below) without using state.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine, it's simple

@@ -0,0 +1,15 @@
const defaultPattern = new RegExp( /([^a-z0-9 -]+)/gi );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

defaultPattern (and its usage as a default param) might be unneeded, but I like how simple it is to test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Haha, you knew I'd comment on that! 🤣

Good enough reason to keep defaultPattern

Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Hi @mike-day,
Nice work, and really good UI feedback when there are illegal characters.

@@ -0,0 +1,15 @@
const defaultPattern = new RegExp( /([^a-z0-9 -]+)/gi );
Copy link
Contributor

Choose a reason for hiding this comment

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

Haha, you knew I'd comment on that! 🤣

Good enough reason to keep defaultPattern

@@ -64,12 +75,29 @@ export default function CategoriesPanel( {
}
);
} }
onInputChange={ ( event ) => {
setCategoryTitleIsInvalid(
!! checkIllegalChars( event )
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe checkIllegalChars() could return a boolean, so it doesn't need !! in front of it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good suggestion! Updated with 0e133b2.

Copy link
Contributor Author

@mike-day mike-day Jun 16, 2023

Choose a reason for hiding this comment

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

Updated the name to hasIllegalChars for that function as well. It is a much more descriptive name for this usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

0e133b2 looks good! Good idea for hasIllegalChars().

Comment on lines +28 to +29
const [ categoryTitleIsInvalid, setCategoryTitleIsInvalid ] =
useState( false );
Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine, it's simple

menuPlacement="auto"
styles={ {
menu: ( base ) => ( {
...base,
zIndex: 100,
} ),
control: ( baseStyles ) => ( {
...baseStyles,
borderColor: categoryTitleIsInvalid
Copy link
Contributor

Choose a reason for hiding this comment

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

Really nice handling.

You can still create a category with the valid characters, but you know something's invalid:

Screenshot 2023-06-15 at 3 19 24 PM

[
'String !#@$% with ^&*() separated \'"? illegal []{}|/ chars',
[ '!#@$%', '^&*()', '\'"?', '[]{}|/' ],
],
Copy link
Contributor

Choose a reason for hiding this comment

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

Good test cases

@mike-day mike-day merged commit bf3d3e8 into try/add-new-categories-in-editor-ui Jun 16, 2023
@mike-day mike-day deleted the bugfix/validate-new-category-input branch June 16, 2023 14:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants