Skip to content

Commit cd81594

Browse files
committed
Apply review suggestions
1 parent cd434a1 commit cd81594

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

packages/ra-core/src/controller/input/ReferenceInputBase.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export const ReferenceInputBase = (props: ReferenceInputBaseProps) => {
8383

8484
const shouldRenderOffline =
8585
isPaused &&
86-
allChoices == null &&
86+
// TODO v6: we can't rely on isPlaceHolderData here because useReferenceInputController always return at least an empty array
87+
allChoices?.length === 0 &&
8788
offline !== false &&
8889
offline !== undefined;
8990

packages/ra-core/src/controller/input/useReferenceInputController.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
197197
() =>
198198
({
199199
sort: currentSort,
200-
allChoices: finalData,
201-
availableChoices: possibleValuesData,
200+
// TODO v6: we shouldn't return a default empty array. This is actually enforced at the type level in other hooks such as useListController
201+
allChoices: finalData ?? [],
202+
// TODO v6: same as above
203+
availableChoices: possibleValuesData ?? [],
204+
// TODO v6: same as above
202205
selectedChoices: referenceRecord ? [referenceRecord] : [],
203206
displayedFilters: params.displayedFilters,
204207
error: errorReference || errorPossibleValues,

packages/ra-core/src/form/choices/useChoicesContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ export const useChoicesContext = <ChoicesType extends RaRecord = RaRecord>(
4141
hasPreviousPage:
4242
options.hasPreviousPage ?? list.hasPreviousPage,
4343
hideFilter: options.hideFilter ?? list.hideFilter,
44+
isFetching: list.isFetching ?? false, // same
4445
isLoading: list.isLoading ?? false, // we must take the one for useList, otherwise the loading state isn't synchronized with the data
46+
isPaused: list.isPaused ?? false, // same
4547
isPending: list.isPending ?? false, // same
46-
isFetching: list.isFetching ?? false, // same
48+
isPlaceholderData: list.isPlaceholderData ?? false, // same
4749
page: options.page ?? list.page,
4850
perPage: options.perPage ?? list.perPage,
4951
refetch: options.refetch ?? list.refetch,

packages/ra-ui-materialui/src/input/AutocompleteInput.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,23 @@ If you provided a React element for the optionText prop, you must also provide t
626626
const renderHelperText = !!fetchError || helperText !== false || invalid;
627627

628628
const handleInputRef = useForkRef(field.ref, TextFieldProps?.inputRef);
629+
// When there is no network connectivity and we weren't even able to load the current value
630+
if (
631+
isPaused &&
632+
offline !== false &&
633+
offline !== undefined &&
634+
((field.value != null && selectedChoice == null) ||
635+
suggestions.length === 0)
636+
) {
637+
return offline;
638+
}
639+
640+
const finalLoadingText =
641+
typeof loadingText === 'string'
642+
? translate(loadingText, {
643+
_: loadingText,
644+
})
645+
: loadingText;
629646
return (
630647
<>
631648
<StyledAutocomplete
@@ -634,12 +651,10 @@ If you provided a React element for the optionText prop, you must also provide t
634651
closeText={translate(closeText, { _: closeText })}
635652
loadingText={
636653
isPaused && isPlaceholderData
637-
? offline
638-
: typeof loadingText === 'string'
639-
? translate(loadingText, {
640-
_: loadingText,
641-
})
642-
: loadingText
654+
? offline !== false && offline !== undefined
655+
? offline
656+
: finalLoadingText
657+
: finalLoadingText
643658
}
644659
openOnFocus
645660
openText={translate(openText, { _: openText })}

packages/ra-ui-materialui/src/input/ReferenceInput.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import { ReferenceInputBase, ReferenceInputBaseProps } from 'ra-core';
33

44
import { AutocompleteInput } from './AutocompleteInput';
5-
import { Offline } from '../Offline';
65

76
/**
87
* An Input component for choosing a reference record. Useful for foreign keys.
@@ -65,27 +64,18 @@ import { Offline } from '../Offline';
6564
* a `setFilters` function. You can call this function to filter the results.
6665
*/
6766
export const ReferenceInput = (props: ReferenceInputProps) => {
68-
const {
69-
children = defaultChildren,
70-
offline = defaultOffline,
71-
...rest
72-
} = props;
67+
const { children = defaultChildren, ...rest } = props;
7368

7469
if (props.validate && process.env.NODE_ENV !== 'production') {
7570
throw new Error(
7671
'<ReferenceInput> does not accept a validate prop. Set the validate prop on the child instead.'
7772
);
7873
}
7974

80-
return (
81-
<ReferenceInputBase {...rest} offline={offline}>
82-
{children}
83-
</ReferenceInputBase>
84-
);
75+
return <ReferenceInputBase {...rest}>{children}</ReferenceInputBase>;
8576
};
8677

8778
const defaultChildren = <AutocompleteInput />;
88-
const defaultOffline = <Offline variant="inline" />;
8979

9080
export interface ReferenceInputProps extends ReferenceInputBaseProps {
9181
/**

0 commit comments

Comments
 (0)