Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feature) Reset column widths via the headers context menu #808

Merged
merged 11 commits into from
May 8, 2024
31 changes: 25 additions & 6 deletions src/ui/DataTable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import React, {
useMemo,
useState,
useEffect,
useCallback,
} from 'react'
import { useTranslation } from 'react-i18next'
import { useDispatch, useSelector } from 'react-redux'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { Menu } from '@blueprintjs/core'
import { Menu, MenuItem } from '@blueprintjs/core'
import {
Column,
ColumnHeaderCell,
CopyCellsMenuItem,
Table,
} from '@blueprintjs/table'
Expand Down Expand Up @@ -84,6 +86,27 @@ const DataTable = ({

const getCellData = (rowIndex, columnIndex) => tableColumns[columnIndex]?.copyText(rowIndex)

const columnWidthReset = useCallback(() => {
setUseCustomColsWidth(false)
dispatch(setColumnsWidth({ section }))
}, [dispatch])

const columnHeaderCellRenderer = (name) => {
const menuRenderer = () => (
<>
{enableColumnResizing && (
<Menu>
<MenuItem
onClick={columnWidthReset}
text={t('column.defaultWidth')}
/>
</Menu>
)}
</>
)
return <ColumnHeaderCell name={name} menuRenderer={menuRenderer} />
}

const renderBodyContextMenu = (context) => {
const isSingleColumnSelected = singleColumnSelectedCheck(context)
const hasNumericValue = columnHasNumericValueCheck(context, tableColumns)
Expand All @@ -99,11 +122,6 @@ const DataTable = ({
}
}

const columnWidthReset = () => {
setUseCustomColsWidth(false)
dispatch(setColumnsWidth({ section }))
}

return (
<Menu>
<CopyCellsMenuItem
Expand Down Expand Up @@ -249,6 +267,7 @@ const DataTable = ({
cellRenderer={column.renderer}
className={column?.className ?? 'align-right'}
name={column.nameStr ? column.nameStr : t(column.name)}
columnHeaderCellRenderer={() => columnHeaderCellRenderer(column?.nameStr ?? t(column.name))}
/>
))}
</Table>
Expand Down
10 changes: 10 additions & 0 deletions src/ui/DataTable/_DataTable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
.cell-no-data {
color: var(--color2);
}

.bp3-table-thead {
.bp3-table-header {
.bp3-table-column-name {
.bp3-table-th-menu-container{
display: none;
}
}
}
}
}

.bp3-table {
Expand Down