Skip to content

Commit

Permalink
feat: resolved navbar options equivalent to navbar_options()
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Dec 2, 2024
1 parent 963a1eb commit b163ffa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
25 changes: 16 additions & 9 deletions R/navs-legacy.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ navbar_options <- function(
)

structure(
dropNulls(opts),
opts,
class = c("bslib_navbar_options", "list"),
is_default = is_default,
waldo_opts = list(ignore_attr = TRUE)
Expand Down Expand Up @@ -270,14 +270,17 @@ navbar_options_resolve_deprecated <- function(
# options. We take the direct option if the user option is a default value,
# warning if otherwise ignored.
# TODO-deprecated: Remove this and warning when direct options are hard-deprecated
ignored <- c()
is_default <- attr(options_user, "is_default") %||% list()
keep_user_values <- vapply(
names(options_user),
function(x) !isTRUE(is_default[[x]]),
logical(1)
)
options_user <- options_user[keep_user_values]

ignored <- c()
for (opt in names(options_old)) {
can_use_direct <-
!opt %in% names(options_user) ||
isTRUE(is_default[[opt]])

if (can_use_direct) {
if (!opt %in% names(options_user)) {
options_user[[opt]] <- options_old[[opt]]
} else if (!identical(options_old[[opt]], options_user[[opt]])) {
ignored <- c(ignored, opt)
Expand Down Expand Up @@ -309,12 +312,16 @@ print.bslib_navbar_options <- function(x, ...) {
return(invisible(x))
}

fields <- names(dropNulls(x))
fields <- names(x)
opt_w <- max(nchar(fields))
is_default <- attr(x, "is_default") %||% list()
for (opt in fields) {
value <- x[[opt]]
value <- x[[opt]] %||% "NULL"
if (isTRUE(is_default[[opt]])) {
if (identical(value, "NULL")) {
# Skip printing default NULL values
next
}
value <- sprintf("(%s)", value)
}
cat(sprintf("%*s", opt_w, opt), ": ", value, "\n", sep = "")
Expand Down
48 changes: 48 additions & 0 deletions tests/testthat/test-navs-legacy.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,54 @@ test_that("navbar_options() print method", {
)
})

test_that("navbar_options_resolve_deprecated() consolidates correctly", {
# TODO-deprecated: Remove when direction options are deprecated with an error

# deprecation messages are handled through other tests
rlang::local_options(lifecycle_verbosity = "quiet")

expect_equal(
navbar_options_resolve_deprecated(navbar_options(), bg = "red")$bg,
"red"
)

expect_equal(
navbar_options_resolve_deprecated(list(), bg = "red")$bg,
"red"
)

expect_warning(
expect_equal(
navbar_options_resolve_deprecated(navbar_options(bg = "blue"), bg = "red")$bg,
"blue"
)
)

expect_warning(
expect_equal(
navbar_options_resolve_deprecated(list(bg = "blue"), bg = "red")$bg,
"blue"
)
)

expect_warning(
expect_null(
navbar_options_resolve_deprecated(navbar_options(bg = NULL), bg = "red")$bg
)
)

expect_warning(
expect_null(
navbar_options_resolve_deprecated(list(bg = NULL), bg = "red")$bg
)
)

expect_equal(
attr(navbar_options(underline = FALSE), "is_default"),
attr(navbar_options_resolve_deprecated(underline = FALSE), "is_default")
)
})

test_that("navset_bar() warns if using deprecated args", {
lifecycle::expect_deprecated(
navset_bar(position = "fixed-top")
Expand Down

0 comments on commit b163ffa

Please sign in to comment.