Skip to content

Commit 144d223

Browse files
committed
fix(forms): allow the reset of a FormAutocomplete selected value
1 parent 4152ec5 commit 144d223

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

demo/FormExamples.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ export function FormExamples() {
130130
label="Autocomplete"
131131
options={['1234', '2345', '3456']}
132132
placeholder="Type some numbers"
133-
required
134133
disabled
135134
/>
136135
</div>
@@ -148,6 +147,7 @@ export function FormExamples() {
148147
onSearch={console.log}
149148
placeholder="Type some letters"
150149
openOnFocus={true}
150+
required
151151
/>
152152
</div>
153153
<div className="col">

demo/demo.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ReactDOM.render(
1818
<StatefulTabs
1919
vertical={true}
2020
onlyRenderActiveTab={true}
21-
initialTab={5}
21+
initialTab={2}
2222
tabs={[
2323
{
2424
title: 'Dialog',

src/forms/FormAutocomplete.jsx

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef, useCallback, useEffect } from 'react';
1+
import React, { useState, useRef, useCallback, useEffect, useMemo } from 'react';
22
import PropTypes from 'prop-types';
33
import { handleInputChange, normalizeOptions, normalizeDisabled } from './helpers/form-helpers';
44
import { Dropdown } from '../mixed/Dropdown';
@@ -37,17 +37,6 @@ export function FormAutocomplete({
3737

3838
const controlFeedback = getFormSubmitedAttempted() ? (isValid() ? 'is-valid' : 'is-invalid') : '';
3939

40-
useEffect(() => {
41-
searchInputRef.current.setCustomValidity(controlFeedback === 'is-invalid' ? 'invalid' : '');
42-
}, [controlFeedback]);
43-
44-
useEffect(() => {
45-
if (isEmpty(value) && !isFocused) {
46-
setSearchValue('');
47-
setSelectedItem(null);
48-
}
49-
}, [isFocused, value]);
50-
5140
const onSearchInputType = useCallback(
5241
(_, nextSearchValue) => {
5342
setSearchValue(nextSearchValue);
@@ -61,6 +50,14 @@ export function FormAutocomplete({
6150
[onSearch, open, setValue, value]
6251
);
6352

53+
const inputHandleChange = useMemo(
54+
() =>
55+
handleInputChange.bind(null, {
56+
update: onSearchInputType,
57+
}),
58+
[onSearchInputType]
59+
);
60+
6461
const onSearchInputFocus = useCallback(() => {
6562
if (openOnFocus) {
6663
setTimeout(() => {
@@ -70,13 +67,18 @@ export function FormAutocomplete({
7067
}, [open, openOnFocus]);
7168

7269
const onSearchInputBlur = useCallback(() => {
70+
if (isEmpty(searchValue) && value) {
71+
setValue('');
72+
setSelectedItem(null);
73+
}
74+
7375
if (ignoreBlur) {
7476
searchInputRef.current.focus();
7577
} else {
7678
close();
7779
setFocus(false);
7880
}
79-
}, [close, ignoreBlur]);
81+
}, [close, ignoreBlur, searchValue, setValue, value]);
8082

8183
const enableSearchInput = useCallback(() => {
8284
if (disabled) {
@@ -103,16 +105,28 @@ export function FormAutocomplete({
103105
[close, setValue]
104106
);
105107

108+
const updateSearchInputValidation = useCallback(() => {
109+
searchInputRef.current.setCustomValidity(controlFeedback === 'is-invalid' ? 'invalid' : '');
110+
}, [controlFeedback]);
111+
112+
const clearSearchValue = useCallback(() => {
113+
if (isEmpty(value) && !isFocused) {
114+
setSearchValue('');
115+
setSelectedItem(null);
116+
}
117+
}, [isFocused, value]);
118+
119+
useEffect(updateSearchInputValidation, [updateSearchInputValidation]);
120+
useEffect(clearSearchValue, [clearSearchValue]);
121+
106122
return (
107123
<>
108124
<input
109125
{...{ placeholder, disabled }}
110-
type="text"
126+
type="search"
111127
ref={searchInputRef}
112128
className={formatClasses(['form-control form-autocomplete-search', isFocused ? '' : 'd-none', controlFeedback])}
113-
onChange={handleInputChange.bind(null, {
114-
update: onSearchInputType,
115-
})}
129+
onChange={inputHandleChange}
116130
onFocus={onSearchInputFocus}
117131
onBlur={onSearchInputBlur}
118132
value={searchValue}
@@ -134,12 +148,13 @@ export function FormAutocomplete({
134148

135149
<input
136150
type="text"
137-
className={`form-control d-none`}
151+
className={formatClasses(['form-control', 'd-none'])}
138152
{...{ name, required, id }}
139153
onChange={() => {}}
140154
value={getValue()}
141155
ref={registerRef}
142156
/>
157+
143158
<Dropdown
144159
className="form-autocomplete-dropdown"
145160
isOpen={isOpen()}

0 commit comments

Comments
 (0)