Sammy/t 131 i see a ascending descending option in (#73)
* refactor: use safe css selector * feature: onclick update the order option * feature: set option in the dropdown * feature: display icon for sort options * refactor: indent key so react does not complain of conflicting keys * feature: align icon and text * feature: fix size of icon to align text * feature: create design for TopOption in dropdown * refactor: set font weight in style * feature: finalise design of TopOption * refactor: rename option to sort * refactor: remove order from the sortType * refactor: move sort mapper in service * test: selection of Descending in SortDropdownButton * refactor: fix styme-component warning * feature: add sorting by people * refactor: set SortFields types for tables * feature: add sort by company * refactor: rename sortFields to singular * refactor: rename option to SortDirection
This commit is contained in:
@ -9,15 +9,15 @@ import {
|
||||
import TableHeader from './table-header/TableHeader';
|
||||
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
||||
import styled from '@emotion/styled';
|
||||
import { SortType } from './table-header/SortAndFilterBar';
|
||||
import { SelectedSortType, SortType } from './table-header/SortAndFilterBar';
|
||||
|
||||
type OwnProps<TData> = {
|
||||
type OwnProps<TData, SortField> = {
|
||||
data: Array<TData>;
|
||||
columns: Array<ColumnDef<TData, any>>;
|
||||
viewName: string;
|
||||
viewIcon?: IconProp;
|
||||
onSortsUpdate?: (sorts: Array<SortType>) => void;
|
||||
sortsAvailable?: Array<SortType>;
|
||||
onSortsUpdate?: (sorts: Array<SelectedSortType<SortField>>) => void;
|
||||
sortsAvailable?: Array<SortType<SortField>>;
|
||||
};
|
||||
|
||||
const StyledTable = styled.table`
|
||||
@ -71,14 +71,14 @@ const StyledTableScrollableContainer = styled.div`
|
||||
flex: 1;
|
||||
`;
|
||||
|
||||
function Table<TData>({
|
||||
function Table<TData, SortField extends string>({
|
||||
data,
|
||||
columns,
|
||||
viewName,
|
||||
viewIcon,
|
||||
onSortsUpdate,
|
||||
sortsAvailable,
|
||||
}: OwnProps<TData>) {
|
||||
}: OwnProps<TData, SortField>) {
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
|
||||
@ -57,11 +57,11 @@ const StyledDropdown = styled.ul`
|
||||
li {
|
||||
border-radius: 2px;
|
||||
|
||||
&:first-child {
|
||||
&:first-of-type {
|
||||
border-top-left-radius: var(--outer-border-radius);
|
||||
border-top-right-radius: var(--outer-border-radius);
|
||||
}
|
||||
&:last-child {
|
||||
&:last-of-type {
|
||||
border-bottom-left-radius: var(--outer-border-radius);
|
||||
border-bottom-right-radius: var(--outer-border-radius);
|
||||
}
|
||||
@ -70,6 +70,7 @@ const StyledDropdown = styled.ul`
|
||||
|
||||
const StyledDropdownItem = styled.li`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: ${(props) => props.theme.spacing(2)}
|
||||
calc(${(props) => props.theme.spacing(2)} - 2px);
|
||||
margin: 2px;
|
||||
@ -82,9 +83,29 @@ const StyledDropdownItem = styled.li`
|
||||
}
|
||||
`;
|
||||
|
||||
const StyledDropdownTopOption = styled.li`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: calc(${(props) => props.theme.spacing(2)} + 2px)
|
||||
calc(${(props) => props.theme.spacing(2)});
|
||||
background: rgba(0, 0, 0, 0);
|
||||
cursor: pointer;
|
||||
color: ${(props) => props.theme.text60};
|
||||
font-weight: ${(props) => props.theme.fontWeightBold};
|
||||
|
||||
&:hover {
|
||||
background: rgba(0, 0, 0, 0.04);
|
||||
}
|
||||
border-radius: 0%;
|
||||
border-bottom: 1px solid ${(props) => props.theme.primaryBorder};
|
||||
`;
|
||||
|
||||
const StyledIcon = styled.div`
|
||||
display: flex;
|
||||
margin-right: ${(props) => props.theme.spacing(1)};
|
||||
min-width: ${(props) => props.theme.spacing(4)};
|
||||
justify-content: center;
|
||||
`;
|
||||
|
||||
function DropdownButton({
|
||||
@ -122,6 +143,7 @@ function DropdownButton({
|
||||
}
|
||||
|
||||
DropdownButton.StyledDropdownItem = StyledDropdownItem;
|
||||
DropdownButton.StyledDropdownTopOption = StyledDropdownTopOption;
|
||||
DropdownButton.StyledIcon = StyledIcon;
|
||||
|
||||
export default DropdownButton;
|
||||
|
||||
@ -3,18 +3,21 @@ import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
||||
import SortOrFilterChip from './SortOrFilterChip';
|
||||
import { faArrowDown, faArrowUp } from '@fortawesome/pro-regular-svg-icons';
|
||||
|
||||
type OwnProps = {
|
||||
sorts: Array<SortType>;
|
||||
type OwnProps<SortField> = {
|
||||
sorts: Array<SelectedSortType<SortField>>;
|
||||
onRemoveSort: (sortId: string) => void;
|
||||
};
|
||||
|
||||
export type SortType<SortIds = string> = {
|
||||
label: string;
|
||||
order: 'asc' | 'desc';
|
||||
id: SortIds;
|
||||
icon?: IconProp;
|
||||
};
|
||||
|
||||
export type SelectedSortType<SortField = string> = SortType<SortField> & {
|
||||
order: 'asc' | 'desc';
|
||||
};
|
||||
|
||||
const StyledBar = styled.div`
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
@ -44,7 +47,10 @@ const StyledCancelButton = styled.button`
|
||||
}
|
||||
`;
|
||||
|
||||
function SortAndFilterBar({ sorts, onRemoveSort }: OwnProps) {
|
||||
function SortAndFilterBar<SortField extends string>({
|
||||
sorts,
|
||||
onRemoveSort,
|
||||
}: OwnProps<SortField>) {
|
||||
return (
|
||||
<StyledBar>
|
||||
{sorts.map((sort) => {
|
||||
|
||||
@ -1,27 +1,35 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import DropdownButton from './DropdownButton';
|
||||
import { SortType } from './SortAndFilterBar';
|
||||
import { SelectedSortType, SortType } from './SortAndFilterBar';
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import { faAngleDown } from '@fortawesome/pro-regular-svg-icons';
|
||||
|
||||
type OwnProps = {
|
||||
sorts: SortType[];
|
||||
setSorts: (sorts: SortType[]) => void;
|
||||
sortsAvailable: SortType[];
|
||||
type OwnProps<SortField> = {
|
||||
sorts: SelectedSortType<SortField>[];
|
||||
setSorts: (sorts: SelectedSortType<SortField>[]) => void;
|
||||
sortsAvailable: SortType<SortField>[];
|
||||
};
|
||||
|
||||
export function SortDropdownButton({
|
||||
const options: Array<SelectedSortType<string>['order']> = ['asc', 'desc'];
|
||||
|
||||
export function SortDropdownButton<SortField extends string>({
|
||||
sortsAvailable,
|
||||
setSorts,
|
||||
sorts,
|
||||
}: OwnProps) {
|
||||
}: OwnProps<SortField>) {
|
||||
const [isUnfolded, setIsUnfolded] = useState(false);
|
||||
|
||||
const [isOptionUnfolded, setIsOptionUnfolded] = useState(false);
|
||||
|
||||
const [selectedSortDirection, setSelectedSortDirection] =
|
||||
useState<SelectedSortType<SortField>['order']>('asc');
|
||||
|
||||
const onSortItemSelect = useCallback(
|
||||
(sort: SortType) => {
|
||||
const newSorts = [sort];
|
||||
(sort: SortType<SortField>) => {
|
||||
const newSorts = [{ ...sort, order: selectedSortDirection }];
|
||||
setSorts(newSorts);
|
||||
},
|
||||
[setSorts],
|
||||
[setSorts, selectedSortDirection],
|
||||
);
|
||||
|
||||
return (
|
||||
@ -31,20 +39,42 @@ export function SortDropdownButton({
|
||||
isUnfolded={isUnfolded}
|
||||
setIsUnfolded={setIsUnfolded}
|
||||
>
|
||||
{sortsAvailable.map((option, index) => (
|
||||
<DropdownButton.StyledDropdownItem
|
||||
key={index}
|
||||
onClick={() => {
|
||||
setIsUnfolded(false);
|
||||
onSortItemSelect(option);
|
||||
}}
|
||||
>
|
||||
<DropdownButton.StyledIcon>
|
||||
{option.icon && <FontAwesomeIcon icon={option.icon} />}
|
||||
</DropdownButton.StyledIcon>
|
||||
{option.label}
|
||||
</DropdownButton.StyledDropdownItem>
|
||||
))}
|
||||
{isOptionUnfolded
|
||||
? options.map((option, index) => (
|
||||
<DropdownButton.StyledDropdownItem
|
||||
key={index}
|
||||
onClick={() => {
|
||||
setSelectedSortDirection(option);
|
||||
setIsOptionUnfolded(false);
|
||||
}}
|
||||
>
|
||||
{option === 'asc' ? 'Ascending' : 'Descending'}
|
||||
</DropdownButton.StyledDropdownItem>
|
||||
))
|
||||
: [
|
||||
<DropdownButton.StyledDropdownTopOption
|
||||
key={0}
|
||||
onClick={() => setIsOptionUnfolded(true)}
|
||||
>
|
||||
{selectedSortDirection === 'asc' ? 'Ascending' : 'Descending'}
|
||||
|
||||
<FontAwesomeIcon icon={faAngleDown} />
|
||||
</DropdownButton.StyledDropdownTopOption>,
|
||||
...sortsAvailable.map((sort, index) => (
|
||||
<DropdownButton.StyledDropdownItem
|
||||
key={index + 1}
|
||||
onClick={() => {
|
||||
setIsUnfolded(false);
|
||||
onSortItemSelect(sort);
|
||||
}}
|
||||
>
|
||||
<DropdownButton.StyledIcon>
|
||||
{sort.icon && <FontAwesomeIcon icon={sort.icon} />}
|
||||
</DropdownButton.StyledIcon>
|
||||
{sort.label}
|
||||
</DropdownButton.StyledDropdownItem>
|
||||
)),
|
||||
]}
|
||||
</DropdownButton>
|
||||
);
|
||||
}
|
||||
|
||||
@ -17,10 +17,9 @@ const StyledChip = styled.div`
|
||||
background-color: ${(props) => props.theme.blueHighTransparency};
|
||||
border: 1px solid ${(props) => props.theme.blueLowTransparency};
|
||||
color: ${(props) => props.theme.blue};
|
||||
padding: ${(props) => props.theme.spacing(1)}
|
||||
${(props) => props.theme.spacing(2)};
|
||||
padding: ${(props) => props.theme.spacing(1) + ' ' + props.theme.spacing(2)};
|
||||
margin-left: ${(props) => props.theme.spacing(2)};
|
||||
fontsize: ${(props) => props.theme.fontSizeSmall};
|
||||
font-size: ${(props) => props.theme.fontSizeSmall};
|
||||
`;
|
||||
const StyledIcon = styled.div`
|
||||
margin-right: ${(props) => props.theme.spacing(1)};
|
||||
|
||||
@ -2,15 +2,18 @@ import styled from '@emotion/styled';
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import DropdownButton from './DropdownButton';
|
||||
import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
||||
import SortAndFilterBar, { SortType } from './SortAndFilterBar';
|
||||
import SortAndFilterBar, {
|
||||
SelectedSortType,
|
||||
SortType,
|
||||
} from './SortAndFilterBar';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { SortDropdownButton } from './SortDropdownButton';
|
||||
|
||||
type OwnProps = {
|
||||
type OwnProps<SortField> = {
|
||||
viewName: string;
|
||||
viewIcon?: IconProp;
|
||||
onSortsUpdate?: (sorts: Array<SortType>) => void;
|
||||
sortsAvailable: Array<SortType>;
|
||||
onSortsUpdate?: (sorts: Array<SelectedSortType<SortField>>) => void;
|
||||
sortsAvailable: Array<SortType<SortField>>;
|
||||
};
|
||||
|
||||
const StyledContainer = styled.div`
|
||||
@ -49,16 +52,18 @@ const StyledFilters = styled.div`
|
||||
margin-right: ${(props) => props.theme.spacing(2)};
|
||||
`;
|
||||
|
||||
function TableHeader({
|
||||
function TableHeader<SortField extends string>({
|
||||
viewName,
|
||||
viewIcon,
|
||||
onSortsUpdate,
|
||||
sortsAvailable,
|
||||
}: OwnProps) {
|
||||
const [sorts, innerSetSorts] = useState([] as Array<SortType>);
|
||||
}: OwnProps<SortField>) {
|
||||
const [sorts, innerSetSorts] = useState<Array<SelectedSortType<SortField>>>(
|
||||
[],
|
||||
);
|
||||
|
||||
const setSorts = useCallback(
|
||||
(sorts: SortType[]) => {
|
||||
(sorts: SelectedSortType<SortField>[]) => {
|
||||
innerSetSorts(sorts);
|
||||
onSortsUpdate && onSortsUpdate(sorts);
|
||||
},
|
||||
@ -67,7 +72,7 @@ function TableHeader({
|
||||
|
||||
const onSortItemUnSelect = useCallback(
|
||||
(sortId: string) => {
|
||||
const newSorts = [] as SortType[];
|
||||
const newSorts = [] as SelectedSortType<SortField>[];
|
||||
innerSetSorts(newSorts);
|
||||
onSortsUpdate && onSortsUpdate(newSorts);
|
||||
},
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
import { SelectedSortType, SortType } from '../SortAndFilterBar';
|
||||
import { ThemeProvider } from '@emotion/react';
|
||||
import { lightTheme } from '../../../../layout/styles/themes';
|
||||
import { faArrowDown } from '@fortawesome/pro-regular-svg-icons';
|
||||
import { SortDropdownButton } from '../SortDropdownButton';
|
||||
|
||||
const component = {
|
||||
title: 'SortDropdownButton',
|
||||
component: SortDropdownButton,
|
||||
};
|
||||
|
||||
export default component;
|
||||
|
||||
type OwnProps = {
|
||||
setSorts: () => void;
|
||||
};
|
||||
|
||||
const sorts = [] satisfies SelectedSortType[];
|
||||
|
||||
const availableSorts = [
|
||||
{
|
||||
label: 'Email',
|
||||
id: 'email',
|
||||
icon: faArrowDown,
|
||||
},
|
||||
] satisfies SortType[];
|
||||
|
||||
export const RegularSortDropdownButton = ({ setSorts }: OwnProps) => {
|
||||
return (
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<SortDropdownButton
|
||||
sorts={sorts}
|
||||
sortsAvailable={availableSorts}
|
||||
setSorts={setSorts}
|
||||
/>
|
||||
</ThemeProvider>
|
||||
);
|
||||
};
|
||||
@ -16,7 +16,6 @@ export const RegularTableHeader = () => {
|
||||
{
|
||||
id: 'created_at',
|
||||
label: 'Created at',
|
||||
order: 'asc',
|
||||
icon: faCalendar,
|
||||
},
|
||||
];
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
import { fireEvent, render } from '@testing-library/react';
|
||||
import { RegularSortDropdownButton } from '../__stories__/SortDropdownButton.stories';
|
||||
import { faArrowDown } from '@fortawesome/pro-regular-svg-icons';
|
||||
|
||||
it('Checks the default top option is Ascending', async () => {
|
||||
const setSorts = jest.fn();
|
||||
const { getByText } = render(
|
||||
<RegularSortDropdownButton setSorts={setSorts} />,
|
||||
);
|
||||
|
||||
const sortDropdownButton = getByText('Sort');
|
||||
fireEvent.click(sortDropdownButton);
|
||||
|
||||
const sortByEmail = getByText('Email');
|
||||
fireEvent.click(sortByEmail);
|
||||
|
||||
expect(setSorts).toHaveBeenCalledWith([
|
||||
{
|
||||
label: 'Email',
|
||||
id: 'email',
|
||||
icon: faArrowDown,
|
||||
order: 'asc',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('Checks the selection of Descending', async () => {
|
||||
const setSorts = jest.fn();
|
||||
const { getByText } = render(
|
||||
<RegularSortDropdownButton setSorts={setSorts} />,
|
||||
);
|
||||
|
||||
const sortDropdownButton = getByText('Sort');
|
||||
fireEvent.click(sortDropdownButton);
|
||||
|
||||
const openTopOption = getByText('Ascending');
|
||||
fireEvent.click(openTopOption);
|
||||
|
||||
const selectDescending = getByText('Descending');
|
||||
fireEvent.click(selectDescending);
|
||||
|
||||
const sortByEmail = getByText('Email');
|
||||
fireEvent.click(sortByEmail);
|
||||
|
||||
expect(setSorts).toHaveBeenCalledWith([
|
||||
{
|
||||
label: 'Email',
|
||||
id: 'email',
|
||||
icon: faArrowDown,
|
||||
order: 'desc',
|
||||
},
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user