Skip to content

Commit 3c70021

Browse files
onCheckNewValue on SelectInput and AutocompleteInput (#812)
Signed-off-by: Mathieu DEHARBE <[email protected]>
1 parent 91e085a commit 3c70021

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/components/inputs/reactHookForm/autocompleteInputs/AutocompleteInput.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import { useMemo } from 'react';
99
import { Autocomplete, AutocompleteProps, TextField, TextFieldProps } from '@mui/material';
1010
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';
1614

1715
export interface AutocompleteInputProps
1816
extends Omit<
@@ -31,6 +29,7 @@ export interface AutocompleteInputProps
3129
onChangeCallback?: () => void;
3230
formProps?: Omit<TextFieldProps, 'value' | 'onChange' | 'inputRef' | 'inputProps' | 'InputProps'>;
3331
disabledTooltip?: boolean;
32+
onCheckNewValue?: (value: Option | null) => boolean; // if return false, do not apply the new value
3433
}
3534

3635
export function AutocompleteInput({
@@ -45,6 +44,7 @@ export function AutocompleteInput({
4544
onChangeCallback, // method called when input value is changing
4645
formProps,
4746
disabledTooltip,
47+
onCheckNewValue,
4848
...props
4949
}: AutocompleteInputProps) {
5050
const { validationSchema, getValues, removeOptional, isNodeBuilt, isUpdate } = useCustomFormContext();
@@ -59,6 +59,9 @@ export function AutocompleteInput({
5959
if (currentValue?.id === newValue) {
6060
return;
6161
}
62+
if (!onCheckNewValue?.(newValue)) {
63+
return;
64+
}
6265
onChangeCallback?.();
6366
// if free solo not enabled or if value is not of string type, we call onChange right away
6467
if (!allowNewValue || typeof newValue !== 'string') {

src/components/inputs/reactHookForm/selectInputs/SelectInput.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
*/
77

88
import { useIntl } from 'react-intl';
9-
import { AutocompleteInput, AutocompleteInputProps } from '../autocompleteInputs/AutocompleteInput';
10-
import { Option } from '../../../../utils/types/types';
9+
import { AutocompleteInput, AutocompleteInputProps } from '../autocompleteInputs';
10+
import { Option } from '../../../../utils';
1111

1212
export interface SelectInputProps
1313
extends Omit<
1414
AutocompleteInputProps,
1515
'outputTransform' | 'inputTransform' | 'readOnly' | 'getOptionLabel' // already defined in SelectInput
1616
> {
1717
options: Option[];
18+
onCheckNewValue?: (value: Option | null) => boolean; // if return false, do not apply the new value
1819
}
1920

2021
export function SelectInput(props: SelectInputProps) {

0 commit comments

Comments
 (0)