2022-03-02 21:17:27 +05:00
|
|
|
|
import { memo, useCallback, useEffect, useState } from 'react'
|
|
|
|
|
import { ColumnsType } from 'antd/lib/table'
|
|
|
|
|
import { Button, Modal, Switch, Table } from 'antd'
|
|
|
|
|
import { SettingOutlined } from '@ant-design/icons'
|
|
|
|
|
|
2022-06-09 17:51:41 +05:00
|
|
|
|
import { TableColumnSettings, makeTableSettings, mergeTableSettings, TableSettings } from '@utils'
|
2022-03-02 21:17:27 +05:00
|
|
|
|
import { TableColumns } from './Table'
|
|
|
|
|
import { makeColumn } from '.'
|
|
|
|
|
|
2022-06-09 17:51:41 +05:00
|
|
|
|
const parseSettings = (columns?: TableColumns, settings?: TableSettings | null): TableColumnSettings[] => {
|
|
|
|
|
const newSettings = mergeTableSettings(makeTableSettings(columns ?? []), settings ?? {})
|
2022-03-02 21:17:27 +05:00
|
|
|
|
return Object.values(newSettings).map((set, i) => ({ ...set, key: i }))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 17:51:41 +05:00
|
|
|
|
const unparseSettings = (columns: TableColumnSettings[]): TableSettings =>
|
2022-03-02 21:17:27 +05:00
|
|
|
|
Object.fromEntries(columns.map((column) => [column.columnName, column]))
|
|
|
|
|
|
|
|
|
|
export type TableSettingsChangerProps = {
|
|
|
|
|
title?: string
|
|
|
|
|
columns?: TableColumns
|
|
|
|
|
settings?: TableSettings | null
|
|
|
|
|
onChange: (settings: TableSettings | null) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const TableSettingsChanger = memo<TableSettingsChangerProps>(({ title, columns, settings, onChange }) => {
|
|
|
|
|
const [visible, setVisible] = useState<boolean>(false)
|
2022-06-09 17:51:41 +05:00
|
|
|
|
const [newSettings, setNewSettings] = useState<TableColumnSettings[]>(parseSettings(columns, settings))
|
|
|
|
|
const [tableColumns, setTableColumns] = useState<ColumnsType<TableColumnSettings>>([])
|
2022-03-02 21:17:27 +05:00
|
|
|
|
|
|
|
|
|
const onVisibilityChange = useCallback((index: number, visible: boolean) => {
|
|
|
|
|
setNewSettings((oldSettings) => {
|
|
|
|
|
const newSettings = [...oldSettings]
|
|
|
|
|
newSettings[index].visible = visible
|
|
|
|
|
return newSettings
|
|
|
|
|
})
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const toogleAll = useCallback((show: boolean) => {
|
|
|
|
|
setNewSettings((oldSettings) => oldSettings.map((column) => {
|
|
|
|
|
column.visible = show
|
|
|
|
|
return column
|
|
|
|
|
}))
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setTableColumns([
|
|
|
|
|
makeColumn('Название', 'title'),
|
|
|
|
|
makeColumn(null, 'visible', {
|
|
|
|
|
title: () => (
|
|
|
|
|
<>
|
|
|
|
|
Показать
|
|
|
|
|
<Button type={'link'} onClick={() => toogleAll(true)}>Показать все</Button>
|
|
|
|
|
</>
|
|
|
|
|
),
|
2022-06-09 17:51:41 +05:00
|
|
|
|
render: (visible: boolean, _?: TableColumnSettings, index: number = NaN) => (
|
2022-03-02 21:17:27 +05:00
|
|
|
|
<Switch
|
|
|
|
|
checked={visible}
|
|
|
|
|
checkedChildren={'Отображён'}
|
|
|
|
|
unCheckedChildren={'Скрыт'}
|
|
|
|
|
onChange={(visible) => onVisibilityChange(index, visible)}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
}, [toogleAll, onVisibilityChange])
|
|
|
|
|
|
|
|
|
|
useEffect(() => setNewSettings(parseSettings(columns, settings)), [columns, settings])
|
|
|
|
|
|
|
|
|
|
const onModalOk = useCallback(() => {
|
|
|
|
|
onChange(unparseSettings(newSettings))
|
|
|
|
|
setVisible(false)
|
|
|
|
|
}, [newSettings, onChange])
|
|
|
|
|
|
|
|
|
|
const onModalCancel = useCallback(() => {
|
|
|
|
|
setNewSettings(parseSettings(columns, settings))
|
|
|
|
|
setVisible(false)
|
|
|
|
|
}, [columns, settings])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
centered
|
|
|
|
|
visible={visible}
|
|
|
|
|
onCancel={onModalCancel}
|
|
|
|
|
onOk={onModalOk}
|
|
|
|
|
title={title ?? 'Настройка отображения таблицы'}
|
|
|
|
|
width={1000}
|
|
|
|
|
>
|
|
|
|
|
<Table columns={tableColumns} dataSource={newSettings} />
|
|
|
|
|
</Modal>
|
|
|
|
|
<Button size={'small'} style={{ position: 'absolute', left: 0, top: 0, opacity: .5 }} type={'link'} onClick={() => setVisible(true)} icon={<SettingOutlined />}/>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default TableSettingsChanger
|