thisbeyond / solid-select

The Select component for Solid.
https://solid-select.com
MIT License
172 stars 18 forks source link

Add ability to customise formatting in `createOptions` #58

Closed martinpengellyphillips closed 2 months ago

martinpengellyphillips commented 2 months ago

If you want to customise the display label whilst using createOptions you have to copy the whole function and alter the code. Currently you can only pass a key to index into an option. It would be more useful to pass in a function to return the label to use (effectively every call to getLabel in createOptions).

Example of how it would look:

const props = createOptions(
    [
      { name: "apple" },
      { name: "banana" },
      { name: "pear" },
      { name: "pineapple" },
      { name: "kiwi" },
      "Non-conforming option"
    ],
    { labeler: item => "name" in item ? item["name"] : item }
  );
  return <Select {...props} />;
martinpengellyphillips commented 2 months ago

Could this also address https://github.com/thisbeyond/solid-select/pull/40 in that an additional context parameter can be passed to allow implementation to determine if it is a create label or not?

    { labeler: (value: any, mode: 'default' | 'create') => mode === 'create' ? <>Make <mark>{value}</mark></> : value }
martinpengellyphillips commented 2 months ago

Example of solving #40 with the committed approach:

createOptions(..., {format: (value, type, meta) => <>{meta.prefix && "Créer "}{value}</>}
martinpengellyphillips commented 2 months ago

Release 0.15.0 has now been published which should address this.