From 9fdbbd4250de5abd452b1adbc94a1a754b8ed618 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 1 Oct 2023 22:46:44 +0900 Subject: [PATCH] fix weird behavior on empty placeholder --- src/textarea.rs | 6 +++--- src/widget.rs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/textarea.rs b/src/textarea.rs index e25c7f7..2b4a703 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -61,7 +61,7 @@ pub struct TextArea<'a> { #[cfg(feature = "search")] search: Search, alignment: Alignment, - pub(crate) placeholder: Option, + pub(crate) placeholder: String, pub(crate) placeholder_style: Style, } @@ -160,7 +160,7 @@ impl<'a> TextArea<'a> { #[cfg(feature = "search")] search: Search::default(), alignment: Alignment::Left, - placeholder: None, + placeholder: String::new(), placeholder_style: Style::default().fg(Color::DarkGray), } } @@ -1240,7 +1240,7 @@ impl<'a> TextArea<'a> { /// sets the placeholder text pub fn set_placeholder(&mut self, placeholder: String) { - self.placeholder = Some(placeholder); + self.placeholder = placeholder; } pub fn set_placeholder_style(&mut self, style: Style) { diff --git a/src/widget.rs b/src/widget.rs index cb75d6e..bd8d06a 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -119,11 +119,11 @@ impl<'a> Widget for Renderer<'a> { let top_row = next_scroll_top(top_row, cursor.0 as u16, height); let top_col = next_scroll_top(top_col, cursor.1 as u16, width); - let (text, style) = match &self.0.placeholder { - Some(text) if self.0.is_empty() => { - (Text::from(text.as_str()), self.0.placeholder_style) // When a placeholder is set and no text is in textarea - } - _ => (self.text(top_row as usize, height as usize), self.0.style()), + let (text, style) = if !self.0.placeholder.is_empty() && self.0.is_empty() { + let text = Text::from(self.0.placeholder.as_str()); + (text, self.0.placeholder_style) + } else { + (self.text(top_row as usize, height as usize), self.0.style()) }; // To get fine control over the text color and the surrrounding block they have to be rendered separately