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 | |
parent | 2ff20a6b93a92f0cac6b9f5584e81e39407e51c5 (diff) | |
download | commercel-ui-296f43c5aef2a128defc3fe016c01f7d6455e6ba.tar.gz |
feat: enable transform of item fields
-rw-r--r-- | src/components/ListTable.tsx | 12 | ||||
-rw-r--r-- | src/services.js | 16 |
2 files changed, 25 insertions, 3 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> diff --git a/src/services.js b/src/services.js index b9c2ecf..bebf07f 100644 --- a/src/services.js +++ b/src/services.js @@ -1,5 +1,17 @@ import { ServiceParams } from './containers/Service/ServiceContext'; + +const waybillStatusNames = { + waiting: 'Ожидание', + executed: 'Проведена', + cancelled: 'Отменена', +}; + +const operationNames = { + in: 'Приход', + out: 'Расход', +}; + const services: ServiceParams[] = [ { route: 'products', @@ -36,10 +48,10 @@ const services: ServiceParams[] = [ name: 'Накладные', nameSingular: 'Накладная', tableFields: [ - { key: 'operation', label: 'Операция' }, + { key: 'operation', label: 'Операция', transform: op => operationNames[op] }, { key: 'product.name', label: 'Товар' }, { key: 'contractor.name', label: 'Поставщик' }, - { key: 'status', label: 'Статус' }, + { key: 'status', label: 'Статус', transform: status => waybillStatusNames[status] }, { key: 'quantity', label: 'Количество' }, ], default: { |