Skip to content

Commit 06a43c1

Browse files
committed
feat(colDef): add initWidth argument
1 parent 17ed43e commit 06a43c1

9 files changed

Lines changed: 49 additions & 8 deletions

File tree

R/columns.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
#' Cells in the row names column are automatically marked up as row headers.
6666
#' @param minWidth Minimum width of the column in pixels. Defaults to 100.
6767
#' @param maxWidth Maximum width of the column in pixels.
68+
#' @param initWidth Initial width of the column in pixels. Cannot be specified
69+
#' if `width` is specified.
6870
#' @param width Fixed width of the column in pixels. Overrides `minWidth` and `maxWidth`.
6971
#' @param align Horizontal alignment of content in the column. One of
7072
#' `"left"`, `"right"`, `"center"`. By default, all numbers are right-aligned,
@@ -78,7 +80,7 @@
7880
#'
7981
#' If a sticky column is in a column group, all columns in the group will
8082
#' automatically be made sticky, including the column group header.
81-
#'
83+
#'
8284
#' Sticky columns do not work if `fullWidth` is set to `FALSE` in `reactable()`.
8385
#' @param class Additional CSS classes to apply to cells. Can also be an R function
8486
#' that takes the cell value, row index, and column name as arguments, or a [JS()]
@@ -142,6 +144,7 @@ colDef <- function(
142144
rowHeader = FALSE,
143145
minWidth = 100,
144146
maxWidth = NULL,
147+
initWidth = NULL,
145148
width = NULL,
146149
align = NULL,
147150
vAlign = NULL,
@@ -256,6 +259,22 @@ colDef <- function(
256259
stop("`maxWidth` must be numeric")
257260
}
258261

262+
if (!is.null(initWidth) && !is.numeric(initWidth)) {
263+
stop("`initWidth` must be numeric")
264+
}
265+
266+
if (!is.null(initWidth) && !is.null(width)) {
267+
stop("`initWidth` cannot be specified if `width` is specified")
268+
}
269+
270+
if (!is.null(initWidth) && !is.null(minWidth) && initWidth < minWidth) {
271+
stop("`initWidth` must be greater than or equal to `minWidth`")
272+
}
273+
274+
if (!is.null(initWidth) && !is.null(maxWidth) && initWidth > maxWidth) {
275+
stop("`initWidth` must be less than or equal to `maxWidth`")
276+
}
277+
259278
if (!is.null(width) && !is.numeric(width)) {
260279
stop("`width` must be numeric")
261280
}
@@ -338,6 +357,7 @@ colDef <- function(
338357
rowHeader = if ("rowHeader" %in% userArgs) rowHeader,
339358
minWidth = if ("minWidth" %in% userArgs) minWidth,
340359
maxWidth = maxWidth,
360+
initWidth = initWidth,
341361
width = width,
342362
align = align,
343363
vAlign = vAlign,
@@ -448,7 +468,7 @@ colGroup <- function(
448468
))
449469

450470
group <- tryCatch({
451-
do.call(colDef, args)
471+
do.call(colDef, args)
452472
}, error = function(e) e)
453473

454474
if (inherits(group, "error")) {

inst/htmlwidgets/reactable.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/reactable.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/reactable.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/reactable.server.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/colDef.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/__tests__/columns.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ describe('buildColumnDefs', () => {
625625
expect(cols[0].maxWidth).toEqual(111)
626626
expect(cols[0].width).toEqual(111)
627627
expect(cols[0].disableResizing).toEqual(true)
628+
629+
// Initial width sets width, does not affect min or max width, leaves column resizable
630+
cols = buildColumnDefs([{ id: 'x', initWidth: 120, minWidth: 100, maxWidth: 200, resizable: true }])
631+
expect(cols[0].width).toEqual(120)
632+
expect(cols[0].minWidth).toEqual(100)
633+
expect(cols[0].maxWidth).toEqual(200)
634+
expect(cols[0].resizable).toEqual(true)
635+
expect(cols[0].disableResizing).toEqual(false)
628636
})
629637

630638
test('header sort icons', () => {

srcjs/columns.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ export function buildColumnDefs(columns, groups, tableProps = {}) {
208208
// maxWidth takes priority over minWidth
209209
col.minWidth = Math.min(col.minWidth, col.maxWidth)
210210

211-
// Start column width at min width / flex width, like in v6
212-
col.width = col.minWidth
211+
// Start column width at initial width, otherwise at min width / flex width, like in v6
212+
col.width = col.initWidth || col.minWidth
213213

214214
col.resizable = getFirstDefined(col.resizable, resizable)
215215
// Disable resizing on fixed width columns

tests/testthat/test-columns.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ test_that("colDef minWidth", {
185185
expect_equal(colDef(minWidth = 100)$minWidth, 100)
186186
})
187187

188+
test_that("colDef initWidth", {
189+
expect_error(colDef(initWidth = "auto"), "`initWidth` must be numeric")
190+
expect_equal(colDef()$initWidth, NULL)
191+
expect_equal(colDef(initWidth = 150)$initWidth, 150)
192+
expect_error(colDef(width = 100, initWidth = 120), "`initWidth` cannot be specified if `width` is specified")
193+
expect_error(colDef(initWidth = 50), "`initWidth` must be greater than or equal to `minWidth`")
194+
expect_error(colDef(maxWidth = 200, initWidth = 250), "`initWidth` must be less than or equal to `maxWidth`")
195+
})
196+
188197
test_that("colDef vAlign", {
189198
expect_error(colDef(vAlign = TRUE), '`vAlign` must be one of "top", "center", "bottom"')
190199
expect_null(colDef()$vAlign)

0 commit comments

Comments
 (0)