Add more complex matching support for autocomplete

The desired functionality is to be able to supply autocomplete options that not only match literally against the display value of that option, but support matching against other hidden rules, to enable domain specific predictive capabilities for the user that.

E.g. with a pack with a parameter for a currency, this new functionality would enable that:

  • when the user types “us”, it matches (at least) the “USD” autocomplete option, and
  • when the user types “usa”, it matches (at least) the “USD” autocomplete option, and
  • when the user types “united sta”, it matches (at least) the “USD” autocomplete option

This could be implemented with a prop matches in each autocomplete option, which would be a list of strings that can provide a match for the user input, or even better would be a string supporting a regex pattern, or even better could be either string[] | string.

1 Like

Hi @loucadufault - That’s a great idea for increasing the usability of autocomplete. You can accomplish today by using a dynamic autocomplete function. Here’s an example:

const CurrencyCodes = {
  "USD": ["United States", "USA"],
  "CAD": ["Canada", "Loonie"],
  "GBP": ["Britian", "Pound", "Quid"],
};

pack.addFormula({
  name: "ToUSD",
  description: "Convert from a different currency to US dollars.",
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "currency",
      description: "The currency to convert from.",
      autocomplete: async function (context, search, parameters) {
        search = search.toLowerCase();
        return Object.keys(CurrencyCodes)
          .filter(code => {
            let labels = CurrencyCodes[code];
            return !search 
              || code.toLowerCase().startsWith(search) 
              || labels.some(label => label.toLowerCase().startsWith(search));
          })
      }
    }),
    // ...
  ],
  // ...
});

Screen Shot 2022-10-03 at 2.20.35 PM

With a dynamic autocomplete function you can use any logic you like to generate the suggestions, even calling out to an external API. If this becomes a common pattern we could consider adding a simpler interface like you suggested, but I think this likely serves the needs for now.

1 Like

Exactly what I was looking for!