Skip to content

Conversation

danlobo
Copy link
Contributor

@danlobo danlobo commented Dec 1, 2022

What was done

  • The options property, in Select control, now accepts Promises
  • Control and Select has a new property, isOptionEqualToValue

Why it was done

  • Async methods allows us to pass options to Select from, for example, a fetch call. (callbacks can do it too, but I feel async are cooler)

Example:

controls: [
      Controls.select({
        name: "myCustomers",
        label: "Customers",
        placeholder: '(Select one customer)',
        getOptions: async (inputData, executionContext) => {
          const res = await fetch('/api/customer')
          const json = await res.json()
          
          return (json.result.map(item => ({
            value: item.id,
            label: item.name
          })))
        }
      })
    ]

That alone would enable us more options to obtain.. er.. options.

But, would be more interesting to pass not the id as value, the object itself would be better since we wouldn't need to fetch it again later.

So, the property isOptionEqualToValue will give us the answer to 'how an option object is equal to a potential string (or numeric) value?'

Same example, objects as values instead of ids:

controls: [
      Controls.select({
        name: "myCustomers",
        label: "Customers",
        placeholder: '(Select one customer)',
        getOptions: async (inputData, executionContext) => {
          const res = await fetch('/api/customer')
          const json = await res.json()
          
          return (json.result.map(item => ({
            value: item,
            label: item.name
          })))
        },
        isOptionEqualToValue: (option, value) => option.id === value.id
      })
    ]

That way, at the node inputs resolve phase, we have the selected object to handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant