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]) =>