Skip to content

Commit

Permalink
Merge pull request #35 from grapp-dev/fix/text-input-value
Browse files Browse the repository at this point in the history
fix: display the initial value of TextInput correctly
  • Loading branch information
mobily authored Apr 19, 2024
2 parents 0d1058a + 724d7cc commit 657cae2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/pages/docs/component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,15 @@ Where `Mapping` is:
returns="boolean"
/>

#### is_first_render

> Determines whether the component is rendered for the first time or not.
<Method
name="is_first_render"
returns="boolean"
/>

#### get_id

> Returns the unique identifier for the current component instance.
Expand Down
8 changes: 7 additions & 1 deletion lua/nui-components/component/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function Component:on_renderer_initialization(renderer, parent, children)
self._private.renderer = renderer
self._private.children = children
self._private.props.children = nil
self._private.is_first_render = true

self._private.props.instance:on_next(function(_, key)
local is_layout_prop = fn.isome(layout_props, function(prop)
Expand Down Expand Up @@ -131,7 +132,7 @@ function Component:mount()
self:_attach_mappings()
self:_set_initial_focus()

self:redraw()
self:on_update()

vim.schedule(function()
props.on_mount(self)
Expand All @@ -155,6 +156,7 @@ end

function Component:redraw()
self:on_update()
self._private.is_first_render = false
end

function Component:_default_prop_types()
Expand Down Expand Up @@ -435,6 +437,10 @@ function Component:is_mounted()
return self._.mounted
end

function Component:is_first_render()
return self._private.is_first_render
end

function Component:get_id()
return self._private.id
end
Expand Down
2 changes: 1 addition & 1 deletion lua/nui-components/text-input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function TextInput:on_update()

if not (current_winid == self.winid and mode == "i") then
vim.schedule(function()
if self:_is_next_line_allowed() then
if self:_is_next_line_allowed() or self:is_first_render() then
local lines = self:get_lines()
vim.api.nvim_buf_set_lines(self.bufnr, 0, #lines, false, lines)
end
Expand Down

0 comments on commit 657cae2

Please sign in to comment.