Skip to content

Commit 48038ca

Browse files
committed
fix(experiments): add some experiments
1 parent 09823a4 commit 48038ca

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/components/AutocompleteController.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
useFieldControllerWithOptionsLabels,
66
useOnErrorMessage,
77
} from '../hooks/index';
8-
import { useController } from 'react-hook-form';
8+
import { useController, useWatch } from 'react-hook-form';
99
import { useEffect, useState } from 'react';
1010
import type {
1111
AsyncFieldControllerProps,
@@ -88,32 +88,38 @@ export const AutocompleteController = <
8888
> | null>(initialValue ?? null);
8989

9090
const {
91-
field: { onChange, value, ...other },
91+
field: { onChange, ...rest },
9292
fieldState: { invalid, error },
9393
} = useController({
9494
control,
9595
name,
9696
});
9797

98+
const formValue = useWatch({
99+
control,
100+
name,
101+
});
102+
98103
/**
99104
* Side effects
100105
*/
101106
useEffect(() => {
102-
if (value) {
103-
const optionFound = options.find((option) => optionValueAccessor(option) === value);
107+
if (formValue) {
108+
const optionFound = options.find((option) => optionValueAccessor(option) === formValue);
104109

105110
if (optionFound) {
106111
setSelectedValue(optionFound as AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo> | null);
112+
onChange(optionValueAccessor(optionFound));
107113
}
108114
}
109-
}, [value, optionValueAccessor, options]);
115+
}, [formValue, optionValueAccessor, options]);
110116

111117
/**
112118
* Render
113119
*/
114120
return (
115121
<Autocomplete
116-
{...other}
122+
{...rest}
117123
aria-required={optional ? 'false' : 'true'}
118124
getOptionLabel={(value) => {
119125
if (!value) {

0 commit comments

Comments
 (0)