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

Better index selection when providing initial for autocomplete #413

Open
xav-ie opened this issue Feb 29, 2024 · 0 comments
Open

Better index selection when providing initial for autocomplete #413

xav-ie opened this issue Feb 29, 2024 · 0 comments

Comments

@xav-ie
Copy link

xav-ie commented Feb 29, 2024

Is your feature request related to a problem?

If you provide an initial selection for the autocomplete prompt, and you try to type, the initial index is used when you are searching. In my case (and probably most others?), this is undesirable. I am already typing and just want the sorted list to select first option. I only want intial when this.select = ''. If it is not empty, then it should behave as if I did not provide an initial.

Here is a motivating example where I have implemented this functionality:

  let lastInput = '';

  const answers = await prompts([
    {
      type: 'autocomplete',
      name: 'things',
      choices: getChoices(),
      message: 'Select thing',
      initial: lastDeploy,
      // @ts-ignore: Improperly typed from lib
      onRender() {
        const prompt = (this as unknown) as Record<string, unknown>;
        if (prompt['input'] === '') {
          prompt['select'] = prompt['initial'];
        } else if (prompt['input'] !== lastInput) {
          prompt['select'] = 0;
        }
        lastInput = prompt['input'] as string;
      },
    },
  ]);

Describe the solution you'd like

My jerry-rigged in solution works well, but I would like to know if other users are desiring this feature and if I or someone could make a pr.

Describe alternatives you've considered

I can continue using my jerry-rig solution, for which my team will judge me.

Additional context

Example:

intial = 'xylophone'

things = ['apple', 'base', 'baseball', 'basketball', 'xylophone']
  1. inital prompt has xylophone selected
  2. I type 'b'
  3. prompt now has 'basketball' selected 😭
  4. I type in 'base', 'baseball' now selected!! 😭
  5. I must arrow up to get correct option

This issue is hard to notice, because if you have initial of 'apple' or 'base', it will behave as desired.

@xav-ie xav-ie changed the title Better index selection when providing initial Better index selection when providing initial for autocomplete Feb 29, 2024
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

1 participant