Skip to content

fix(textarea): ensure viewport content is set during update #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v2-exp
Choose a base branch
from
Open
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
26 changes: 21 additions & 5 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.Err = msg
}

// Make sure we set the content of the viewport before updating it.
view := m.view()
m.viewport.SetContent(view)
vp, cmd := m.viewport.Update(msg)
m.viewport = &vp
cmds = append(cmds, cmd)
Expand All @@ -1257,9 +1260,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

// View renders the text area in its current state.
func (m Model) View() string {
if m.Value() == "" && m.row == 0 && m.col == 0 && m.Placeholder != "" {
func (m *Model) view() string {
if len(m.Value()) == 0 && m.row == 0 && m.col == 0 && m.Placeholder != "" {
return m.placeholderView()
}
m.virtualCursor.TextStyle = m.activeStyle().computedCursorLine()
Expand Down Expand Up @@ -1352,8 +1354,22 @@ func (m Model) View() string {
s.WriteRune('\n')
}

m.viewport.SetContent(s.String())
return styles.Base.Render(m.viewport.View())
return s.String()
}

// View renders the text area in its current state.
func (m Model) View() string {
view := strings.TrimSpace(m.viewport.View())
if view == "" {
// XXX: This is a workaround for the case where the viewport hasn't
// been initialized yet like during the initial render. In that case,
// we need to render the view again because Update hasn't been called
// yet to set the content of the viewport.
m.viewport.SetContent(m.view())
view = m.viewport.View()
}
styles := m.activeStyle()
return styles.Base.Render(view)
}

// promptView renders a single line of the prompt.
Expand Down
Loading