From f0e470472a16957217b091489afdab2006719294 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 17 Apr 2021 11:33:32 +0300 Subject: feat: add sort to ListTable --- src/components/ListTable.tsx | 35 ++++++++++++++++++++++++++++++----- src/lib/ServiceList.tsx | 3 +-- 2 files changed, 31 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/components/ListTable.tsx b/src/components/ListTable.tsx index 89a2e06..e90d2db 100644 --- a/src/components/ListTable.tsx +++ b/src/components/ListTable.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useMemo, useCallback } from 'react'; import _ from 'lodash'; export interface Field { @@ -10,9 +10,14 @@ export interface Field { interface Props { items?: T[]; fields: Field[]; - handleRowClick?: (index: number) => void; + handleRowClick?: (item: T, index?: number) => void; } +const sortLabels = { + asc: '^', + desc: 'v', +}; + const getItemField = (item: any, field: Field) => { const value = _.get(item, field.key); return field.transform ? field.transform(value) : value; @@ -20,20 +25,40 @@ const getItemField = (item: any, field: Field) => { const ListTable: React.FC = ({ items = [], fields, handleRowClick = () => {} }) => { + const [sortBy, setSortBy] = useState(''); + const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc'); + + const sortedItems = useMemo(() => { + const result = _.sortBy(items, sortBy); + if (sortOrder === 'asc') result.reverse(); + return result; + }, [items, sortBy, sortOrder]); + + const handleFieldClick = useCallback((field: Field) => { + if (sortBy === field.key) setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc'); + else setSortOrder('desc'); + setSortBy(field.key); + }, [sortBy, sortOrder]); + if (!items.length) return
No data
; + return ( - {fields.map(field => )} + {fields.map(field => ( + + ))} - {items.map((item, index) => ( + {sortedItems.map((item, index) => ( handleRowClick(index)} + onClick={() => handleRowClick(item, index)} > {fields.map(field => (
{field.label} handleFieldClick(field)}> + {field.label} {field.key === sortBy && sortLabels[sortOrder]} +
diff --git a/src/lib/ServiceList.tsx b/src/lib/ServiceList.tsx index 74ad10b..e7015ae 100644 --- a/src/lib/ServiceList.tsx +++ b/src/lib/ServiceList.tsx @@ -16,8 +16,7 @@ const ServiceList: React.FC = () => { route: `/${service.route}/add${location.search}`, }]; - const handleRowClick = (index: number) => { - const item = data && data[index]; + const handleRowClick = (item: any) => { const route = service.rowLink ? service.rowLink(item) : `/${service.route}/${item?._id}`; -- cgit v1.2.3