Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions R/columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
#' Cells in the row names column are automatically marked up as row headers.
#' @param minWidth Minimum width of the column in pixels. Defaults to 100.
#' @param maxWidth Maximum width of the column in pixels.
#' @param initWidth Initial width of the column in pixels. Cannot be specified
#' if `width` is specified.
#' @param width Fixed width of the column in pixels. Overrides `minWidth` and `maxWidth`.
#' @param align Horizontal alignment of content in the column. One of
#' `"left"`, `"right"`, `"center"`. By default, all numbers are right-aligned,
Expand Down Expand Up @@ -142,6 +144,7 @@ colDef <- function(
rowHeader = FALSE,
minWidth = 100,
maxWidth = NULL,
initWidth = NULL,
width = NULL,
align = NULL,
vAlign = NULL,
Expand Down Expand Up @@ -256,6 +259,22 @@ colDef <- function(
stop("`maxWidth` must be numeric")
}

if (!is.null(initWidth) && !is.numeric(initWidth)) {
stop("`initWidth` must be numeric")
}

if (!is.null(initWidth) && !is.null(width)) {
stop("`initWidth` cannot be specified if `width` is specified")
}

if (!is.null(initWidth) && !is.null(minWidth) && initWidth < minWidth) {
stop("`initWidth` must be greater than or equal to `minWidth`")
}

if (!is.null(initWidth) && !is.null(maxWidth) && initWidth > maxWidth) {
stop("`initWidth` must be less than or equal to `maxWidth`")
}

if (!is.null(width) && !is.numeric(width)) {
stop("`width` must be numeric")
}
Expand Down Expand Up @@ -338,6 +357,7 @@ colDef <- function(
rowHeader = if ("rowHeader" %in% userArgs) rowHeader,
minWidth = if ("minWidth" %in% userArgs) minWidth,
maxWidth = maxWidth,
initWidth = initWidth,
width = width,
align = align,
vAlign = vAlign,
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.server.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/reactable.server.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions man/colDef.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions srcjs/__tests__/columns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ describe('buildColumnDefs', () => {
expect(cols[0].maxWidth).toEqual(111)
expect(cols[0].width).toEqual(111)
expect(cols[0].disableResizing).toEqual(true)

// Initial width sets width, does not affect min or max width, leaves column resizable
cols = buildColumnDefs([{ id: 'x', initWidth: 120, minWidth: 100, maxWidth: 200, resizable: true }])
expect(cols[0].width).toEqual(120)
expect(cols[0].minWidth).toEqual(100)
expect(cols[0].maxWidth).toEqual(200)
expect(cols[0].resizable).toEqual(true)
expect(cols[0].disableResizing).toEqual(false)
})

test('header sort icons', () => {
Expand Down
4 changes: 2 additions & 2 deletions srcjs/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ export function buildColumnDefs(columns, groups, tableProps = {}) {
// maxWidth takes priority over minWidth
col.minWidth = Math.min(col.minWidth, col.maxWidth)

// Start column width at min width / flex width, like in v6
col.width = col.minWidth
// Start column width at initial width, otherwise at min width / flex width, like in v6
col.width = col.initWidth || col.minWidth

col.resizable = getFirstDefined(col.resizable, resizable)
// Disable resizing on fixed width columns
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ test_that("colDef minWidth", {
expect_equal(colDef(minWidth = 100)$minWidth, 100)
})

test_that("colDef initWidth", {
expect_error(colDef(initWidth = "auto"), "`initWidth` must be numeric")
expect_equal(colDef()$initWidth, NULL)
expect_equal(colDef(initWidth = 150)$initWidth, 150)
expect_error(colDef(width = 100, initWidth = 120), "`initWidth` cannot be specified if `width` is specified")
expect_error(colDef(initWidth = 50), "`initWidth` must be greater than or equal to `minWidth`")
expect_error(colDef(maxWidth = 200, initWidth = 250), "`initWidth` must be less than or equal to `maxWidth`")
})

test_that("colDef vAlign", {
expect_error(colDef(vAlign = TRUE), '`vAlign` must be one of "top", "center", "bottom"')
expect_null(colDef()$vAlign)
Expand Down