How to blur the select after a clear is executed? #5964
-
Hey everyone! I’m working with react-select in my project and I want to prevent the Select component from gaining focus after clearing the value. Using the actionMeta prop with the select ref is not working because the select is focused again after the blur. Here’s what I have so far: As you can see, right after the One workaround that I found was to put the Any tips or suggestions would be awesome! Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @Rafael-Ramblas, hope this comment by @ebonow helps: #1309 (comment). Codesandbox Link:Screenshot:Code:import { useRef, useState } from "react";
import Select, {
SelectInstance,
OnChangeValue,
ActionMeta,
} from "react-select";
import "./styles.css";
interface ColorOption {
value: string;
label: string;
}
const colorOptions: ColorOption[] = [
{ value: "red", label: "Red" },
{ value: "green", label: "Green" },
{ value: "blue", label: "Blue" },
];
const SelectComponent: React.FC = () => {
const [value, setValue] = useState();
const selectRef = useRef<SelectInstance<ColorOption> | null>(null);
function onClear() {
setTimeout(() => selectRef?.current?.blur(), 1);
}
const handleChange = (
newValue: OnChangeValue<ColorOption, boolean>,
actionMeta: ActionMeta<ColorOption>
) => {
if (actionMeta.action === "clear") {
onClear();
}
setValue(newValue);
};
return (
<div>
<Select
ref={selectRef}
options={colorOptions}
onChange={handleChange}
value={value}
isClearable
onBlur={() => console.warn("blur")}
onFocus={() => console.warn("focus")}
/>
</div>
);
};
export default function App() {
return <SelectComponent />;
} |
Beta Was this translation helpful? Give feedback.
-
Hey @manjushsh, thanks for the answer! |
Beta Was this translation helpful? Give feedback.
Hi @Rafael-Ramblas, hope this comment by @ebonow helps: #1309 (comment).
Codesandbox Link:
Code Sandbox
Screenshot:
Code: