svelecte icon indicating copy to clipboard operation
svelecte copied to clipboard

async support for createTransform

Open shubhendumadhukar opened this issue 7 months ago • 1 comments

I am trying to use Svelecte with creatable=true.

In my use case:

  • When a user types an option not present in the available options, I would like them to be able to create a new option.
  • My createTransform function is as follows:
const createTransform = async (/** @type {string} */ inputValue, /** @type {string} */ creatablePrefix, /** @type {string} */ valueField, /** @type {string} */ labelField) => {
  console.log({ inputValue, creatablePrefix, valueField, labelField });
  const record = await createTag(inputValue);
  return {
    id: record.id,
    [valueField]: record.name,
    [labelField]: creatablePrefix + record.name,
  };
};

This however does not work, understandably, because createTransform probably doesn't support promises. I can make it work by modifying the function as follows:

const createTransform = (/** @type {string} */ inputValue, /** @type {string} */ creatablePrefix, /** @type {string} */ valueField, /** @type {string} */ labelField) => {
  console.log({ inputValue, creatablePrefix, valueField, labelField });
  createTag(inputValue);
  return {
    [valueField]: inputValue,
    [labelField]: creatablePrefix + inputValue,
  };
};

I would however like my createTransform function to return the id value of record created in DB. Is there a way to make createTransform support promises/async functions?

shubhendumadhukar avatar Nov 06 '23 13:11 shubhendumadhukar