From c2dddf6f5535d9307413cf981ed30464faaf1dba Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 11 Mar 2025 20:40:50 +0800 Subject: [PATCH] input: Update gap between input and prefix, suffix. (#709) image --- crates/ui/src/input/input.rs | 23 +++++++++++++++++++++-- crates/ui/src/input/number_input.rs | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index 455806c3..255be4c7 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -224,6 +224,8 @@ pub struct TextInput { pub(super) cleanable: bool, pub(super) size: Size, pub(super) rows: usize, + /// For special case, e.g.: NumberInput + - button + pub(super) no_gap: bool, pub(super) height: Option, pattern: Option, validate: Option bool + 'static>>, @@ -282,6 +284,7 @@ impl TextInput { loading: false, prefix: None, suffix: None, + no_gap: false, size: Size::Medium, height: None, pattern: None, @@ -583,6 +586,14 @@ impl TextInput { self } + /// Set true to not use gap between input and prefix, suffix, and clear button. + /// + /// Default: false + pub(super) fn no_gap(mut self) -> Self { + self.no_gap = true; + self + } + /// Set the regular expression pattern of the input field. pub fn pattern(mut self, pattern: regex::Regex) -> Self { self.pattern = Some(pattern); @@ -1596,6 +1607,14 @@ impl Render for TextInput { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { const LINE_HEIGHT: Rems = Rems(1.25); let focused = self.focus_handle.is_focused(window); + let mut gap_x = match self.size { + Size::Small => px(4.), + Size::Large => px(12.), + _ => px(8.), + }; + if self.no_gap { + gap_x = px(0.); + } let prefix = self.prefix.as_ref().map(|build| build(window, cx)); let suffix = self.suffix.as_ref().map(|build| build(window, cx)); @@ -1672,12 +1691,12 @@ impl Render for TextInput { .when(prefix.is_none(), |this| this.input_pl(self.size)) .when(suffix.is_none(), |this| this.input_pr(self.size)) .children(prefix) - .gap_1() + .gap(gap_x) .items_center() .child( div() .id("TextElement") - .w_full() + .flex_1() .when(self.is_multi_line(), |this| this.h_full()) .flex_grow() .overflow_x_hidden() diff --git a/crates/ui/src/input/number_input.rs b/crates/ui/src/input/number_input.rs index 149437c4..fc8ac79b 100644 --- a/crates/ui/src/input/number_input.rs +++ b/crates/ui/src/input/number_input.rs @@ -38,6 +38,7 @@ impl NumberInput { let input = cx.new(|cx| { TextInput::new(window, cx) .pattern(pattern) + .no_gap() .appearance(false) });