AsyncChoicesInterface

Source
import type { AsyncChoicesInterface } from "@prestojs/viewmodel";

Interface for asynchronous choices.

A choice is a value that identifies the item (eg. an id) and a label that describes the item and is shown to users (eg. the name).

When multiple is true multiple values can be selected.

To define async choices two things are required:

1) A function to resolve existing value(s). This is used when viewing existing value(s) and need label(s) to show (eg. when displaying a choice on a detail view or rendering the value selected on a select widget). 2) A function to list & filter the available choices. This is used when selecting a value (eg. the options shown in a select widget).

Both of these functions may need to store state (eg. pagination for a listing) or access things from context (eg. read values from a cache). This can be done via two hooks - useListProps and useRetrieveProps. This functions should be called from a component or hook that deals with async choices when calling list and retrieve respectively. The return value from the hook is passed to the corresponding function.

For a default implementation see AsyncChoices

API

Methods

Generate the list of choices. This can return an array of single choices or grouped choices.

A grouped choice is a 2 element Array with a label and a list of choices.

ParameterTypeDescription
*itemsItemType[]

One of the following:

Choice[]

OR

[string, Choice[]][]

Either an array of single choices or grouped choices.

Get a label for an item

ParameterTypeDescription
*itemItemType
React.ReactNode

Return label to use when an item can't be found. This can be used by widgets to control what is rendered when an item for a value cannot be found (eg. when it's deleted or when it's loading). The exact details of how this is used depend on the widget.

ParameterTypeDescription
*valueValueType
React.ReactNode

Get the value to use for an item. The value should be unique and is what's used when a choice is selected (eg. it's the value that would be saved to a database).

ParameterTypeDescription
*itemItemType
ValueType

Function to resolve a list of choices based on the provided params.

What this function is passed depends on the implementation but when used with useAsyncChoices it will be passed query (the query object eg. to filter results with), paginator (the current paginator if any) and listOptions (any additional options passed on the listOptions prop to useAsyncChoices). Official presto widgets all use useAsyncChoices and so will use these parameters.

ParameterTypeDescription
*paramsRecord
Promise<ItemType[]>

Function to resolve specific values. This is used to know how to render the label for a value(s).

The first parameter is the value to retrieve (will be an array when multiple is true).

deps is the value returned by useRetrieveProps.

ParameterTypeDescription
*valueValueType[]|ValueType
depsany
Promise<>

Hook that returns any extra props to pass through to list in components/hooks that consume this (eg. useAsyncChoices). This is useful to store state for things like pagination.

What this function is passed depends on the implementation but when used with useAsyncChoices it will be query (the query object eg. to filter results with) and listOptions (any additional options passed on the listOptions prop to useAsyncChoices). Official presto widgets all use useAsyncChoices and so will use these parameters.

ParameterTypeDescription
*argsany
any

Resolve the specific instance of an item to use. By default this should just return item but can be used to resolve a specific instance of a class from a cache for example.

ParameterTypeDescription
*itemItemType|ItemType[]|null
ItemType|ItemType[]|null

Hook that returns props to pass through to retrieve in components/hooks that consume this (eg. useAsyncChoices. This is useful for things like hooking into an existing cache (eg. useViewModelCache). The value returned here is passed as the second parameter to retrieve. In addition the existingValues key is passed through to useAsyncValue as the list of items it can resolve existing values from.

What this function is passed depends on the implementation but when used with useAsyncChoices it will be passed id if there's a current value and it's not an array, ids if there's a current value and it is an array, existingValues which is the values returned by list (may be null if list not yet called) and retrieveOptions (any additional options passed on the retrieveOptions prop to useAsyncChoices). Official presto widgets all use useAsyncChoices and so will use these parameters.

ParameterTypeDescription
*argsany
any

Properties

If true then multiple values can be selected. When this is true retrieve() will be passed and return an array rather than a single value.