Skip to content

Commit

Permalink
81 Adds simple context menu (WIP) and updates the sorting mechanism f…
Browse files Browse the repository at this point in the history
…or tables in the whole app
  • Loading branch information
ricardovdheijden committed Oct 17, 2023
1 parent d95a0bb commit 410c68e
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ export const WFOProcessList: FC<WFOProcessListProps> = ({
setDataDisplayParam,
)}
onUpdateDataSort={getDataSortHandler<ProcessListItem>(
dataDisplayParams,
setDataDisplayParam,
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ export const WFORelatedSubscriptions = ({
},
};

const sortHandler = getDataSortHandler<RelatedSubscription>(
dataDisplayParams,
setDataDisplayParam,
);

const pagination: Pagination = {
pageSize: dataDisplayParams.pageSize,
pageIndex: dataDisplayParams.pageIndex,
Expand All @@ -147,11 +142,8 @@ export const WFORelatedSubscriptions = ({
const onUpdatePage =
getPageChangeHandler<RelatedSubscription>(setDataDisplayParam);

const onCriteriaChange = (criterion: Criteria<RelatedSubscription>) => {
if (criterion.page) {
onUpdatePage(criterion.page);
}
};
const handleCriteriaChange = (criterion: Criteria<RelatedSubscription>) =>
criterion.page && onUpdatePage(criterion.page);

const toggleTerminatedSubscriptions = () => {
setHideTerminatedSubscriptions((currentValue) => !currentValue);
Expand Down Expand Up @@ -180,9 +172,11 @@ export const WFORelatedSubscriptions = ({
columns={tableColumns}
pagination={pagination}
isLoading={isFetching}
onDataSort={sortHandler}
onUpdateDataSorting={getDataSortHandler(
setDataDisplayParam,
)}
dataSorting={dataSorting}
onCriteriaChange={onCriteriaChange}
onCriteriaChange={handleCriteriaChange}
/>
</>
)) || (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export const WFOSubscriptionsList: FC<WFOSubscriptionsListProps> = ({
setDataDisplayParam,
)}
onUpdateDataSort={getDataSortHandler<SubscriptionListItem>(
dataDisplayParams,
setDataDisplayParam,
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type WFOBasicTableProps<T> = {
pagination?: Pagination;
isLoading?: boolean;
onCriteriaChange?: (criteria: Criteria<T>) => void;
onDataSort?: (columnId: keyof T) => void;
onUpdateDataSorting?: (updatedDataSorting: WFODataSorting<T>) => void;
};

export const WFOBasicTable = <T,>({
Expand All @@ -41,7 +41,7 @@ export const WFOBasicTable = <T,>({
pagination,
isLoading,
onCriteriaChange,
onDataSort,
onUpdateDataSorting,
}: WFOBasicTableProps<T>) => {
const { theme } = useOrchestratorTheme();
const { basicTableStyle } = getStyles(theme);
Expand All @@ -54,7 +54,7 @@ export const WFOBasicTable = <T,>({
columns,
hiddenColumns,
dataSorting,
onDataSort,
onUpdateDataSorting,
)}
pagination={pagination}
onChange={onCriteriaChange}
Expand All @@ -67,7 +67,7 @@ function mapWFOTableColumnsToEuiColumns<T>(
tableColumns: WFOBasicTableColumns<T>,
hiddenColumns?: TableColumnKeys<T>,
dataSorting?: WFODataSorting<T>,
onDataSort?: (columnId: keyof T) => void,
onDataSort?: (updatedDataSorting: WFODataSorting<T>) => void,
): EuiBasicTableColumn<T>[] {
function isVisibleColumn(columnKey: string) {
return !hiddenColumns?.includes(columnKey as keyof T);
Expand All @@ -79,10 +79,10 @@ function mapWFOTableColumnsToEuiColumns<T>(
tableColumns[typedColumnKey];
const { name, render, width, description, sortable } = column;

const sortDirection =
const sortOrder =
dataSorting?.field === colKey ? dataSorting.sortOrder : undefined;

const handleClick = () => onDataSort?.(typedColumnKey);
// Todo introduce a handleSetSortOrder...

// Not spreading the column object here as it might contain additional props.
// EUI does not handle extra props well.
Expand All @@ -93,8 +93,10 @@ function mapWFOTableColumnsToEuiColumns<T>(
field: typedColumnKey,
name: name && (
<WFOTableHeaderCell
sortDirection={sortDirection}
onClick={handleClick}
sortOrder={sortOrder}
onSetSortOrder={(sortOrder) =>
onDataSort?.({ field: typedColumnKey, sortOrder })
}
isSortable={sortable}
>
{name}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,143 @@
import React, { FC, ReactNode } from 'react';
import React, { FC, ReactNode, useState } from 'react';

import { SortOrder } from '../../../types';
import { WFOSortDirectionIcon } from './WFOSortDirectionIcon';
import {
EuiFieldSearch,
EuiHorizontalRule,
EuiPopover,
EuiText,
useGeneratedHtmlId,
} from '@elastic/eui';
import { WFOSortAsc, WFOSortDesc } from '../../../icons';
import { useOrchestratorTheme } from '../../../hooks';

export type WFOTableHeaderCellProps = {
sortDirection?: SortOrder;
onClick?: () => void;
sortOrder?: SortOrder;
onSetSortOrder?: (updatedSortOrder: SortOrder) => void;
isSortable?: boolean;
children: ReactNode;
};

// Todo add dropdown logic here
export const WFOTableHeaderCell: FC<WFOTableHeaderCellProps> = ({
sortDirection,
sortOrder,
children,
onClick,
isSortable = true,
}) => (
<div
css={{
display: 'flex',
alignItems: 'center',
cursor: isSortable ? 'pointer' : 'not-allowed',
}}
onClick={isSortable ? onClick : undefined}
>
<div>{children}</div>
{isSortable && sortDirection ? (
<WFOSortDirectionIcon sortDirection={sortDirection} />
) : null}
</div>
);
onSetSortOrder,
}) => {
const { theme } = useOrchestratorTheme();
const smallContextMenuPopoverId = useGeneratedHtmlId({
prefix: 'smallContextMenuPopover',
});

const [isPopoverOpen, setPopover] = useState(false);
const handleButtonClick = () => setPopover(!isPopoverOpen);
const handleClosePopover = () => setPopover(false);

const PopoverButton = () => (
<button onClick={handleButtonClick}>
<div
css={{
display: 'flex',
alignItems: 'center',
cursor: isSortable ? 'pointer' : 'not-allowed',
}}
>
<div css={{ fontWeight: theme.font.weight.semiBold }}>
{children}
</div>
{sortOrder && (
<WFOSortDirectionIcon sortDirection={sortOrder} />
)}
</div>
</button>
);

return (
<EuiPopover
id={smallContextMenuPopoverId}
button={<PopoverButton />}
isOpen={isPopoverOpen}
closePopover={handleClosePopover}
panelPaddingSize="none"
anchorPosition="downLeft"
>
<div
css={{
margin: 12,
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<EuiText
size="xs"
css={{ fontWeight: theme.font.weight.medium }}
>
Title and controls
</EuiText>
<div css={{ display: 'flex', alignItems: 'center' }}>
<button
css={{ display: 'flex', alignItems: 'center' }}
onClick={() =>
onSetSortOrder && onSetSortOrder(SortOrder.ASC)
}
>
<WFOSortAsc
color={
sortOrder === SortOrder.ASC
? theme.colors.title
: theme.colors.lightShade
}
/>
</button>
<button
css={{ display: 'flex', alignItems: 'center' }}
onClick={() =>
onSetSortOrder && onSetSortOrder(SortOrder.DESC)
}
>
<WFOSortDesc
color={
sortOrder === SortOrder.DESC
? theme.colors.title
: theme.colors.lightShade
}
/>
</button>
</div>
</div>
<EuiHorizontalRule margin="none" />
<div css={{ margin: 12 }}>
<EuiFieldSearch
placeholder="Search"
// value={value}
// onChange={(e) => onChange(e)}
isClearable={false}
/>
</div>
</EuiPopover>
);
};

// export const WFOTableHeaderCell: FC<WFOTableHeaderCellProps> = ({
// sortDirection,
// children,
// onClick,
// isSortable = true,
// }) => (
// <div
// css={{
// display: 'flex',
// alignItems: 'center',
// cursor: isSortable ? 'pointer' : 'not-allowed',
// }}
// onClick={isSortable ? onClick : undefined}
// >
// <div>{children}</div>
// {isSortable && sortDirection ? (
// <WFOSortDirectionIcon sortDirection={sortDirection} />
// ) : null}
// </div>
// );
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type WFOTableWithFilterProps<T> = {
detailModalTitle?: string;
onUpdateEsQueryString: (esQueryString: string) => void;
onUpdatePage: (criterion: Criteria<T>['page']) => void;
onUpdateDataSort: (newSortColumnId: keyof T) => void;
onUpdateDataSort: (dataSorting: WFODataSorting<T>) => void;
};

export const WFOTableWithFilter = <T,>({
Expand Down Expand Up @@ -201,7 +201,7 @@ export const WFOTableWithFilter = <T,>({
columns={tableColumnsWithControlColumns}
hiddenColumns={hiddenColumns}
dataSorting={dataSorting}
onDataSort={onUpdateDataSort}
onUpdateDataSorting={onUpdateDataSort}
pagination={pagination}
isLoading={isLoading}
onCriteriaChange={onCriteriaChange}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { Criteria } from '@elastic/eui';
import { SortOrder } from '../../../types';

import type {
DataDisplayReturnValues,
DataDisplayParams,
} from '../../../hooks';
import type { DataDisplayReturnValues } from '../../../hooks';
import { WFODataSorting } from './columns';

export const determinePageIndex = (pageIndex: number, pageSize: number) =>
Math.floor(pageIndex / pageSize);
Expand All @@ -25,29 +23,12 @@ export const determineNewSortOrder = <T>(

export const getDataSortHandler =
<Type>(
dataDisplayParams: DataDisplayParams<Type>,
setDataDisplayParam: DataDisplayReturnValues<Type>['setDataDisplayParam'],
) =>
(newSortField: keyof Type) => {
const newOrder = (() => {
if (
dataDisplayParams.sortBy &&
dataDisplayParams.sortBy.order &&
dataDisplayParams.sortBy.field
) {
return determineNewSortOrder<Type>(
dataDisplayParams.sortBy.field,
dataDisplayParams.sortBy.order,
newSortField,
);
}

return SortOrder.ASC;
})();

({ field, sortOrder }: WFODataSorting<Type>) => {
setDataDisplayParam('sortBy', {
field: newSortField,
order: newOrder,
field: field,
order: sortOrder,
});
};

Expand Down
32 changes: 32 additions & 0 deletions packages/orchestrator-ui-components/src/icons/WFOSortAsc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { FC } from 'react';
import { WFOIconProps } from './WFOIconProps';

export const WFOSortAsc: FC<WFOIconProps> = ({
width = 24,
height = 24,
color = '#000000',
}) => (
<svg
width={width}
height={height}
viewBox="0 0 24 24"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
>
<title>icon/sort-asc</title>
<g
id="Symbols"
stroke="none"
strokeWidth="1"
fill="none"
fillRule="evenodd"
>
<g id="icon/sort-asc" fill={color} fillRule="nonzero">
<path
d="M5,5 C4.44772,5 4,5.44772 4,6 C4,6.55228 4.44772,7 5,7 L16,7 C16.5523,7 17,6.55228 17,6 C17,5.44772 16.5523,5 16,5 L5,5 Z M5,9 C4.44772,9 4,9.44772 4,10 C4,10.55228 4.44772,11 5,11 L10,11 C10.55228,11 11,10.55228 11,10 C11,9.44772 10.55228,9 10,9 L5,9 Z M5,13 C4.44772,13 4,13.4477 4,14 C4,14.5523 4.44772,15 5,15 L9,15 C9.55228,15 10,14.5523 10,14 C10,13.4477 9.55228,13 9,13 L5,13 Z M15.0000025,18 C15.0000025,18.5523 15.4477,19 16.0000025,19 C16.5523,19 17.0000025,18.5523 17.0000025,18 L17.0000025,12.4142 L18.2929,13.7071 C18.6834,14.0976 19.3166,14.0976 19.7071,13.7071 C20.0976,13.3166 20.0976,12.6834 19.7071,12.2929 L16.7071,9.29289 C16.5196,9.10536 16.2652,9 16.0000025,9 C15.7348,9 15.4804,9.10536 15.2929,9.29289 L12.2929,12.2929 C11.90237,12.6834 11.90237,13.3166 12.2929,13.7071 C12.6834,14.0976 13.3166,14.0976 13.7071,13.7071 L15.0000025,12.4142 L15.0000025,18 Z"
id="Combined-Shape"
></path>
</g>
</g>
</svg>
);
Loading

0 comments on commit 410c68e

Please sign in to comment.