8
8
import { useMemo } from 'react' ;
9
9
import { Autocomplete , AutocompleteProps , TextField , TextFieldProps } from '@mui/material' ;
10
10
import { useController } from 'react-hook-form' ;
11
- import { genHelperError , identity , isFieldRequired } from '../utils/functions' ;
12
- import { FieldLabel } from '../utils/FieldLabel' ;
13
- import { useCustomFormContext } from '../provider/useCustomFormContext' ;
14
- import { Option } from '../../../../utils/types/types' ;
15
- import { HelperPreviousValue } from '../utils/HelperPreviousValue' ;
11
+ import { genHelperError , identity , isFieldRequired , FieldLabel , HelperPreviousValue } from '../utils' ;
12
+ import { useCustomFormContext } from '../provider' ;
13
+ import { Option } from '../../../../utils' ;
16
14
17
15
export interface AutocompleteInputProps
18
16
extends Omit <
@@ -31,6 +29,7 @@ export interface AutocompleteInputProps
31
29
onChangeCallback ?: ( ) => void ;
32
30
formProps ?: Omit < TextFieldProps , 'value' | 'onChange' | 'inputRef' | 'inputProps' | 'InputProps' > ;
33
31
disabledTooltip ?: boolean ;
32
+ onCheckNewValue ?: ( value : Option | null ) => boolean ; // if return false, do not apply the new value
34
33
}
35
34
36
35
export function AutocompleteInput ( {
@@ -45,6 +44,7 @@ export function AutocompleteInput({
45
44
onChangeCallback, // method called when input value is changing
46
45
formProps,
47
46
disabledTooltip,
47
+ onCheckNewValue,
48
48
...props
49
49
} : AutocompleteInputProps ) {
50
50
const { validationSchema, getValues, removeOptional, isNodeBuilt, isUpdate } = useCustomFormContext ( ) ;
@@ -59,6 +59,9 @@ export function AutocompleteInput({
59
59
if ( currentValue ?. id === newValue ) {
60
60
return ;
61
61
}
62
+ if ( ! onCheckNewValue ?.( newValue ) ) {
63
+ return ;
64
+ }
62
65
onChangeCallback ?.( ) ;
63
66
// if free solo not enabled or if value is not of string type, we call onChange right away
64
67
if ( ! allowNewValue || typeof newValue !== 'string' ) {
0 commit comments