Skip to content

Commit bd6b345

Browse files
committed
fix(forms): fix selected item on FormAutocomplete when options is a dynamic array
1 parent f088053 commit bd6b345

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

demo/TableExamples.jsx

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ export function TableExamples() {
8787
attribute: 'selected',
8888
label: '#',
8989
format(_, __, index) {
90-
console.log(index);
91-
9290
return (
9391
<div onClick={stopPropagation}>
9492
<FormCheckbox name={`selected[${index}].isSelected`} id={`selected[${index}].isSelected`} />

demo/demo.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ReactDOM.render(
1616
<ToastsContainer>
1717
<StatefulTabs
1818
vertical={true}
19-
initialTab={4}
19+
initialTab={1}
2020
tabs={[
2121
{
2222
title: 'Dialog',

src/forms/FormAutocomplete.jsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ export function FormAutocomplete({
1818
filter,
1919
disabled: _disabled,
2020
}) {
21+
const { getValue, setValue, register, isValid, getFormSubmitedAttempted, getFormData } = useFormControl(name);
22+
const value = getValue();
23+
2124
const [searchValue, setSearchValue] = useState('');
25+
const items = normalizeOptions(options, getFormData(), searchValue);
26+
const _selectedItem = items.find((item) => item.value === value);
27+
28+
const [selectedItem, setSelectedItem] = useState(_selectedItem);
2229
const { isOpen, open, close } = useOpenState();
2330
const [ignoreBlur, setIgnoreBlur] = useState(false);
2431
const [isFocused, setFocus] = useState(false);
25-
const { getValue, setValue, register, isValid, getFormSubmitedAttempted, getFormData } = useFormControl(name);
2632
const registerRef = useCallback(register, []);
2733
const searchInputRef = useRef(null);
2834

29-
const items = normalizeOptions(options, getFormData(), searchValue);
30-
const value = getValue();
31-
const selectedItem = items.find((item) => item.value === value);
3235
const disabled = normalizeDisabled(_disabled, getFormData());
3336

34-
const controlFeedback = `${getFormSubmitedAttempted() ? (isValid() ? 'is-valid' : 'is-invalid') : ''}`;
37+
const controlFeedback = getFormSubmitedAttempted() ? (isValid() ? 'is-valid' : 'is-invalid') : '';
3538

3639
useEffect(() => {
3740
searchInputRef.current.setCustomValidity(controlFeedback === 'is-invalid' ? 'invalid' : '');
@@ -110,6 +113,7 @@ export function FormAutocomplete({
110113
onSelect={({ value, label }) => {
111114
setValue(value);
112115
setSearchValue(label);
116+
setSelectedItem({ value, label });
113117
setIgnoreBlur(false);
114118
setTimeout(() => {
115119
setFocus(false);
@@ -135,6 +139,7 @@ FormAutocomplete.defaultProps = {
135139

136140
return itemValue.includes(searchValue);
137141
},
142+
template: (x) => x,
138143
};
139144

140145
FormAutocomplete.propTypes = {

0 commit comments

Comments
 (0)