-
Notifications
You must be signed in to change notification settings - Fork 263
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
textinput: placeholder changes component width #358
Comments
I just looked into fixing this - and it is a simple fix to basically cut the placeholder off, so that the width never changes. But the current behavior could have benefits: You can have input fields that are narrow to not take up a lot of space but when you focus them, you can set a longer placeholder to describe the field - and unset that placeholder again when you unfocus... So people might depend on this behavior? This would be a very simple possible patch: (Not making a PR because I'm really not sure what the desired behavior is. But I'd happy to create a PR.) diff --git a/textinput/textinput.go b/textinput/textinput.go
index e7bd08b..2d863c0 100644
--- a/textinput/textinput.go
+++ b/textinput/textinput.go
@@ -651,8 +651,8 @@ func (m Model) placeholderView() string {
m.Cursor.SetChar(p[:1])
v += m.Cursor.View()
- // The rest of the placeholder text
- v += style(p[1:])
+ // The rest of the placeholder text, cut off to fit the width
+ v += style(p[1:m.Width] + " ")
return m.PromptStyle.Render(m.Prompt) + v
}
|
Took me a long time to find the issue that was causing this bug. Personally, the behavior I would expect is that if a placeholder string is smaller than the defined width of the component, it shouldn't shrink the component width. |
@residualmind nice find. Personally, I agree with @Static-Flow that if a placeholder is smaller than the text input, it shouldn't shrink the text input. That seems counter to the notion of setting a width on the text input in the first place, and the current behavior feels pretty inconsistent with, well, most other text inputs I've come across. Would you mind making a PR (or I'd be glad to make one with your change) and maybe someone from the core team can merge or decline if they feel that the current behavior is the desired behavior? |
Hey everyone. So if I understand this correctly the thought is that the placeholder should get truncated if the width of the If so, that's definitely the desired behavior; please do make a PR. Use |
Is this issue actually resolved now? Seems that the fix was merged. I've run up the test app in the original message and it seems to be working correctly now. @meowgorithm You should be able to close this one. |
I noticed that the cursor seemed to be adding an extra space which the width calculations weren't accounting for. This also led me to discover an edge case with the view (`offset`/`offsetRight`) calculation when the cursor (`pos`) sits at the very end. Additionally, `View()` was always rendering the cursor, even when `focus` was false, which the comment suggests should not be the case. These should all be resolved, such that Width is now fully respected in both `View()` and `placeholderView()`: they should _never_ render a string that exceeds the requested width. (Well... with one caveat: I did not touch the suggestion logic, as I've never used it. I did leave a comment, however, that perhaps suggestions shouldn't even render when the component does not have focus.) Fixes charmbracelet#358, charmbracelet#307
I'm seeing strange behavior using a Placeholder with the textinput bubble. Here is a minimum reproducible example:
Changing the length of the Placeholder string changes the way that the textinput is rendered before it receives the first character, triggering a resize after the first character is received. See examples with various Placeholders.
Placeholder = "123"
Placeholder = "12345"
Placeholder = "1234567890"
Placeholder = "12345 " (with trailing space)
no Placeholder
So to avoid a resize it seems there needs to either be no Placeholder at all, or the length of the Placeholder must be equal to Width + 1.
The text was updated successfully, but these errors were encountered: