feedback fixes / clean up

This commit is contained in:
Julie Plantey 2022-09-06 17:19:04 +02:00
parent 6a411fbc5e
commit 1aa4f9c75f
3 changed files with 27 additions and 18 deletions

View File

@ -11,7 +11,6 @@ import { Icon } from '@strapi/design-system/Icon';
import { FieldLabel, FieldError, FieldHint, Field } from '@strapi/design-system/Field'; import { FieldLabel, FieldError, FieldHint, Field } from '@strapi/design-system/Field';
import { TextButton } from '@strapi/design-system/TextButton'; import { TextButton } from '@strapi/design-system/TextButton';
import { Typography } from '@strapi/design-system/Typography'; import { Typography } from '@strapi/design-system/Typography';
// import { Loader } from '@strapi/design-system/Loader';
import Cross from '@strapi/icons/Cross'; import Cross from '@strapi/icons/Cross';
import Refresh from '@strapi/icons/Refresh'; import Refresh from '@strapi/icons/Refresh';
@ -21,6 +20,7 @@ import { Relation } from './components/Relation';
import { RelationItem } from './components/RelationItem'; import { RelationItem } from './components/RelationItem';
import { RelationList } from './components/RelationList'; import { RelationList } from './components/RelationList';
import { Option } from './components/Option'; import { Option } from './components/Option';
import { RELATION_ITEM_HEIGHT } from './constants';
const LinkEllipsis = styled(Link)` const LinkEllipsis = styled(Link)`
white-space: nowrap; white-space: nowrap;
@ -41,6 +41,7 @@ const rotation = keyframes`
// Temporary component - to replace with loading prop on TextButton after DS release // Temporary component - to replace with loading prop on TextButton after DS release
const LoaderWrapper = styled(Box)` const LoaderWrapper = styled(Box)`
animation: ${rotation} 2s infinite linear; animation: ${rotation} 2s infinite linear;
will-change: transform;
`; `;
const RelationInput = ({ const RelationInput = ({
@ -49,6 +50,7 @@ const RelationInput = ({
error, error,
id, id,
name, name,
numberOfRelationsToDisplay,
label, label,
labelLoadMore, labelLoadMore,
loadingMessage, loadingMessage,
@ -65,18 +67,20 @@ const RelationInput = ({
searchResults, searchResults,
}) => { }) => {
const listRef = useRef(); const listRef = useRef();
const [overflow, setOverflow] = useState('bottom'); const [overflow, setOverflow] = useState('');
// To clean (number relations + numberOfRelationsToDisplay as prop?) const flattenRelations = relations.data?.pages.flat();
const totalNumberOfRelations = relations.data?.pages[0]?.length; const totalNumberOfRelations = flattenRelations?.length || 0;
const relationItemHeight = 58;
const numberOfRelationsToDisplay = 5;
const dynamicListHeight = const dynamicListHeight =
totalNumberOfRelations > numberOfRelationsToDisplay totalNumberOfRelations > numberOfRelationsToDisplay
? Math.min(totalNumberOfRelations, numberOfRelationsToDisplay) * relationItemHeight + ? Math.min(totalNumberOfRelations, numberOfRelationsToDisplay) * RELATION_ITEM_HEIGHT +
relationItemHeight / 2 RELATION_ITEM_HEIGHT / 2
: Math.min(totalNumberOfRelations, numberOfRelationsToDisplay) * relationItemHeight; : Math.min(totalNumberOfRelations, numberOfRelationsToDisplay) * RELATION_ITEM_HEIGHT;
// TODO: improve load more conditions
const nextPage = (!relations.hasNextPage() && relations.isLoading) || relations.hasNextPage();
const isLoadMoreButton = labelLoadMore && !disabled && nextPage;
const handleOverflow = ({ const handleOverflow = ({
overscanStartIndex, overscanStartIndex,
@ -84,7 +88,9 @@ const RelationInput = ({
visibleStartIndex, visibleStartIndex,
visibleStopIndex, visibleStopIndex,
}) => { }) => {
// To fix: overflow will work only when item is not visible (index change) if (totalNumberOfRelations <= numberOfRelationsToDisplay) return;
// TODO: needs a fix, overflow will work only when item is not visible (index change)
// normally overflow should start after we started scrolling even if item is still visible // normally overflow should start after we started scrolling even if item is still visible
// + with 6 items onItemsRendered doesn't fire because it fires only when first or last item can leave visibility space // + with 6 items onItemsRendered doesn't fire because it fires only when first or last item can leave visibility space
const overflowTop = overscanStartIndex !== visibleStartIndex; const overflowTop = overscanStartIndex !== visibleStartIndex;
@ -94,8 +100,8 @@ const RelationInput = ({
setOverflow('top-bottom'); setOverflow('top-bottom');
} else if (overflowBottom && !overflowTop) { } else if (overflowBottom && !overflowTop) {
setOverflow('bottom'); setOverflow('bottom');
} else { } else if (!overflowBottom && overflowTop) {
setOverflow('bottom'); setOverflow('top');
} }
}; };
@ -130,14 +136,13 @@ const RelationInput = ({
</> </>
} }
loadMore={ loadMore={
!disabled && isLoadMoreButton && (
labelLoadMore && (
<TextButton <TextButton
disabled={relations.isLoading} disabled={relations.isLoading}
onClick={() => onRelationLoadMore()} onClick={() => onRelationLoadMore()}
startIcon={ startIcon={
relations.isLoading ? ( relations.isLoading ? (
// To replace with loading prop on TextButton after DS release // TODO: To replace with loading prop on TextButton after DS release
<LoaderWrapper> <LoaderWrapper>
<Loader /> <Loader />
</LoaderWrapper> </LoaderWrapper>
@ -151,13 +156,13 @@ const RelationInput = ({
) )
} }
> >
<RelationList overflow={totalNumberOfRelations > numberOfRelationsToDisplay && overflow}> <RelationList overflow={overflow}>
<List <List
height={dynamicListHeight} height={dynamicListHeight}
ref={listRef} ref={listRef}
itemCount={totalNumberOfRelations} itemCount={totalNumberOfRelations}
itemSize={relationItemHeight} itemSize={RELATION_ITEM_HEIGHT}
itemData={relations.data?.pages[0]} itemData={flattenRelations}
onItemsRendered={handleOverflow} onItemsRendered={handleOverflow}
> >
{({ data, index, style }) => { {({ data, index, style }) => {
@ -230,6 +235,7 @@ const ReactQueryRelationResult = PropTypes.shape({
) )
), ),
}), }),
hasNextPage: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired,
isSuccess: PropTypes.bool.isRequired, isSuccess: PropTypes.bool.isRequired,
}); });
@ -268,6 +274,7 @@ RelationInput.propTypes = {
labelLoadMore: PropTypes.string, labelLoadMore: PropTypes.string,
loadingMessage: PropTypes.string.isRequired, loadingMessage: PropTypes.string.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
numberOfRelationsToDisplay: PropTypes.number.isRequired,
onRelationAdd: PropTypes.func.isRequired, onRelationAdd: PropTypes.func.isRequired,
onRelationRemove: PropTypes.func.isRequired, onRelationRemove: PropTypes.func.isRequired,
onRelationLoadMore: PropTypes.func.isRequired, onRelationLoadMore: PropTypes.func.isRequired,

View File

@ -0,0 +1 @@
export const RELATION_ITEM_HEIGHT = 58;

View File

@ -123,6 +123,7 @@ export const RelationInputWrapper = ({
defaultMessage: 'Relations are loading', defaultMessage: 'Relations are loading',
})} })}
name={name} name={name}
numberOfRelationsToDisplay={5}
onRelationAdd={(relation) => handleRelationAdd(relation)} onRelationAdd={(relation) => handleRelationAdd(relation)}
onRelationRemove={(relation) => handleRelationRemove(relation)} onRelationRemove={(relation) => handleRelationRemove(relation)}
onRelationLoadMore={() => handleRelationLoadMore()} onRelationLoadMore={() => handleRelationLoadMore()}