grapp-dev / nui-components.nvim

A feature-rich and highly customizable library for creating user interfaces in Neovim.
https://nui-components.grapp.dev
MIT License
330 stars 7 forks source link

bug: n.select `on_select` field returns nil when node clearly exists #37

Closed vague2k closed 6 months ago

vague2k commented 6 months ago

I'm trying to incorporate this library into my own plugin, but with the following file.

local function tonode(themes)
  local nodes = {}
  for _, theme in pairs(themes) do
    local node = n.option(theme, { name = theme })
    table.insert(nodes, node)
  end
  return nodes
end

-- TODO: let create_renderer to take in a function or a table of acceptable values
local renderer = n.create_renderer({
  width = 40,
  height = vim.api.nvim_win_get_height(0),
  relative = "editor",
  -- position starts from the left corner
  position = {
    row = 0,
    col = vim.api.nvim_win_get_width(0) - 3,
  },
})

local body = n.columns(n.rows(
  { flex = 2 },
  n.prompt({
    autofocus = true,
    prefix = " ::: ",
    size = 1,
    border_label = {
      text = "󰌁 Huez",
      align = "center",
    },
  }),

  n.select({
    flex = 1,
    autofocus = false,
    border_label = "Themes",
    data = tonode(api.get_installed_themes(vim.g.huez_config.exclude)), -- this api call return's a list of strings which are then cast to nodes
    on_change = function(theme)
      vim.cmd("colorscheme " .. theme.name)
    end,
    on_select = function(theme)
      api.save_colorscheme(theme.name) -- <--- the problem is here, and I've already tested that this api call works as intended in my testing suite.
      renderer:close()
      utils.log_info("Selected " .. theme.name)
    end,
  })
))

The on_change function works completely fine giving me the impression that theme.name is not nil. BUT when I select a color scheme, the on_select function will print me theme.name prints nil, I have already tested that my api's work, I'm wondering if there's anything I'm doing that's particularly wrong.

steps to reproduce

  1. create a function strings that returns a list of strings
  2. create a function to_node that takes in a list of strings as a arg and converts each string to a n.option returning an array of NuiTreeNodes
  3. copy paste the above code and replace data = ... with data = to_node(strings)
  4. Source the file and trying selecting an option
mobily commented 6 months ago

@vague2k thanks for posting this issue! fixed in v1.5.1

vague2k commented 6 months ago

thanks for the quick fix, i really appreciate it 👍🏼