Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select: getOptions async and isOptionEqualToValue #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

None yet

1 participant