Clear current value text on focus
Is there a way to clear the current value text when focusing a select with searchable equal to true? Would like the input to empty when focused, and refill when blurred.
I was wondering the same thing!
Ping?
Same, Users don't seem to notice the list is searchable as the field is not cleared onFocus. The caret navigation is blinking but not very visible. I tried the following, but did not work:
value={this.state.isFocused ? this.state.inputValue : selected_id}
Stuck with the same issue. How did you guys fix it?
.Select.is-open .Select-value {
display: none;
}
^^ Setting display: none removes the placeholder as well
Does anyone have a v2 solution to this?
i've tried something like this but with no result
const selectStyleFn = {
valueContainer : (base: any, state: any) => {
return { display: 'none' }
}, singleValue : (base: any, state: any) => {
return { display: 'none' }
},
}
return (
<Async
style={selectStyleFn}
key={'omni-select-monad'}
name='omni-select'
value={this.state.searchValue}
className={style.search}
placeholder={'Search...'}
menuContainerStyle={{ maxHeight: '402px' }}
menuStyle={{ maxHeight: '400px' }}
onChange={(e: any) => this.selectObject(e)}
loadOptions={this.combineObservables}
/>
)
Following up on this, I figured out a solution for the v2 version. What you have to do is set the value to null, (and do not set the object to { value: null ...}). Example given below
onFocus = () => { this.setState({ searchValue: null }) }
...
<Async
style={selectStyleFn}
key={'omni-select-monad'}
name='omni-select'
value={this.state.searchValue}
className={style.search}
placeholder={'Search...'}
menuContainerStyle={{ maxHeight: '402px' }}
menuStyle={{ maxHeight: '400px' }}
onFocus={this.onFocus}
onChange={(e: any) => this.selectObject(e)}
loadOptions={this.combineObservables}
/>
This is how I've done it (simplified the most I could), hope it can help:
const { value } = this.props
const { focus } = this.state
return (
<Select
value={focus ? '' : value}
onFocus={()=> this.setState({focus: true})}
onBlur={()=> this.setState({focus: false})}
blurInputOnSelect={true}
/>
)
I solved it by adding custom css for focused AsyncSelect component.
.YOURCLASSNAME__field__control--is-focused .CustomValueRenderClass {
display: none;
}
Cleanest solution:
onInputChange (...args) {
this.setState({ isLastActionFocus: false });
this.props.onInputChange && this.props.onInputChange(...args);
}
onBlur (...args) {
this.setState({ isLastActionFocus: false });
this.props.onBlur && this.props.onBlur(...args);
}
onFocus (...args) {
this.setState({ isLastActionFocus: true });
this.props.onFocus && this.props.onFocus(...args);
}
render () {
const { value, ...restProps } = this.props;
return <AsyncSelect
onInputChange={this.onInputChange}
value={this.state.isLastActionFocus ? '' : value}
loadOptions={getAddresses}
onFocus={this.onFocus}
onBlur={this.onBlur}
{...restProps}
/>;
CSS fix for 2019, works with version 3.0.4:
Add following properties to your react-select component:
className={"react-select-container"}
classNamePrefix={"react-select"}
and create following CSS:
.react-select__control--is-focused.react-select__control--menu-is-open .react-select__single-value {
display: none;
}
The current value will not be shown when component is in focus and will return after blur occurs.
Thanks for the simple suggestion, @sombrerox! I used that general CSS approach except instead of targeting their really specific class in a style sheet, I used the style object approach:
const customStyles = {
singleValue: (provided, state) => {(
display: state.selectProps.menuIsOpen ? 'none' : 'block',
)}
}
const App = () => (
<Select
styles={customStyles}
options={...}
/>
);
@apennell Thank you for the quick and simple solution! Was wracking my brain on how to handle this properly and none of the other solutions would work properly.
Hello -
In an effort to sustain the react-select project going forward, we're closing old issues.
We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version.
If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.
However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you!
@apennell Thank you. You saved my day.
This seems to be working well. clearValue is a default method common to components
<AsyncSelect { ...commonDDLProps }
ref={ ddlRef }
onFocus={ (evt) => {
ddlRef.current.select.select.clearValue(evt);
} }
it is not normal when there are so many problems with the instrument. Starting with the lack of documentation, and ending with a bunch of different ways of entering arguments in onChange.
My task is,
- hide value if input is focused to show placeholder
- restore the previous value if nothing is selected and focus is removed
my solution now looks like this:
const MySelect = ({defaultValue}) => {
...
const [isFocused, setFocus] = React.useState(false);
return (
<Root>
{isFocused ? (
<AsyncSelect
...
autoFocus
onBlur={() => { setFocus(false); }}
placeholder="Enter and select a city"
/>
) : (
<FakeLocationSelector
onClick={() => { setFocus(true); }}
>
{defaultValue.label}
</FakeLocationSelector>
)}
</Root>
);
};
const FakeLocationSelector = styled.div`
padding: 22px 0 22px 51px;
...
`;
works like a charm
I used the onMenuOpen prop to achieve this.
const [value, setValue] = useState(null);
.......
<AsyncSelect
value={value}
placeholder="Search"
onChange={handleChange}
onMenuOpen={() => setValue("")}
/>
Here's what I did.
import { Fragment, useState } from "react";
import Select, { components } from "react-select";
const options = [
{ label: "one", value: 1 },
{ label: "two", value: 2 },
{ label: "three", value: 3 }
];
/** Hide the selected value when showing the dropdown list */
function SingleValue(props) {
const { children, ...rest } = props;
const { selectProps } = props;
if (selectProps.menuIsOpen) return <Fragment></Fragment>;
return <components.SingleValue {...rest}>{children}</components.SingleValue>;
}
// For TypeScript, declare like this:
// function SingleValue<T>(props: SingleValueProps<T, false>) {
export default function App() {
const [selectedOption, setSelectedOption] = useState(options[1]);
return (
<Select
options={options}
components={{ SingleValue }}
value={selectedOption}
onChange={setSelectedOption}
/>
);
}
Live demo: https://codesandbox.io/s/hide-selected-on-focus-react-select-n05yi2?file=/src/App.js:0-877
I also recorded this solution in a Stack Overflow Q&A to help others in the future.