Skip to content

Commit 60624bd

Browse files
Add isValidNewOption to MultiSuggestField
1 parent bfcbe84 commit 60624bd

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1818
- `useOnly` property: specify if only parts of the content should be used for the shortened preview, this property replaces `firstNonEmptyLineOnly`
1919
- `<ContentBlobToggler />`
2020
- `forceInline` property: force inline rendering
21+
- `<MultiSuggestField />`:
22+
- `isValidNewOption` property: Checks if an input string is or can be turned into a valid new option.
2123

2224
### Fixed
2325

src/components/MultiSelect/MultiSelect.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ interface MultiSuggestFieldCommonProps<T>
7070
*/
7171
newItemCreationText?: string;
7272
/**
73-
* Allows to creates new item from a given query. If this is not provided then no new items can be created.
73+
* Allows to create new item from a given query. If this is not provided then no new items can be created.
7474
*/
7575
createNewItemFromQuery?: (query: string) => T;
76+
/** Validates if a new item can be created from the current query string. */
77+
isValidNewOption?: (query: string) => boolean;
7678
/**
7779
* Items that were newly created and not taken from the list will be post-fixed with this string.
7880
*/
@@ -159,6 +161,7 @@ export function MultiSuggestField<T>({
159161
newItemPostfix = " (new item)",
160162
disabled,
161163
createNewItemFromQuery,
164+
isValidNewOption,
162165
requestDelay = 0,
163166
clearQueryOnSelection = false,
164167
className,
@@ -387,7 +390,9 @@ export function MultiSuggestField<T>({
387390
*/
388391
const handleOnKeyUp = (event: React.KeyboardEvent<HTMLElement>) => {
389392
if (event.key === "Enter" && !filteredItems.length && !!requestState.current.query && createNewItemFromQuery) {
390-
createNewItem(requestState.current.query);
393+
if(!isValidNewOption || isValidNewOption(requestState.current.query)) {
394+
createNewItem(requestState.current.query);
395+
}
391396
}
392397
inputRef.current?.focus();
393398
};
@@ -403,7 +408,11 @@ export function MultiSuggestField<T>({
403408
if (focusedItem) {
404409
onItemSelect(focusedItem);
405410
} else {
406-
onItemSelect(createNewItem(requestState.current.query));
411+
if (!isValidNewOption || isValidNewOption(requestState.current.query)) {
412+
onItemSelect(createNewItem(requestState.current.query));
413+
} else {
414+
return
415+
}
407416
}
408417
requestState.current.query = "";
409418
setTimeout(() => inputRef.current?.focus());
@@ -418,7 +427,7 @@ export function MultiSuggestField<T>({
418427
* @returns
419428
*/
420429
const newItemRenderer = (label: string, active: boolean, handleClick: React.MouseEventHandler<HTMLElement>) => {
421-
if (!createNewItemFromQuery) return undefined;
430+
if (!createNewItemFromQuery || isValidNewOption && !isValidNewOption(label)) return undefined;
422431
const clickHandler = (e: React.MouseEvent<HTMLElement>) => {
423432
createNewItem(label);
424433
handleClick(e);

0 commit comments

Comments
 (0)