Description
I am writing a AutoComplete input that stores values from users
table login
field. The code is as follows. Everything works just fine if perPage
is large enough to include all records from users table. However, if perPage
is small, then I am missing the current value in the drop-down box, and value appears to be empty
<ReferenceInput
source='owner' // reference owner field in the current table; it contains users.login value
reference='users' // use users table as reference
page={1}
perPage={5}
queryOptions={{ refetchOnWindowFocus: false, retry: false }}
>
<AutocompleteInput
optionValue='login' // use users.login instead of users.id
/>
</ReferenceInput>
Root-cause:
Reference input performs 2 queries from useReferenceInputController
useGetList
to fetch possible valuesuseReference
to fetch current values
The issue with useReference
query is that it expects value to include id
. In my example above, I am using users.login
attribute instead of users.id
as optionValue; as the result, the only records that are received are the records returned by useGetList
and, due to pagination settings, the current value may be missing from available choices and will not be displayed.
Proposed fix
- Add
optionValue
attribute toReferenceInput
which defaults toid
- If
optionValue === 'id'
, continue usinguseReference
as displayed below - If
optionValue !== 'id'
, fetch current selection usinguseGetList
withfilter: { [optionValue]: currentValue }
// fetch current value
const {
referenceRecord: currentReferenceRecord,
refetch: refetchReference,
error: errorReference,
isLoading: isLoadingReference,
isFetching: isFetchingReference,
isPending: isPendingReference,
} = useReference<RecordType>({
id: currentValue,
reference,
// @ts-ignore the types of the queryOptions for the getMAny and getList are not compatible
options: {
enabled: currentValue != null && currentValue !== '',
meta,
...otherQueryOptions,
},
});
Another issue is that sometimes values stored in current table's owner
attribute are not found in users.login
table (due to data retention issues). When that happens, the drop-down box will be empty.
If a referenced value is still missing from allChoices
and availableChoices
, can we add { [optionValue]: currentValue }
to these variables, so we can display invalid values as selected?