2021-08-30 15:11:21 +05:00
|
|
|
|
import { ReactNode } from 'react'
|
2021-10-08 17:02:15 +05:00
|
|
|
|
import { makeNumericSorter, makeStringSorter} from './sorters'
|
|
|
|
|
export { makeDateSorter, makeNumericSorter, makeStringSorter} from './sorters'
|
2021-08-20 12:31:24 +05:00
|
|
|
|
export { Table } from 'antd'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
export { EditableTable } from './EditableTable'
|
|
|
|
|
export { DatePickerWrapper } from './DatePickerWrapper'
|
2021-08-20 12:31:24 +05:00
|
|
|
|
export { SelectFromDictionary } from './SelectFromDictionary'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
|
|
|
|
|
export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/
|
|
|
|
|
export const formatDate='YYYY.MM.DD HH:mm'
|
|
|
|
|
|
2021-10-08 17:02:15 +05:00
|
|
|
|
export const makeNumericRender = (fixed?:number) => (value: any, row: object): ReactNode => {
|
|
|
|
|
const placeholder = '-'
|
|
|
|
|
let val = placeholder
|
|
|
|
|
if((value !== null) ||
|
|
|
|
|
(value !== undefined) ||
|
|
|
|
|
Number.isNaN(value) ||
|
|
|
|
|
!Number.isFinite(value)){
|
|
|
|
|
val = !!fixed
|
|
|
|
|
? (+value).toFixed(fixed)
|
|
|
|
|
: (+value).toPrecision(5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (<div className='text-align-r-container'>
|
|
|
|
|
<span>{val}</span>
|
|
|
|
|
</div>)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const makeNumericColumnOptions = (fixed?:number, sorterKey?:string ) => ({
|
2021-08-20 10:49:20 +05:00
|
|
|
|
editable: true,
|
|
|
|
|
initialValue: 0,
|
|
|
|
|
width:100,
|
2021-10-08 17:02:15 +05:00
|
|
|
|
sorter: sorterKey? makeNumericSorter(sorterKey) : null,
|
2021-08-20 10:49:20 +05:00
|
|
|
|
formItemRules: [
|
|
|
|
|
{
|
|
|
|
|
required: true,
|
|
|
|
|
message: `Введите число`,
|
|
|
|
|
pattern: RegExpIsFloat,
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-10-08 17:02:15 +05:00
|
|
|
|
render: makeNumericRender(fixed),
|
|
|
|
|
})
|
2021-08-20 10:49:20 +05:00
|
|
|
|
|
2021-08-30 10:04:44 +05:00
|
|
|
|
/*
|
2021-08-20 10:49:20 +05:00
|
|
|
|
other - объект с дополнительными свойствами колонки
|
|
|
|
|
поддерживаются все базовые свойства из описания https://ant.design/components/table/#Column
|
2021-08-30 10:04:44 +05:00
|
|
|
|
плю дополнительные для колонок EditableTable: */
|
|
|
|
|
interface columnPropsOther {
|
|
|
|
|
// редактируемая колонка
|
|
|
|
|
editable?: boolean
|
|
|
|
|
// react компонента редактора
|
2021-08-30 15:11:21 +05:00
|
|
|
|
input?: ReactNode
|
2021-08-30 10:04:44 +05:00
|
|
|
|
// значение может быть пустым
|
|
|
|
|
isRequired?: boolean
|
|
|
|
|
// css класс для <FormItem/>, если требуется
|
|
|
|
|
formItemClass?: string
|
|
|
|
|
// массив правил валидации значений https://ant.design/components/form/#Rule
|
|
|
|
|
formItemRules?: any[]
|
|
|
|
|
// дефолтное значение при добавлении новой строки
|
|
|
|
|
initialValue?: string|number
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:11:21 +05:00
|
|
|
|
export const makeColumn = (title:string | ReactNode, key:string, other?:columnPropsOther) => ({
|
2021-08-20 10:49:20 +05:00
|
|
|
|
title: title,
|
|
|
|
|
key: key,
|
|
|
|
|
dataIndex: key,
|
|
|
|
|
...other,
|
|
|
|
|
})
|
|
|
|
|
|
2021-08-30 15:11:21 +05:00
|
|
|
|
export const makeColumnsPlanFact = (title:string | ReactNode, key:string|string[], columsOther?:any|any[], gruopOther?:any) =>
|
2021-08-20 10:49:20 +05:00
|
|
|
|
{
|
|
|
|
|
let keyPlanLocal = key
|
|
|
|
|
let keyFactLocal = key
|
|
|
|
|
|
|
|
|
|
if(key instanceof Array){
|
|
|
|
|
keyPlanLocal = key[0]
|
|
|
|
|
keyFactLocal = key[1]
|
|
|
|
|
}else{
|
|
|
|
|
keyPlanLocal = key + 'Plan'
|
|
|
|
|
keyFactLocal = key + 'Fact'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let columsOtherLoacl :any[2]
|
|
|
|
|
if(columsOther instanceof Array)
|
|
|
|
|
columsOtherLoacl = [columsOther[0], columsOther[1]]
|
|
|
|
|
else
|
|
|
|
|
columsOtherLoacl = [columsOther, columsOther]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: title,
|
|
|
|
|
...gruopOther,
|
|
|
|
|
children: [
|
|
|
|
|
makeColumn('план', keyPlanLocal, columsOtherLoacl[0]),
|
|
|
|
|
makeColumn('факт', keyFactLocal, columsOtherLoacl[1]),
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-25 10:54:07 +05:00
|
|
|
|
|
|
|
|
|
export const makeFilterTextMatch = (key: string | number) => (filterValue: string | number, dataItem: any) =>
|
|
|
|
|
dataItem[key] === filterValue
|
|
|
|
|
|
2021-08-30 12:39:46 +05:00
|
|
|
|
export const makeGroupColumn = (title: string, children: object[]) => ({
|
2021-08-25 10:54:07 +05:00
|
|
|
|
title: title,
|
|
|
|
|
children: children,
|
|
|
|
|
})
|
|
|
|
|
|
2021-09-13 12:17:38 +05:00
|
|
|
|
export const makeTextColumn = (
|
|
|
|
|
title: string,
|
|
|
|
|
dataIndex: string,
|
|
|
|
|
filters: object[],
|
|
|
|
|
sorter?: (key: string) => any,
|
|
|
|
|
render?: any,
|
|
|
|
|
other?: any) => ({
|
2021-08-30 12:39:46 +05:00
|
|
|
|
title: title,
|
|
|
|
|
dataIndex: dataIndex,
|
|
|
|
|
key: dataIndex,
|
|
|
|
|
filters: filters,
|
|
|
|
|
onFilter: filters ? makeFilterTextMatch(dataIndex) : null,
|
2021-09-13 12:17:38 +05:00
|
|
|
|
sorter: sorter ?? makeStringSorter(dataIndex),
|
2021-08-30 12:39:46 +05:00
|
|
|
|
render: render,
|
|
|
|
|
...other
|
2021-08-25 10:54:07 +05:00
|
|
|
|
})
|
|
|
|
|
|
2021-08-30 12:39:46 +05:00
|
|
|
|
export const makeNumericColumn = (title: string, dataIndex: string,
|
2021-08-31 16:41:13 +05:00
|
|
|
|
filters: object[], filterDelegate: (key: string | number) => any,
|
2021-10-12 11:10:33 +05:00
|
|
|
|
renderDelegate: (_: any, row: object) => any, width: string, other?: columnPropsOther) => ({
|
2021-08-30 12:39:46 +05:00
|
|
|
|
title: title,
|
|
|
|
|
dataIndex: dataIndex,
|
|
|
|
|
key: dataIndex,
|
|
|
|
|
filters: filters,
|
|
|
|
|
onFilter: filterDelegate ? filterDelegate(dataIndex) : null,
|
|
|
|
|
sorter: makeNumericSorter(dataIndex),
|
2021-08-31 11:55:26 +05:00
|
|
|
|
width: width,
|
2021-10-08 17:02:15 +05:00
|
|
|
|
render: renderDelegate??makeNumericRender(),
|
2021-10-12 11:10:33 +05:00
|
|
|
|
align: 'right',
|
|
|
|
|
...other
|
2021-08-25 10:54:07 +05:00
|
|
|
|
})
|
|
|
|
|
|
2021-08-31 16:41:13 +05:00
|
|
|
|
export const makeNumericColumnPlanFact = (title: string, dataIndex: string, filters: object[],
|
|
|
|
|
filterDelegate: (key: string | number) => any, renderDelegate: (_: any, row: object) => any, width: string) =>
|
2021-08-25 10:54:07 +05:00
|
|
|
|
makeGroupColumn( title, [
|
2021-08-31 16:41:13 +05:00
|
|
|
|
makeNumericColumn('п', dataIndex + 'Plan', filters, filterDelegate, renderDelegate, width),
|
|
|
|
|
makeNumericColumn('ф', dataIndex + 'Fact', filters, filterDelegate, renderDelegate, width),
|
2021-08-25 10:54:07 +05:00
|
|
|
|
])
|
|
|
|
|
|
2021-10-12 11:10:33 +05:00
|
|
|
|
export const makeNumericStartEnd = (
|
|
|
|
|
title: string,
|
|
|
|
|
dataIndex: string,
|
|
|
|
|
editable: boolean,
|
|
|
|
|
filters: object[],
|
|
|
|
|
filterDelegate: (key: string | number) => any,
|
|
|
|
|
renderDelegate: (_: any, row: object) => any,
|
|
|
|
|
width: string,
|
|
|
|
|
) => makeGroupColumn( title, [
|
|
|
|
|
makeNumericColumn('старт', dataIndex + '_start', filters, filterDelegate, renderDelegate, width, {editable}),
|
|
|
|
|
makeNumericColumn('конец', dataIndex + '_end', filters, filterDelegate, renderDelegate, width, {editable})
|
|
|
|
|
])
|
|
|
|
|
|
2021-10-11 16:10:19 +05:00
|
|
|
|
export const makeNumericAvgRange = (
|
|
|
|
|
title: string,
|
|
|
|
|
dataIndex: string,
|
2021-10-12 11:10:33 +05:00
|
|
|
|
editable: boolean,
|
2021-10-11 16:10:19 +05:00
|
|
|
|
filters: object[],
|
|
|
|
|
filterDelegate: (key: string | number) => any,
|
|
|
|
|
renderDelegate: (_: any, row: object) => any,
|
|
|
|
|
width: string
|
|
|
|
|
) => makeGroupColumn( title, [
|
2021-10-12 11:10:33 +05:00
|
|
|
|
makeNumericColumn('мин', dataIndex + '_min', filters, filterDelegate, renderDelegate, width, {editable}),
|
|
|
|
|
makeNumericColumn('сред', dataIndex + '_avg', filters, filterDelegate, renderDelegate, width, {editable}),
|
|
|
|
|
makeNumericColumn('макс', dataIndex + '_max', filters, filterDelegate, renderDelegate, width, {editable})
|
2021-10-11 16:10:19 +05:00
|
|
|
|
])
|
|
|
|
|
|
2021-08-20 10:49:20 +05:00
|
|
|
|
type PaginationContainer = {
|
|
|
|
|
skip?: number;
|
|
|
|
|
take?: number;
|
|
|
|
|
count?: number;
|
|
|
|
|
items?: any[] | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const makePaginationObject = (paginationContainer:PaginationContainer, ...other:any) => {
|
|
|
|
|
let page = 1 + Math.floor((paginationContainer.skip??0) /(paginationContainer.take??1));
|
|
|
|
|
return {
|
|
|
|
|
...other,
|
|
|
|
|
pageSize: paginationContainer.take,
|
|
|
|
|
total: paginationContainer.count ?? paginationContainer.items?.length ?? 0,
|
|
|
|
|
current: page,
|
|
|
|
|
}
|
2021-08-20 12:31:24 +05:00
|
|
|
|
}
|