From 39c1289d3237f601b254999a4fc1c8a9bb4d74ec Mon Sep 17 00:00:00 2001 From: goodmice Date: Thu, 6 Oct 2022 14:37:07 +0500 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=82=D0=B8=D0=BF=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D0=BE=D0=BD=D0=BA=D0=B0=D0=BC=D0=B8=20=D1=82=D0=B0=D0=B1?= =?UTF-8?q?=D0=BB=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Table/Columns/index.ts | 2 +- src/components/Table/Columns/numeric.tsx | 90 ++++++++++++------------ src/components/Table/Columns/text.tsx | 18 +++-- src/components/Table/sorters.ts | 3 +- 4 files changed, 56 insertions(+), 57 deletions(-) diff --git a/src/components/Table/Columns/index.ts b/src/components/Table/Columns/index.ts index 0a4e113..c97f091 100755 --- a/src/components/Table/Columns/index.ts +++ b/src/components/Table/Columns/index.ts @@ -30,7 +30,7 @@ export type DataType = Record export type RenderMethod = (value: T, dataset?: DataType, index?: number) => ReactNode export type SorterMethod = (a?: DataType | null, b?: DataType | null) => number -export type columnPropsOther = ColumnProps & { +export type columnPropsOther = ColumnProps> & { // редактируемая колонка editable?: boolean // react компонента редактора diff --git a/src/components/Table/Columns/numeric.tsx b/src/components/Table/Columns/numeric.tsx index 3737ca6..ad8fdee 100755 --- a/src/components/Table/Columns/numeric.tsx +++ b/src/components/Table/Columns/numeric.tsx @@ -2,11 +2,14 @@ import { InputNumber } from 'antd' import { ReactNode } from 'react' import { makeNumericSorter } from '../sorters' -import { columnPropsOther, makeGroupColumn, RenderMethod } from '.' +import makeColumn, { columnPropsOther, DataType, makeGroupColumn, RenderMethod } from '.' +import { ColumnFilterItem, CompareFn } from 'antd/lib/table/interface' export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/ -export const makeNumericRender = (fixed?: number): RenderMethod => (value) => { +type FilterMethod = (value: string | number | boolean, record: DataType) => boolean + +export const makeNumericRender = (fixed?: number): RenderMethod => (value: T) => { let val = '-' if ((value ?? null) !== null && Number.isFinite(+value)) { val = (fixed ?? null) !== null @@ -21,77 +24,74 @@ export const makeNumericRender = (fixed?: number): RenderMeth ) } -export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): columnPropsOther => ({ +export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): columnPropsOther => ({ editable: true, initialValue: 0, width: 100, - sorter: sorterKey ? makeNumericSorter(sorterKey) : undefined, + sorter: sorterKey ? makeNumericSorter(sorterKey) : undefined, formItemRules: [{ required: true, message: 'Введите число', pattern: RegExpIsFloat, }], - render: makeNumericRender(fixed), + render: makeNumericRender(fixed), }) -export const makeNumericColumn = ( +export const makeNumericColumn = ( title: ReactNode, - dataIndex: string, - filters: object[], - filterDelegate: (key: string | number) => any, - renderDelegate: (_: any, row: object) => any, - width: string, - other?: columnPropsOther -) => ({ - title: title, - dataIndex: dataIndex, - key: dataIndex, - filters: filters, - onFilter: filterDelegate ? filterDelegate(dataIndex) : null, - sorter: makeNumericSorter(dataIndex), - width: width, + key: string, + filters?: ColumnFilterItem[], + filterDelegate?: (key: string | number) => FilterMethod, + renderDelegate?: RenderMethod, + width?: string | number, + other?: columnPropsOther, +) => makeColumn(title, key, { + filters, + onFilter: filterDelegate ? filterDelegate(key) : undefined, + sorter: makeNumericSorter(key), + width, input: , - render: renderDelegate ?? makeNumericRender(), + render: renderDelegate ?? makeNumericRender(2), align: 'right', ...other }) -export const makeNumericColumnPlanFact = ( +export const makeNumericColumnPlanFact = ( title: ReactNode, - dataIndex: string, - filters: object[], - filterDelegate: (key: string | number) => any, - renderDelegate: (_: any, row: object) => any, - width: string + key: string, + filters?: ColumnFilterItem[], + filterDelegate?: (key: string | number) => FilterMethod, + renderDelegate?: RenderMethod, + width?: string | number ) => makeGroupColumn(title, [ - makeNumericColumn('п', dataIndex + 'Plan', filters, filterDelegate, renderDelegate, width), - makeNumericColumn('ф', dataIndex + 'Fact', filters, filterDelegate, renderDelegate, width), + makeNumericColumn('п', key + 'Plan', filters, filterDelegate, renderDelegate, width), + makeNumericColumn('ф', key + 'Fact', filters, filterDelegate, renderDelegate, width), ]) -export const makeNumericStartEnd = ( +export const makeNumericStartEnd = ( title: ReactNode, - dataIndex: string, + key: string, fixed: number, - filters: object[], - filterDelegate: (key: string | number) => any, - renderDelegate: (_: any, row: object) => any, - width: string, + filters?: ColumnFilterItem[], + filterDelegate?: (key: string | number) => FilterMethod, + renderDelegate?: RenderMethod, + width?: string | number, ) => makeGroupColumn(title, [ - makeNumericColumn('старт', dataIndex + 'Start', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Start')), - makeNumericColumn('конец', dataIndex + 'End', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'End')) + makeNumericColumn('старт', key + 'Start', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, key + 'Start')), + makeNumericColumn('конец', key + 'End', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, key + 'End')) ]) -export const makeNumericMinMax = ( +export const makeNumericMinMax = ( title: ReactNode, - dataIndex: string, + key: string, fixed: number, - filters: object[], - filterDelegate: (key: string | number) => any, - renderDelegate: (_: any, row: object) => any, - width: string, + filters?: ColumnFilterItem[], + filterDelegate?: (key: string | number) => FilterMethod, + renderDelegate?: RenderMethod, + width?: string | number, ) => makeGroupColumn(title, [ - makeNumericColumn('мин', dataIndex + 'Min', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Min')), - makeNumericColumn('макс', dataIndex + 'Max', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Max')), + makeNumericColumn('мин', key + 'Min', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, key + 'Min')), + makeNumericColumn('макс', key + 'Max', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, key + 'Max')), ]) export default makeNumericColumn diff --git a/src/components/Table/Columns/text.tsx b/src/components/Table/Columns/text.tsx index 992b984..afa6caa 100755 --- a/src/components/Table/Columns/text.tsx +++ b/src/components/Table/Columns/text.tsx @@ -1,6 +1,7 @@ +import { ColumnFilterItem } from 'antd/lib/table/interface' import { ReactNode } from 'react' -import { columnPropsOther, DataType, RenderMethod, SorterMethod } from '.' +import makeColumn, { columnPropsOther, DataType, RenderMethod, SorterMethod } from '.' import { makeStringSorter } from '../sorters' export const makeFilterTextMatch = (key: keyof DataType) => @@ -8,18 +9,15 @@ export const makeFilterTextMatch = (key: keyof DataType) = export const makeTextColumn = ( title: ReactNode, - dataIndex: string, - filters: object[], + key: string, + filters?: ColumnFilterItem[], sorter?: SorterMethod, render?: RenderMethod, other?: columnPropsOther -) => ({ - title: title, - dataIndex: dataIndex, - key: dataIndex, - filters: filters, - onFilter: filters ? makeFilterTextMatch(dataIndex) : null, - sorter: sorter ?? makeStringSorter(dataIndex), +) => makeColumn(title, key, { + filters, + onFilter: filters ? makeFilterTextMatch(key) : undefined, + sorter: sorter ?? makeStringSorter(key), render: render, ...other }) diff --git a/src/components/Table/sorters.ts b/src/components/Table/sorters.ts index 80ca5c0..6f50401 100755 --- a/src/components/Table/sorters.ts +++ b/src/components/Table/sorters.ts @@ -3,8 +3,9 @@ import { isRawDate } from '@utils' import { TimeDto } from '@api' import { DataType } from './Columns' +import { CompareFn } from 'antd/lib/table/interface' -export const makeNumericSorter = (key: keyof DataType) => +export const makeNumericSorter = (key: keyof DataType): CompareFn> => (a: DataType, b: DataType) => Number(a[key]) - Number(b[key]) export const makeNumericObjSorter = (key: [string, string]) =>