Skip to content

Commit

Permalink
[@mantine/core] Add option to not call onChange in creatable Select a…
Browse files Browse the repository at this point in the history
…nd MultiSelect when onCreate function returns null or undefined (#2095)
  • Loading branch information
rtivital committed Aug 18, 2022
1 parent f221971 commit 98b4f90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/mantine-core/src/MultiSelect/MultiSelect.tsx
Expand Up @@ -75,7 +75,7 @@ export interface MultiSelectProps
shouldCreate?(query: string, data: SelectItem[]): boolean;

/** Called when create option is selected */
onCreate?(query: string): SelectItem | string;
onCreate?(query: string): SelectItem | string | null | undefined;

/** Change dropdown component, can be used to add custom scrollbars */
dropdownComponent?: any;
Expand Down Expand Up @@ -334,10 +334,12 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
} else {
if (item.creatable && typeof onCreate === 'function') {
const createdItem = onCreate(item.value);
if (typeof createdItem === 'string') {
setValue([..._value, createdItem]);
} else {
setValue([..._value, createdItem.value]);
if (typeof createdItem !== 'undefined' && createdItem !== null) {
if (typeof createdItem === 'string') {
setValue([..._value, createdItem]);
} else {
setValue([..._value, createdItem.value]);
}
}
} else {
setValue([..._value, item.value]);
Expand Down
12 changes: 7 additions & 5 deletions src/mantine-core/src/Select/Select.tsx
Expand Up @@ -107,7 +107,7 @@ export interface SelectProps
shouldCreate?(query: string, data: SelectItem[]): boolean;

/** Called when create option is selected */
onCreate?(query: string): SelectItem | string;
onCreate?(query: string): SelectItem | string | null | undefined;

/** Change dropdown component, can be used to add native scrollbars */
dropdownComponent?: any;
Expand Down Expand Up @@ -296,10 +296,12 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
} else {
if (item.creatable && typeof onCreate === 'function') {
const createdItem = onCreate(item.value);
if (typeof createdItem === 'string') {
handleChange(createdItem);
} else {
handleChange(createdItem.value);
if (typeof createdItem !== 'undefined' && createdItem !== null) {
if (typeof createdItem === 'string') {
handleChange(createdItem);
} else {
handleChange(createdItem.value);
}
}
} else {
handleChange(item.value);
Expand Down

0 comments on commit 98b4f90

Please sign in to comment.