diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-14 13:47:56 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-14 13:47:56 +0300 |
commit | 296f43c5aef2a128defc3fe016c01f7d6455e6ba (patch) | |
tree | d90f67a1867529cb785ba2b0c3554ad5e63e8ceb /src/components/ListTable.tsx | |
parent | 2ff20a6b93a92f0cac6b9f5584e81e39407e51c5 (diff) | |
download | commercel-ui-296f43c5aef2a128defc3fe016c01f7d6455e6ba.tar.gz |
feat: enable transform of item fields
Diffstat (limited to 'src/components/ListTable.tsx')
-rw-r--r-- | src/components/ListTable.tsx | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/components/ListTable.tsx b/src/components/ListTable.tsx index c333698..7c316ed 100644 --- a/src/components/ListTable.tsx +++ b/src/components/ListTable.tsx @@ -4,6 +4,7 @@ import _ from 'lodash'; interface Field { key: string; label: string; + transform?: (text: string) => string; } interface Props { @@ -12,6 +13,11 @@ interface Props { handleRowClick?: (index: number) => void; } +const getItemField = (item: any, field: Field) => { + const value = _.get(item, field.key); + return field.transform ? field.transform(value) : value; +}; + const ListTable: React.FC<Props> = ({ items = [], fields, handleRowClick = () => {} }) => { if (!items.length) return <div className="text-center p-6">No data</div>; @@ -29,7 +35,11 @@ const ListTable: React.FC<Props> = ({ items = [], fields, handleRowClick = () => className={`border-b hover:bg-gray-100 cursor-pointer ${index % 2 && 'bg-gray-50'}`} onClick={() => handleRowClick(index)} > - {fields.map(field => <td key={`${item._id} ${field.label}`} className="p-3">{_.get(item, field.key)}</td>)} + {fields.map(field => ( + <td key={`${item._id} ${field.label}`} className="p-3"> + {getItemField(item, field)} + </td> + ))} </tr> ))} </tbody> |