Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ReferenceInput to let its child handle loading #5767

Merged
merged 5 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ describe('<ReferenceInputController />', () => {
choices: [{ id: 1 }],
error: null,
filter: { q: '' },
loading: false,
loaded: false,
loading: true,
pagination: { page: 1, perPage: 25 },
sort: { field: 'title', order: 'ASC' },
warning: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ export const useReferenceInputController = (
// kept for backwards compatibility
// @deprecated to be removed in 4.0
error: dataStatus.error,
loading: dataStatus.waiting,
loading: possibleValuesLoading || referenceLoading,
loaded: possibleValuesLoaded && referenceLoaded,
filter: filterValues,
setFilter,
pagination,
Expand Down Expand Up @@ -231,6 +232,7 @@ export interface ReferenceInputValue {
};
choices: Record[];
error?: string;
loaded: boolean;
loading: boolean;
pagination: PaginationPayload;
setFilter: (filter: string) => void;
Expand Down
17 changes: 17 additions & 0 deletions packages/ra-ui-materialui/src/input/ArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useFieldArray } from 'react-final-form-arrays';
import { InputLabel, FormControl } from '@material-ui/core';

import sanitizeInputRestProps from './sanitizeInputRestProps';
import Labeled from './Labeled';
import { LinearProgress } from '../layout';

/**
* To edit arrays of data embedded inside a record, <ArrayInput> creates a list of sub-forms.
Expand Down Expand Up @@ -52,6 +54,8 @@ const ArrayInput: FC<ArrayInputProps> = ({
className,
defaultValue,
label,
loaded,
loading,
children,
record,
resource,
Expand All @@ -71,6 +75,19 @@ const ArrayInput: FC<ArrayInputProps> = ({
...rest,
});

if (loading) {
return (
<Labeled
label={label}
source={source}
resource={resource}
className={className}
>
<LinearProgress />
</Labeled>
);
}

return (
<FormControl
fullWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,65 @@ describe('<AutocompleteArrayInput />', () => {
fireEvent.focus(input);
expect(queryAllByRole('option')).toHaveLength(1);
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import InputHelperText from './InputHelperText';
import AutocompleteSuggestionList from './AutocompleteSuggestionList';
import AutocompleteSuggestionItem from './AutocompleteSuggestionItem';
import { AutocompleteInputLoader } from './AutocompleteInputLoader';

interface Options {
suggestionsContainerProps?: any;
Expand Down Expand Up @@ -110,6 +111,8 @@ const AutocompleteArrayInput: FunctionComponent<
input: inputOverride,
isRequired: isRequiredOverride,
label,
loaded,
loading,
limitChoicesToValue,
margin = 'dense',
matchSuggestion,
Expand Down Expand Up @@ -410,6 +413,9 @@ const AutocompleteArrayInput: FunctionComponent<
))}
</div>
),
endAdornment: loading && (
<AutocompleteInputLoader />
),
onBlur,
onChange: event => {
handleFilterChange(event);
Expand Down
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,65 @@ describe('<AutocompleteInput />', () => {
expect(queryByDisplayValue('foo')).not.toBeNull();
});
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
15 changes: 14 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import InputHelperText from './InputHelperText';
import AutocompleteSuggestionList from './AutocompleteSuggestionList';
import AutocompleteSuggestionItem from './AutocompleteSuggestionItem';
import { AutocompleteInputLoader } from './AutocompleteInputLoader';

interface Options {
suggestionsContainerProps?: any;
Expand Down Expand Up @@ -112,6 +113,8 @@ const AutocompleteInput: FunctionComponent<AutocompleteInputProps> = props => {
isRequired: isRequiredOverride,
label,
limitChoicesToValue,
loaded,
loading,
margin = 'dense',
matchSuggestion,
meta: metaOverride,
Expand Down Expand Up @@ -355,7 +358,12 @@ const AutocompleteInput: FunctionComponent<AutocompleteInputProps> = props => {

const getEndAdornment = openMenu => {
if (!resettable) {
return endAdornment;
if (endAdornment) {
return endAdornment;
}
if (loading) {
return <AutocompleteInputLoader />;
}
} else if (!filterValue) {
const label = translate('ra.action.clear_input_value');
if (clearAlwaysVisible) {
Expand All @@ -376,6 +384,7 @@ const AutocompleteInput: FunctionComponent<AutocompleteInputProps> = props => {
)}
/>
</IconButton>
{loading && <AutocompleteInputLoader />}
</InputAdornment>
);
} else {
Expand All @@ -386,6 +395,7 @@ const AutocompleteInput: FunctionComponent<AutocompleteInputProps> = props => {
return (
<InputAdornment position="end">
<span className={classes.clearButton}>&nbsp;</span>
{loading && <AutocompleteInputLoader />}
</InputAdornment>
);
}
Expand All @@ -411,6 +421,7 @@ const AutocompleteInput: FunctionComponent<AutocompleteInputProps> = props => {
})}
/>
</IconButton>
{loading && <AutocompleteInputLoader />}
</InputAdornment>
);
}
Expand Down Expand Up @@ -586,6 +597,8 @@ export interface AutocompleteInputProps
Omit<DownshiftProps<any>, 'onChange'> {
clearAlwaysVisible?: boolean;
resettable?: boolean;
loaded?: boolean;
loading?: boolean;
}

export default AutocompleteInput;
13 changes: 13 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInputLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { CircularProgress } from '@material-ui/core';
import { useTimeout } from 'ra-core';

export const AutocompleteInputLoader = ({ timeout = 1000 }) => {
const oneSecondHasPassed = useTimeout(timeout);

if (oneSecondHasPassed) {
return <CircularProgress size={24} />;
}

return null;
};
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,65 @@ describe('<CheckboxGroupInput />', () => {
expect(error.classList.contains('Mui-error')).toEqual(true);
});
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
Loading