-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onClear #1309
Comments
Hi @babenzele, I had the same inconvenient, you can try this temporary solution:
|
@jfelipebc Thanks! That's a good move putting the onClear on the clear button's onClick. |
It would be great to have an onClear prop - I have a number of select boxes on the first page and athough the onChange event is fired, nothing is passed with it. It would be great to be able to pass the item object back up the onClear chain. |
is onClear implemented in current version? |
Hi! I really need the onClear prop :( |
News? This is extremely important! Especially if the implementer uses the new react api with hooks! ⚛ ⚓ |
Docs are not super "clear" on this, but looks like you can use second argument to onChange to detect clearing. <Select onChange={(selectedOption, triggeredAction) => {
if (triggeredAction.action === 'clear') {
// Clear happened
}
}} /> Hope this helps. |
Thanks it helps, but only solves a part of the problem for me sinceis onFocus is triggered just after that so in my case it clears the state I'm using to set style on focus. |
Hey guys, Check this sandbox that I made: |
Any update on whether an onClear prop can be added to the library? Or can there be an alternate solution where we can override the |
I've started a fork of react-select. Feel free to resubmit this issue on the fork and/or submit a PR to resolve this issue on the fork and we can get it merged and released. |
Greetings, The onChange handler is meant to deal with the clear button. I understand it's not documented perhaps as well as it could be and documentation is certainly on the docket of things to address, but this is still a one and a half line code implementation to achieve the result being asked for. const selectRef = useRef();
const onChange = (_, { action }) => action === "clear" && setTimeout(() => selectRef.current.select.blur(), 1); Yes, setTimeout is not ideal, but it does accomplish adding this to the end of the event queue after the state changes. Given the age of this post, I will be closing this so we can continue to focus our efforts on bugs and issues. Please let me know if anyone has any issues with the provided sandbox/solution. |
Does this still work? This "clear" action doesn't seems to exist anymore. EditIn case someone else need this, I've found that workaround: import Select, { components } from 'react-select'; const clear = () => {
setValue(null);
setInputValue('');
};
const ClearIndicator = (props) => {
const customProps = {
...props,
innerProps: {
...props.innerProps,
onMouseDown: clear,
},
};
return <components.ClearIndicator {...customProps} />;
}; <Select
components={{ ClearIndicator }}
value={value}
inputValue={inputValue}
/> This is not the full code, but I think you can get the idea. |
What doesn't work in the sample code I provided above is the "blur" since the refs have changed from v4 -> v5. For v5, the code would instead look something like... const onChange = (_, { action }) => action === "clear" && setTimeout(() => selectRef.current.blur(), 1); Note that this is for an UNCONTROLLED input. For a controlled input (value is passed to the Select via its prop), then yes you would of course need to change the const [ value, setValue ] = useState();
const selectRef = useRef();
const onClear = setTimeout(() => selectRef.current.blur(), 1);
const onChange = (selected, { action }) => {
if (action === "clear") {
onClear();
}
setValue(selected);
};
return <Select ref={selectRef} value={value} onChange={onChange} {...otherProps} /> That said, your solution of creating a custom ClearIndicator component would also be a viable approach. |
@ebonow In my case |
@brendomaciel not sure what is happening in your implementation, but there doesn't appear to be any issues with the solution I suggested https://codesandbox.io/s/sad-grothendieck-ev5c8?file=/src/App.js |
@ebonow Thank you very much for your time and insights. I will investigate and, if I found, I will share the cause here. |
Seems that still not working for some weird reason haha anyway, I used the example of @ebonow to fix the select in my project, oh, and thanks for the example :) here is how I create the ClearIndicator:
|
I resolved the issue by just checking wether |
For reference I am pasting my code in case if it can be of help to others
|
It could be nice to have an onClear prop where I could set a function to run when the select is cleared. That way I can update my state when the clear button is clicked.
I'm working around this by checking for an empty value in onChange.
The text was updated successfully, but these errors were encountered: