-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: QUV-758 Create column sorting and OrderIndicator
- add orderindicator component - add example order table - add hook useOrder
- Loading branch information
santiago.trigo
committed
Dec 28, 2023
1 parent
bbb669b
commit c940c60
Showing
12 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import { ColDef } from '../../declarations'; | ||
import { StyledSortIndicator } from './StyledSortIndicator'; | ||
import { StyledSortIndex } from './StyledSortIndex'; | ||
import { Flex } from '@devoinc/genesys-ui'; | ||
|
||
interface OrderIndicatorProps { | ||
colDef: ColDef; | ||
} | ||
|
||
export const OrderIndicator: React.FC<OrderIndicatorProps> = ({ colDef }) => { | ||
return ( | ||
<Flex flexDirection="row" gap="cmp-xs" justifyContent={'flex-end'}> | ||
<StyledSortIndicator sort={colDef.sort} /> | ||
<StyledSortIndex>{colDef.sortIndex}</StyledSortIndex> | ||
</Flex> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const StyledSortIndex = styled.span` | ||
position: relative; | ||
display: block; | ||
font-family: 'gi', sans-serif; | ||
font-size: 1rem; | ||
line-height: 1.5; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import styled, { css } from 'styled-components'; | ||
import icons from '@devoinc/genesys-icons/dist/icon-variables.js'; | ||
|
||
const iconsSort = { | ||
asc: icons.sort_asc_carets, | ||
desc: icons.sort_desc_carets, | ||
}; | ||
|
||
interface StyledSortIndicatorProps { | ||
sort: 'asc' | 'desc' | 'none'; | ||
} | ||
|
||
export const StyledSortIndicator = styled.span<StyledSortIndicatorProps>` | ||
${({ sort }) => { | ||
return css` | ||
&::before { | ||
content: '${iconsSort[sort]}'; | ||
position: relative; | ||
display: block; | ||
font-family: 'gi', sans-serif; | ||
font-size: 1rem; | ||
line-height: 1.5; | ||
} | ||
`; | ||
}} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { OrderColumn, useOrderStruct } from './useOrderStruct'; | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
describe('getOptionsFromData', () => { | ||
const cases: [string, OrderColumn[], string, OrderColumn[]][] = [ | ||
['Changed desc - none', [{ id: 'id', sort: 'desc' }], 'id', []], | ||
[ | ||
'Changed asc - desc', | ||
[{ id: 'id', sort: 'asc' }], | ||
'id', | ||
[{ id: 'id', sort: 'desc' }], | ||
], | ||
['sort none', [], 'id', [{ id: 'id', sort: 'asc' }]], | ||
]; | ||
|
||
it.each(cases)('%s', (_title, initial, id, expected) => { | ||
const { result } = renderHook(() => useOrderStruct(initial)); | ||
renderHook(() => result.current.onSort(id)); | ||
expect(result.current.orderStruct).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react'; | ||
|
||
export type OrderColumn = { | ||
id: string; | ||
sort: 'asc' | 'desc'; | ||
}; | ||
|
||
export const useOrderStruct = (initial: OrderColumn[] = []) => { | ||
const [orderStruct, setOrderStruct] = React.useState<OrderColumn[]>(initial); | ||
|
||
const onSort = (id: string) => { | ||
let newOrderStruct = orderStruct.map((col) => ({ ...col })); | ||
|
||
const col = orderStruct.find((col) => col.id === id); | ||
if (col) { | ||
if (col.sort === 'asc') { | ||
newOrderStruct = newOrderStruct.map((col) => | ||
col.id === id ? { ...col, sort: 'desc' } : col, | ||
); | ||
} else { | ||
newOrderStruct = newOrderStruct.filter((col) => col.id !== id); | ||
} | ||
} else { | ||
newOrderStruct = newOrderStruct.concat({ id, sort: 'asc' }); | ||
} | ||
|
||
setOrderStruct(newOrderStruct); | ||
}; | ||
|
||
return { orderStruct, onSort }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/table/src/helpers/orderDataByOrderStruct.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { OrderColumn } from '../core/hooks/useOrderStruct'; | ||
import { Data } from '../declarations'; | ||
import { orderDataByOrderStruct } from './orderDataByOrderStruct'; | ||
|
||
describe('getOptionsFromData', () => { | ||
const cases: [string, Data, OrderColumn[], Data][] = [ | ||
[ | ||
'Order number desc', | ||
[ | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
], | ||
[{ id: 'id', sort: 'desc' }], | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order number asc', | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
[{ id: 'id', sort: 'asc' }], | ||
[ | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order none', | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
[], | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order multi number', | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 3, name: 'Jimmy Hogan', age: 50 }, | ||
], | ||
[ | ||
{ id: 'id', sort: 'desc' }, | ||
{ id: 'age', sort: 'desc' }, | ||
], | ||
[ | ||
{ id: 3, name: 'Jimmy Hogan', age: 50 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order multi number-string', | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 3, name: 'Jimmy Hogan', age: 50 }, | ||
], | ||
[ | ||
{ id: 'age', sort: 'desc' }, | ||
{ id: 'name', sort: 'asc' }, | ||
], | ||
[ | ||
{ id: 3, name: 'Jimmy Hogan', age: 50 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order stirng desc', | ||
[ | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
], | ||
[{ id: 'name', sort: 'desc' }], | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
], | ||
[ | ||
'Order string asc', | ||
[ | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
], | ||
[{ id: 'name', sort: 'asc' }], | ||
[ | ||
{ id: 1, name: 'Ina Osborne', age: 20 }, | ||
{ id: 2, name: 'Jimmy Hogan', age: 20 }, | ||
], | ||
], | ||
]; | ||
|
||
it.each(cases)('%s', (_title, data, orderStruct, expected) => { | ||
expect(orderDataByOrderStruct(data, orderStruct)).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { OrderColumn } from '../core/hooks/useOrderStruct'; | ||
|
||
const order = (orderStruct, a, b) => { | ||
for (const { sort, id } of orderStruct) { | ||
if (typeof a[id] === 'number') { | ||
if (sort === 'asc' && a[id] - b[id] !== 0) { | ||
return a[id] - b[id]; | ||
} | ||
if (sort === 'desc' && b[id] - a[id] !== 0) { | ||
return b[id] - a[id]; | ||
} | ||
} | ||
|
||
if (typeof a[id] === 'string') { | ||
if (sort === 'asc' && a[id].localeCompare(b[id]) !== 0) { | ||
return a[id].localeCompare(b[id]); | ||
} | ||
if (sort === 'desc' && b[id].localeCompare(a[id]) !== 0) { | ||
return b[id].localeCompare(a[id]); | ||
} | ||
} | ||
} | ||
}; | ||
export const orderDataByOrderStruct = (data, orderStruct: OrderColumn[]) => { | ||
if (orderStruct.length > 0) { | ||
const newData = data; | ||
return newData.sort((a, b) => order(orderStruct, a, b)); | ||
} | ||
return data; | ||
}; |
Oops, something went wrong.