react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Clear current value text on focus

Open stinoga opened this issue 10 years ago • 20 comments

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.

stinoga avatar Nov 03 '16 18:11 stinoga

I was wondering the same thing!

Ricka7x avatar Dec 01 '16 00:12 Ricka7x

Ping?

stinoga avatar Jan 03 '17 16:01 stinoga

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}

KaihatsuOnline avatar Oct 10 '17 02:10 KaihatsuOnline

Stuck with the same issue. How did you guys fix it?

shreyass avatar Mar 11 '18 09:03 shreyass

.Select.is-open .Select-value { display: none; }

Ericky14 avatar May 03 '18 18:05 Ericky14

^^ Setting display: none removes the placeholder as well

pondrejack avatar Aug 06 '18 22:08 pondrejack

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}
      />
    )

captaincole avatar Aug 06 '18 23:08 captaincole

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}
      />

captaincole avatar Aug 16 '18 01:08 captaincole

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}
      />
    )

code-jorge avatar Dec 18 '18 17:12 code-jorge

I solved it by adding custom css for focused AsyncSelect component.

.YOURCLASSNAME__field__control--is-focused .CustomValueRenderClass {
  display: none;
}

ykokw avatar Jan 21 '19 06:01 ykokw

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}
    />;

barroudjo avatar Jun 04 '19 13:06 barroudjo

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.

sombrerox avatar Jun 20 '19 11:06 sombrerox

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 avatar Jun 24 '19 19:06 apennell

@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.

calebhypes avatar Jul 22 '19 19:07 calebhypes

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!

bladey avatar May 28 '20 05:05 bladey

@apennell Thank you. You saved my day.

mununki avatar Oct 13 '20 03:10 mununki

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);
                                } }
                               

pstephenwille avatar Feb 01 '21 19:02 pstephenwille

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

jt3k avatar Mar 23 '21 19:03 jt3k

I used the onMenuOpen prop to achieve this.

const [value, setValue] = useState(null);

.......

<AsyncSelect
value={value}
placeholder="Search"
onChange={handleChange}
onMenuOpen={() => setValue("")}
/>

ghost avatar Jun 17 '21 12:06 ghost

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.

justingrant avatar Aug 17 '22 23:08 justingrant