fix: Improvements in table component (#2450)
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
import { isEqual } from "lodash";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { CollapsedIcon } from "outline-icons";
|
import { CollapsedIcon } from "outline-icons";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
@ -24,6 +25,7 @@ export type Props = {|
|
|||||||
onChangePage: (index: number) => void,
|
onChangePage: (index: number) => void,
|
||||||
onChangeSort: (sort: ?string, direction: "ASC" | "DESC") => void,
|
onChangeSort: (sort: ?string, direction: "ASC" | "DESC") => void,
|
||||||
columns: any,
|
columns: any,
|
||||||
|
defaultSortDirection: "ASC" | "DESC",
|
||||||
|};
|
|};
|
||||||
|
|
||||||
function Table({
|
function Table({
|
||||||
@ -39,6 +41,7 @@ function Table({
|
|||||||
topRef,
|
topRef,
|
||||||
onChangeSort,
|
onChangeSort,
|
||||||
onChangePage,
|
onChangePage,
|
||||||
|
defaultSortDirection,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const {
|
const {
|
||||||
@ -62,32 +65,52 @@ function Table({
|
|||||||
autoResetPage: false,
|
autoResetPage: false,
|
||||||
pageCount: totalPages,
|
pageCount: totalPages,
|
||||||
initialState: {
|
initialState: {
|
||||||
sortBy: [{ id: defaultSort, desc: false }],
|
sortBy: [
|
||||||
|
{
|
||||||
|
id: defaultSort,
|
||||||
|
desc: defaultSortDirection === "DESC" ? true : false,
|
||||||
|
},
|
||||||
|
],
|
||||||
pageSize,
|
pageSize,
|
||||||
pageIndex: page,
|
pageIndex: page,
|
||||||
},
|
},
|
||||||
|
stateReducer: (newState, action, prevState) => {
|
||||||
|
if (!isEqual(newState.sortBy, prevState.sortBy)) {
|
||||||
|
return { ...newState, pageIndex: 0 };
|
||||||
|
}
|
||||||
|
return newState;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
useSortBy,
|
useSortBy,
|
||||||
usePagination
|
usePagination
|
||||||
);
|
);
|
||||||
|
|
||||||
React.useEffect(() => {
|
const prevSortBy = React.useRef(sortBy);
|
||||||
onChangePage(pageIndex);
|
|
||||||
}, [pageIndex]);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
if (!isEqual(sortBy, prevSortBy.current)) {
|
||||||
|
prevSortBy.current = sortBy;
|
||||||
onChangePage(0);
|
onChangePage(0);
|
||||||
onChangeSort(
|
onChangeSort(
|
||||||
sortBy.length ? sortBy[0].id : undefined,
|
sortBy.length ? sortBy[0].id : undefined,
|
||||||
sortBy.length && sortBy[0].desc ? "DESC" : "ASC"
|
!sortBy.length ? defaultSortDirection : sortBy[0].desc ? "DESC" : "ASC"
|
||||||
);
|
);
|
||||||
}, [sortBy]);
|
}
|
||||||
|
}, [defaultSortDirection, onChangePage, onChangeSort, sortBy]);
|
||||||
|
|
||||||
|
const handleNextPage = () => {
|
||||||
|
nextPage();
|
||||||
|
onChangePage(pageIndex + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePreviousPage = () => {
|
||||||
|
previousPage();
|
||||||
|
onChangePage(pageIndex - 1);
|
||||||
|
};
|
||||||
|
|
||||||
const isEmpty = !isLoading && data.length === 0;
|
const isEmpty = !isLoading && data.length === 0;
|
||||||
const showPlaceholder = isLoading && data.length === 0;
|
const showPlaceholder = isLoading && data.length === 0;
|
||||||
|
|
||||||
console.log({ canNextPage, pageIndex, totalPages, rows, data });
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Anchor ref={topRef} />
|
<Anchor ref={topRef} />
|
||||||
@ -142,12 +165,12 @@ function Table({
|
|||||||
>
|
>
|
||||||
{/* Note: the page > 0 check shouldn't be needed here but is */}
|
{/* Note: the page > 0 check shouldn't be needed here but is */}
|
||||||
{canPreviousPage && page > 0 && (
|
{canPreviousPage && page > 0 && (
|
||||||
<Button onClick={previousPage} neutral>
|
<Button onClick={handlePreviousPage} neutral>
|
||||||
{t("Previous page")}
|
{t("Previous page")}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{canNextPage && (
|
{canNextPage && (
|
||||||
<Button onClick={nextPage} neutral>
|
<Button onClick={handleNextPage} neutral>
|
||||||
{t("Next page")}
|
{t("Next page")}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
@ -209,7 +232,7 @@ const SortWrapper = styled(Flex)`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const Cell = styled.td`
|
const Cell = styled.td`
|
||||||
padding: 8px 0;
|
padding: 6px;
|
||||||
border-bottom: 1px solid ${(props) => props.theme.divider};
|
border-bottom: 1px solid ${(props) => props.theme.divider};
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
|
|
||||||
@ -226,6 +249,14 @@ const Cell = styled.td`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const Row = styled.tr`
|
const Row = styled.tr`
|
||||||
|
${Cell} {
|
||||||
|
&:first-child {
|
||||||
|
padding-left: 0;
|
||||||
|
}
|
||||||
|
&:last-child {
|
||||||
|
padding-right: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
&:last-child {
|
&:last-child {
|
||||||
${Cell} {
|
${Cell} {
|
||||||
border-bottom: 0;
|
border-bottom: 0;
|
||||||
@ -237,7 +268,7 @@ const Head = styled.th`
|
|||||||
text-align: left;
|
text-align: left;
|
||||||
position: sticky;
|
position: sticky;
|
||||||
top: 54px;
|
top: 54px;
|
||||||
padding: 6px 0;
|
padding: 6px;
|
||||||
border-bottom: 1px solid ${(props) => props.theme.divider};
|
border-bottom: 1px solid ${(props) => props.theme.divider};
|
||||||
background: ${(props) => props.theme.background};
|
background: ${(props) => props.theme.background};
|
||||||
transition: ${(props) => props.theme.backgroundTransition};
|
transition: ${(props) => props.theme.backgroundTransition};
|
||||||
@ -245,6 +276,14 @@ const Head = styled.th`
|
|||||||
color: ${(props) => props.theme.textSecondary};
|
color: ${(props) => props.theme.textSecondary};
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
|
|
||||||
|
:first-child {
|
||||||
|
padding-left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
:last-child {
|
||||||
|
padding-right: 0;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export default observer(Table);
|
export default observer(Table);
|
||||||
|
@ -226,6 +226,7 @@ function People(props) {
|
|||||||
onChangePage={handleChangePage}
|
onChangePage={handleChangePage}
|
||||||
page={page}
|
page={page}
|
||||||
totalPages={totalPages}
|
totalPages={totalPages}
|
||||||
|
defaultSortDirection="ASC"
|
||||||
/>
|
/>
|
||||||
{can.inviteUser && (
|
{can.inviteUser && (
|
||||||
<Modal
|
<Modal
|
||||||
|
@ -202,14 +202,14 @@
|
|||||||
"Choose a collection": "Choose a collection",
|
"Choose a collection": "Choose a collection",
|
||||||
"Unpin": "Unpin",
|
"Unpin": "Unpin",
|
||||||
"Pin to collection": "Pin to collection",
|
"Pin to collection": "Pin to collection",
|
||||||
"Enable embeds": "Enable embeds",
|
|
||||||
"Disable embeds": "Disable embeds",
|
|
||||||
"New nested document": "New nested document",
|
"New nested document": "New nested document",
|
||||||
"Create template": "Create template",
|
"Create template": "Create template",
|
||||||
"Duplicate": "Duplicate",
|
"Duplicate": "Duplicate",
|
||||||
"Unpublish": "Unpublish",
|
"Unpublish": "Unpublish",
|
||||||
"Permanently delete": "Permanently delete",
|
"Permanently delete": "Permanently delete",
|
||||||
"Move": "Move",
|
"Move": "Move",
|
||||||
|
"Enable embeds": "Enable embeds",
|
||||||
|
"Disable embeds": "Disable embeds",
|
||||||
"Download": "Download",
|
"Download": "Download",
|
||||||
"Print": "Print",
|
"Print": "Print",
|
||||||
"Move {{ documentName }}": "Move {{ documentName }}",
|
"Move {{ documentName }}": "Move {{ documentName }}",
|
||||||
|
Reference in New Issue
Block a user