Skip to content

Commit bc02cc4

Browse files
committed
Correctly handle offline case in ReferenceInputBase
1 parent 72c665b commit bc02cc4

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from '../..';
2929

3030
export default {
31-
title: 'ra-core/input/ReferenceInputBase',
31+
title: 'ra-core/controller/input/ReferenceInputBase',
3232
excludeStories: ['dataProviderWithAuthors'],
3333
};
3434

@@ -526,7 +526,7 @@ export const Meta = ({
526526

527527
export const Offline = () => (
528528
<CoreAdminContext dataProvider={dataProvider} i18nProvider={i18nProvider}>
529-
<Form onSubmit={() => {}} defaultValues={{ tag_ids: [5] }}>
529+
<Form onSubmit={() => {}} defaultValues={{ tag_ids: 5 }}>
530530
<div
531531
style={{
532532
width: '200px',

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ export const ReferenceInputBase = (props: ReferenceInputBaseProps) => {
7979
filter,
8080
});
8181

82-
const { isPaused, allChoices } = controllerProps;
83-
82+
const { isPaused, isPending } = controllerProps;
83+
// isPending is true: there's no cached data and no query attempt was finished yet
84+
// isPaused is true: the query was paused (e.g. due to a network issue)
85+
// Both true: we're offline and have no data to show
8486
const shouldRenderOffline =
85-
isPaused &&
86-
// TODO v6: we can't rely on isPlaceHolderData here because useReferenceInputController always return at least an empty array
87-
allChoices?.length === 0 &&
88-
offline !== false &&
89-
offline !== undefined;
87+
isPaused && isPending && offline !== undefined && offline !== false;
9088

9189
return (
9290
<ResourceContextProvider value={reference}>

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
141141
isPendingPossibleValues;
142142

143143
const isPaused = isPausedReference || isPausedPossibleValues;
144-
const isPlaceholderData =
145-
isPlaceholderDataReference || isPlaceholderDataPossibleValues;
146144

147145
// We need to delay the update of the referenceRecord and the finalData
148146
// to the next React state update, because otherwise it can raise a warning
@@ -212,7 +210,9 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
212210
isLoading: isLoadingReference || isLoadingPossibleValues,
213211
isPaused: isPausedReference || isPausedPossibleValues,
214212
isPending,
215-
isPlaceholderData,
213+
isPlaceholderData:
214+
isPlaceholderDataReference ||
215+
isPlaceholderDataPossibleValues,
216216
page: params.page,
217217
perPage: params.perPage,
218218
refetch,
@@ -248,7 +248,8 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
248248
isPausedPossibleValues,
249249
isPausedReference,
250250
isPending,
251-
isPlaceholderData,
251+
isPlaceholderDataReference,
252+
isPlaceholderDataPossibleValues,
252253
pageInfo,
253254
params.displayedFilters,
254255
params.filter,

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,11 @@ 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-
) {
629+
// isPending is true: there's no cached data and no query attempt was finished yet
630+
// isPaused is true: the query was paused (e.g. due to a network issue)
631+
// Both true: we're offline, have no data to show
632+
// If the component that provides the ChoicesContext does not handle this case, we should should render the offline element
633+
if (isPending && isPaused && offline !== false && offline !== undefined) {
637634
return offline;
638635
}
639636

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { ReferenceInputBase, ReferenceInputBaseProps } from 'ra-core';
3-
3+
import { Offline } from '../Offline';
44
import { AutocompleteInput } from './AutocompleteInput';
55

66
/**
@@ -64,19 +64,27 @@ import { AutocompleteInput } from './AutocompleteInput';
6464
* a `setFilters` function. You can call this function to filter the results.
6565
*/
6666
export const ReferenceInput = (props: ReferenceInputProps) => {
67-
const { children = defaultChildren, ...rest } = props;
67+
const {
68+
children = defaultChildren,
69+
offline = defaultOffline,
70+
...rest
71+
} = props;
6872

6973
if (props.validate && process.env.NODE_ENV !== 'production') {
7074
throw new Error(
7175
'<ReferenceInput> does not accept a validate prop. Set the validate prop on the child instead.'
7276
);
7377
}
7478

75-
return <ReferenceInputBase {...rest}>{children}</ReferenceInputBase>;
79+
return (
80+
<ReferenceInputBase {...rest} offline={offline}>
81+
{children}
82+
</ReferenceInputBase>
83+
);
7684
};
7785

7886
const defaultChildren = <AutocompleteInput />;
79-
87+
const defaultOffline = <Offline variant="inline" />;
8088
export interface ReferenceInputProps extends ReferenceInputBaseProps {
8189
/**
8290
* Call validate on the child component instead

0 commit comments

Comments
 (0)