-
Notifications
You must be signed in to change notification settings - Fork 6
Validate new categories on input #198
Changes from 3 commits
45d4f26
ca0e239
6f9e726
0e133b2
2dbe129
00bee10
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState } from '@wordpress/element'; | ||
import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; | ||
import { Spinner } from '@wordpress/components'; | ||
|
||
|
@@ -7,6 +8,10 @@ import Creatable from 'react-select/creatable'; | |
import toKebabCase from '../../utils/toKebabCase'; | ||
import getSelectedOptions from '../../utils/getSelectedOptions'; | ||
import getCustomCategories from '../../utils/getCustomCategories'; | ||
import { | ||
checkIllegalChars, | ||
stripIllegalChars, | ||
} from '../../utils/validateInput'; | ||
import type { BaseSidebarProps, AdditionalSidebarProps } from './types'; | ||
|
||
/** | ||
|
@@ -20,6 +25,9 @@ export default function CategoriesPanel( { | |
handleChange, | ||
}: BaseSidebarProps< 'categories' | 'customCategories' > & | ||
AdditionalSidebarProps< 'categoryOptions' > ) { | ||
const [ categoryTitleIsInvalid, setCategoryTitleIsInvalid ] = | ||
useState( false ); | ||
|
||
return ( | ||
<PluginDocumentSettingPanel | ||
name="patternmanager-pattern-editor-pattern-categories" | ||
|
@@ -55,7 +63,10 @@ export default function CategoriesPanel( { | |
onCreateOption={ ( newCategoryTitle ) => { | ||
handleChange( | ||
'customCategories', | ||
[ ...customCategories, newCategoryTitle ], | ||
[ | ||
...customCategories, | ||
stripIllegalChars( newCategoryTitle ), | ||
kienstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
{ | ||
categories: [ | ||
...categories, | ||
|
@@ -64,12 +75,29 @@ export default function CategoriesPanel( { | |
} | ||
); | ||
} } | ||
onInputChange={ ( event ) => { | ||
setCategoryTitleIsInvalid( | ||
!! checkIllegalChars( event ) | ||
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. Maybe 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. That's a good suggestion! Updated with 0e133b2. 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. Updated the name to 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. 0e133b2 looks good! Good idea for |
||
); | ||
} } | ||
formatCreateLabel={ ( userInput ) => | ||
`Create "${ stripIllegalChars( userInput ) }"` | ||
} | ||
menuPlacement="auto" | ||
styles={ { | ||
menu: ( base ) => ( { | ||
...base, | ||
zIndex: 100, | ||
} ), | ||
control: ( baseStyles ) => ( { | ||
...baseStyles, | ||
borderColor: categoryTitleIsInvalid | ||
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. |
||
? 'red !important' | ||
: baseStyles.borderColor, | ||
boxShadow: categoryTitleIsInvalid | ||
? '0 0 0 1px red' | ||
: baseStyles.boxShadow, | ||
} ), | ||
} } | ||
/> | ||
) : ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { checkIllegalChars, stripIllegalChars } from '../validateInput'; | ||
|
||
const regexPattern = new RegExp( /([^a-z0-9 -]+)/gi ); | ||
|
||
describe( 'validateInput', () => { | ||
describe( 'checkIllegalChars', () => { | ||
it.each( [ | ||
[ '', null ], | ||
[ 'Nothing to strip', null ], | ||
[ 'String with !#@$%^&*() illegal chars', [ '!#@$%^&*()' ] ], | ||
[ | ||
'String !#@$% with ^&*() separated \'"? illegal []{}|/ chars', | ||
[ '!#@$%', '^&*()', '\'"?', '[]{}|/' ], | ||
], | ||
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. Good test cases |
||
] )( 'matches the illegal characters', ( input, expected ) => { | ||
expect( checkIllegalChars( input, regexPattern ) ).toEqual( | ||
expected | ||
); | ||
} ); | ||
} ); | ||
describe( 'stripIllegalChars', () => { | ||
it.each( [ | ||
[ '', '' ], | ||
[ 'Nothing to strip', 'Nothing to strip' ], | ||
[ | ||
'String with !#@$%^&*() illegal chars', | ||
'String with illegal chars', | ||
], | ||
[ | ||
"This might've caused a whitescreen previously!", | ||
'This mightve caused a whitescreen previously', | ||
], | ||
] )( 'strips the illegal characters', ( input, expected ) => { | ||
expect( stripIllegalChars( input, regexPattern ) ).toBe( expected ); | ||
} ); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const defaultPattern = new RegExp( /([^a-z0-9 -]+)/gi ); | ||
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. Haha, you knew I'd comment on that! 🤣 Good enough reason to keep |
||
|
||
export function checkIllegalChars( | ||
input: string, | ||
regexPattern = defaultPattern | ||
) { | ||
return input.match( regexPattern ); | ||
} | ||
|
||
export function stripIllegalChars( | ||
input: string, | ||
regexPattern = defaultPattern | ||
) { | ||
return input.replace( regexPattern, '' ); | ||
} |
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 couldn't think of a simple way to show the modified style (the
styles
prop inCreatable
below) without using state.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.
That's fine, it's simple