AsyncChoicesInterface
SourceInterface 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
getChoices(items)
SourceGenerate 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.
Parameter | Type | Description | |
---|---|---|---|
* | items | ItemType[] |
One of the following:
Choice[]OR
[string, Choice[]][]Either an array of single choices or grouped choices.
getLabel(item)
SourceGet a label for an item
Parameter | Type | Description | |
---|---|---|---|
* | item | ItemType |
getMissingLabel(value)
SourceReturn 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.
Parameter | Type | Description | |
---|---|---|---|
* | value | ValueType |
getValue(item)
SourceGet 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).
Parameter | Type | Description | |
---|---|---|---|
* | item | ItemType |
list(params)
SourceFunction 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.
Parameter | Type | Description | |
---|---|---|---|
* | params | Record |
retrieve(value,?deps)
SourceFunction 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
.
Parameter | Type | Description | |
---|---|---|---|
* | value | ValueType[]|ValueType | |
deps | any |
useListProps(args)
SourceHook 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.
Parameter | Type | Description | |
---|---|---|---|
* | args | any |
useResolveItems(item)
SourceResolve 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.
Parameter | Type | Description | |
---|---|---|---|
* | item | ItemType|ItemType[]|null |
useRetrieveProps(args)
SourceHook 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.
Parameter | Type | Description | |
---|---|---|---|
* | args | any |
Properties
multiple: boolean
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.