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

(improvements) Default column widths #810

Merged
merged 26 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
49972f8
Merge pull request #807 from bitfinexcom/staging
prdn Apr 17, 2024
eb4aa14
Implement columnHeaderCellRenderer
alexstotsky Apr 15, 2024
7b14b9f
Fix columns header naming providing
alexstotsky Apr 15, 2024
2560abd
Fix table menu representation
alexstotsky Apr 15, 2024
6c43d8c
Extract columnWidthReset handler for better reusability
alexstotsky Apr 15, 2024
300b990
Optimize columnWidthReset handler
alexstotsky Apr 15, 2024
ddcb0c6
Improve header cell name providing
alexstotsky Apr 15, 2024
c324afb
Adjust menu availability only for resizable tables
alexstotsky Apr 15, 2024
5b40051
Minor fix
alexstotsky Apr 19, 2024
5845207
Fix headers names alignment
alexstotsky Apr 23, 2024
568c731
Lint fix
alexstotsky Apr 23, 2024
037e80b
Implement getColumnsMinWidths helper
alexstotsky Apr 19, 2024
39bcf48
Fix columns util export
alexstotsky Apr 22, 2024
d8df559
Update first cols defult calculations
alexstotsky Apr 22, 2024
0f5bd28
Implement getIsDefaultsWiderThanContainer checker utility
alexstotsky Apr 22, 2024
2e7b6ee
Cleanup
alexstotsky Apr 22, 2024
babacc1
Implement wide/small columns checking flow
alexstotsky Apr 22, 2024
7fee017
Update average width calc flow
alexstotsky Apr 23, 2024
a5d81d9
Add small/wide cols precalculation flow
alexstotsky Apr 23, 2024
cab700e
Implement columns widths adjustments flow
alexstotsky Apr 23, 2024
44faf57
Adjust small multiplier
alexstotsky Apr 23, 2024
24fc562
Enhance average width calcs flow
alexstotsky Apr 23, 2024
0002980
Cleanup
alexstotsky Apr 23, 2024
bb47b52
Optimize avg width calculation flow
alexstotsky Apr 24, 2024
efb18bc
Adjust defaults
alexstotsky Apr 24, 2024
4046d15
Minor tweak
alexstotsky Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 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,33 @@ const DataTable = ({

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

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

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

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

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

return (
<Menu>
<CopyCellsMenuItem
Expand Down Expand Up @@ -249,6 +273,12 @@ 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),
column?.className ?? 'align-right',
)
}
/>
))}
</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
49 changes: 36 additions & 13 deletions src/utils/columns.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react'
import _map from 'lodash/map'
import _sum from 'lodash/sum'
import _size from 'lodash/size'
import _head from 'lodash/head'
import _fill from 'lodash/fill'
import _floor from 'lodash/floor'
import _filter from 'lodash/filter'
import _forEach from 'lodash/forEach'
import { Cell } from '@blueprintjs/table'
import { get, pick, isEqual } from '@bitfinex/lib-js-util-base'

Expand Down Expand Up @@ -53,7 +58,7 @@ const COLUMN_WIDTH_STANDARD = {
marginFunding: 132,
marginFundingType: 130,
marginFundingPayment: 178,
moreDetails: 95,
moreDetails: 130,
meta: 160,
merchantName: 120,
mobile: 90,
Expand All @@ -77,13 +82,13 @@ const COLUMN_WIDTH_STANDARD = {
priceLiq: 132,
priceSpot: 132,
priceTrailing: 132,
rate: 120,
rate: 130,
redirectUrl: 300,
sale: 152,
side: 100,
sellingAmount: 132,
sellingWeightedPrice: 160,
status: 105,
status: 135,
symbol: 92,
swap: 132,
time: 150,
Expand Down Expand Up @@ -620,24 +625,42 @@ export const formatSumUpValue = value => {
return parseFloat(value).toFixed(8).replace(/\d(?=(\d{3})+\.)/g, '$&,')
}

export const MIN_COLUMN_WIDTH = 100

export const MIN_COLUMN_WIDTH = 125
export const WIDE_COLUMN_DEFAULT_WIDTH = 300
export const DEFAULT_CONTAINER_WIDTH = 1000

export const getColumnsMinWidths = (columns) => _map(columns,
(column) => COLUMN_WIDTHS?.[column.id] ?? MIN_COLUMN_WIDTH)

export const getAverageWidth = (avgWidth) => (avgWidth < MIN_COLUMN_WIDTH
? MIN_COLUMN_WIDTH
: avgWidth)

export const getCalculatedColumnWidths = (columns, containerWidth) => {
if (columns.length === 0) {
if (_size(columns) === 0) {
return []
}

const avgWidth = Math.floor(containerWidth / columns.length)
if (avgWidth < MIN_COLUMN_WIDTH) {
return _map(columns, () => MIN_COLUMN_WIDTH)
}
const colsDefaults = getColumnsMinWidths(columns)
let avgWidth = getAverageWidth(_floor(containerWidth / _size(columns)))
const preparedColsWidths = _fill(Array(_size(columns)), 0)
const wideColsIndexes = []
const smallColsIndexes = []

_forEach(colsDefaults, (value, index) => {
if (value < MIN_COLUMN_WIDTH) smallColsIndexes.push(index)
if (value > WIDE_COLUMN_DEFAULT_WIDTH) wideColsIndexes.push(index)
})
_forEach(wideColsIndexes, (colIndex) => { preparedColsWidths[colIndex] = _floor(avgWidth * 2) })
_forEach(smallColsIndexes, (colIndex) => { preparedColsWidths[colIndex] = _floor(avgWidth * 0.7) })

const columnWidths = _map(columns, () => avgWidth)
columnWidths[0] = containerWidth - ((columns.length - 1) * avgWidth)
const calculatedCols = _size(smallColsIndexes) + _size(wideColsIndexes)
avgWidth = getAverageWidth(
_floor((containerWidth - _sum(preparedColsWidths)) / (_size(columns) - calculatedCols)),
)
_forEach(preparedColsWidths, (value, index) => { if (isEqual(value, 0)) preparedColsWidths[index] = avgWidth })

return columnWidths
return preparedColsWidths
}

export default {
Expand Down