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

Prevent clearing value on click outside #23

Open
clementmas opened this issue Sep 14, 2022 · 5 comments
Open

Prevent clearing value on click outside #23

clementmas opened this issue Sep 14, 2022 · 5 comments

Comments

@clementmas
Copy link

Is it possible to keep the input value when clicking outside the input instead of automatically clearing it?

From what I understand, clicking outside the input closes the Select and always triggers clearInputValue().

Thanks!

@martinpengellyphillips
Copy link
Contributor

Curious what the use case for this would be?

@clementmas
Copy link
Author

It's for a city search autocomplete similar to Photon, Geoapify or even Google Maps. Clicking away does not clear the value.

I'm using createSelect combined with async options. It works great but I'd rather clear the value manually when an option has been selected.

@martinpengellyphillips
Copy link
Contributor

Interesting. I'm not sure preventing clearing the input would be enough for your use case - currently the logic might hide the input entirely as well. This is because a displayed value can be more complex (elements) than the editable text input.

I wonder though; would setting the value on click away solve the same problem. Essentially track the input value (onInput) and then set the value (e.g. initialValue) to that onBlur.

Might be confusing though that on refocus the text isn't editable / positionable. You'd likely need to have a custom Select component to improve that.

@clementmas
Copy link
Author

Hi Martin, thanks for your feedback. I'm already using a custom <Select /> component with createSelect.

TravelMap-admin-map-search-autocomplete

I do want to close the result list when clicking away but not clearing the value. At the moment, I don't see any clean way to do it.

import { createSignal, createResource } from 'solid-js';
import { createSelect } from '@thisbeyond/solid-select';
import { debounce } from '@solid-primitives/scheduled';

export default function Select(props) {
  // Copied from solid-select `createAsyncOptions` but adding debounce
  const [inputValue, setInputValue] = createSignal();
  const [asyncOptions] = createResource(inputValue, props.onSearch, {
    initialValue: [],
  });

  const select = createSelect({
    get options() {
      return asyncOptions();
    },
    onInput: debounce(setInputValue, 400),
    onChange: props.onChange,
  });

  return (
    <div ref={select.containerRef}>
      {/* Input */}
      <input
        ref={select.inputRef}
        disabled={select.disabled}
      />

      {/* Result list */}
      <Show when={select.isOpen && !asyncOptions.loading}>
        <div ref={select.listRef}>
          <For each={select.options}>{/*  */}</For>
        </div>
      </Show>
    </div>
  );
}

I might just leave it like that. The rest is working well.

@clementmas
Copy link
Author

Hi Martin,

The recent update makes it easier to use createSelect with a custom <Select /> component.

However the value is still being cleared whenever the list is closed:

setInputValue("");

Can it be replaced by setIsActive(false) and move setInputValue('') to the built-in <Select /> component?

I can understand if that's not a priority/concern. If so, I'll find another solution. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants