Skip to content

Better index selection when providing initial for autocomplete #413

@xav-ie

Description

@xav-ie

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions