prompts icon indicating copy to clipboard operation
prompts copied to clipboard

Better index selection when providing initial for autocomplete

Open xav-ie opened this issue 1 year ago • 0 comments

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 :sob:
  4. I type in 'base', 'baseball' now selected!! :sob:
  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 avatar Feb 29 '24 18:02 xav-ie