Skip to content
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

[useAutocomplete] Add option to disable selecting text on first focus #19134

Closed
1 task done
Bebersohl opened this issue Jan 8, 2020 · 8 comments · Fixed by #19281
Closed
1 task done

[useAutocomplete] Add option to disable selecting text on first focus #19134

Bebersohl opened this issue Jan 8, 2020 · 8 comments · Fixed by #19281
Labels
component: autocomplete This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request

Comments

@Bebersohl
Copy link
Contributor

  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

The text of the input is selected on first focus of input. Add a new prop to useAutocomplete to opt out of this behavior

Examples 🌈

add selectTextOnFirstFocus prop that defaults to true

const handleClick = () => {
    if (
      firstFocus.current &&
      inputRef.current.selectionEnd - inputRef.current.selectionStart === 0
    ) {
      inputRef.current.focus();
      selectTextOnFirstFocus && inputRef.current.select();
    }

    firstFocus.current = false;
  };

Motivation 🔦

To make useAutocomplete more flexible.

@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Jan 8, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 11, 2020

What's the motivation for this behavior?

@oliviertassinari oliviertassinari added status: waiting for author Issue with insufficient information and removed status: waiting for author Issue with insufficient information labels Jan 11, 2020
@Bebersohl
Copy link
Contributor Author

So that you can use useAutoComplete without the input text being selected in the initial focus.

Here is a work around for anyone else that wants to opt out of the select behavior:

  const {
    ref,
    ...inputProps
  } = getInputProps() as any;

  // prevent text being selected on first input focus
  if (ref && ref.current) {
    ref.current.select = () => undefined;
  }

  return (
      <div {...getRootProps()}>
        <input
          ref={ref}
          {...inputProps}
        />
    </div>

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 11, 2020

I had a closer look at the issue. I think that you are raising an interesting concern. Say that you want to implement a search bar with suggestions (I have this very use case). Then, you will benchmark that not selecting thei input text on first focus is a common approach. For instance, Google search won't select while Chrome URL box will.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 11, 2020

@Bebersohl What do you think of a new selectOnFocus prop, null by default (so people can force a value) and with a fallback?

selectOnFocus = !freeSolo;

@oliviertassinari oliviertassinari added good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request labels Jan 11, 2020
@Bebersohl
Copy link
Contributor Author

@oliviertassinari Sounds good to me.

@oliviertassinari
Copy link
Member

Great, do you want to submit a pull request? :)

@Bebersohl
Copy link
Contributor Author

Sure

@ManavJethani

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants