asb_cloud_front/src/components/Table/index.ts

179 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-08-20 12:31:24 +05:00
export { Table } from 'antd'
export { EditableTable } from './EditableTable'
export { DatePickerWrapper } from './DatePickerWrapper'
2021-08-20 12:31:24 +05:00
export { SelectFromDictionary } from './SelectFromDictionary'
export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/
export const formatDate='YYYY.MM.DD HH:mm'
export const numericColumnOptions = {
editable: true,
initialValue: 0,
width:100,
formItemRules: [
{
required: true,
message: `Введите число`,
pattern: RegExpIsFloat,
},
],
};
/*
other - объект с дополнительными свойствами колонки
поддерживаются все базовые свойства из описания https://ant.design/components/table/#Column
плю дополнительные для колонок EditableTable:
editable - редактируемая колонка, bool
input - react компонента редактора (<Input/>, <InputNumber/>, <DatePickerWrapper/>...)
isRequired - значение может быть пустым,
formItemClass - css класс для <FormItem/>, если требуется
formItemRules - массив правил валидации значений https://ant.design/components/form/#Rule,
initialValue - дефолтное значение при добавлении новой строки
*/
export const makeColumn = (title:string, key:string, other?:any) => ({
title: title,
key: key,
dataIndex: key,
...other,
})
export const makeColumnsPlanFact = (title:string, key:string|string[], columsOther?:any|any[], gruopOther?:any) =>
{
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]),
]
}
}
const maxPrefix = "isMax"
const minPrefix = "isMin"
export const makeFilterMinMaxFunction = (key: string | number) => (filterValue: string | number,
dataItem: any) =>
filterValue === "max"
? dataItem[maxPrefix + key]
: filterValue === "min"
? dataItem[minPrefix + key]
: false
export const makeFilterTextMatch = (key: string | number) => (filterValue: string | number, dataItem: any) =>
dataItem[key] === filterValue
export const makeNumericSorter = (key: any) => (a: any, b: any) => a[key] - b[key]
export const makeStringSorter = (key: any) => (a: any, b: any) =>
{
for (let i = 0; i < a.length; i++) {
if (isNaN(b.charCodeAt(i)) || (a.charCodeAt(i) > b.charCodeAt(i)))
return 1
if (a.charCodeAt(i) > b.charCodeAt(i))
return -1
}
return 0
}
export const makeGroupColumn = (title: any, children: any) => ({
title: title,
children: children,
})
export const makeTextColumn = (title: any, dataIndex: any, filters: any, sorter: any, render: any, other: any) => ({
title: title,
dataIndex: dataIndex,
key: dataIndex,
filters: filters,
onFilter: filters ? makeFilterTextMatch(dataIndex) : null,
sorter: sorter ? makeStringSorter(dataIndex) : null,
render: render,
...other
})
export const makeNumericColumn = (title: any, dataIndex: any, filters: any, width: string) => ({
title: title,
dataIndex: dataIndex,
key: dataIndex,
filters: filters,
onFilter: makeFilterMinMaxFunction(dataIndex),
sorter: makeNumericSorter(dataIndex),
width: width
})
2021-08-26 17:45:36 +05:00
export const makeNumericColumnPlanFact = (title: any, dataIndex: any, filters: any, width: string) =>
makeGroupColumn( title, [
2021-08-26 17:45:36 +05:00
makeNumericColumn('п', dataIndex + 'Plan', filters, width),
makeNumericColumn('ф', dataIndex + 'Fact', filters, width),
])
export const calcAndUpdateStats = (data: any, keys: any) => {
let mins: any = {}
let maxs: any = {}
keys.forEach((key: any) => {
maxs[key] = Number.MIN_VALUE
mins[key] = Number.MAX_VALUE
})
data.forEach((item: any) => {
keys.forEach((key: any) => {
if (mins[key] > item[key]) mins[key] = item[key]
if (maxs[key] < item[key]) maxs[key] = item[key]
})
})
for (let i = 0; i < data.length; i++) {
keys.forEach((key: any) => {
data[i][maxPrefix + key] = data[i][key] === maxs[key]
data[i][minPrefix + key] = data[i][key] === mins[key]
})
}
}
export const calcAndUpdateStatsBySections = (data: any, keys: any) => {
const sectionTypes = new Set()
data.forEach((item: any) => sectionTypes.add(item.sectionType))
sectionTypes.forEach(sectionType => {
const filteredBySectionData = data.filter((item: any) => item.sectionType === sectionType)
calcAndUpdateStats(filteredBySectionData, keys)
})
}
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
}