-
Notifications
You must be signed in to change notification settings - Fork 290
[Feature] Add auto-resizing textarea widget with character counter #1083
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
Changes from 4 commits
d7b0fae
9179770
66f3087
b6445fe
c677117
7664516
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import json | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from markupsafe import Markup | ||
| from markupsafe import Markup, escape | ||
| from wtforms import Field, SelectFieldBase, widgets | ||
| from wtforms.widgets import html_params | ||
|
|
||
|
|
@@ -124,3 +124,39 @@ def __call__(self, field: Field, **kwargs: Any) -> Markup: | |
| + str(Markup.escape(super().__call__(field, **kwargs))) | ||
| + "</div>" | ||
| ) | ||
|
|
||
|
|
||
| class TextAreaWidget(widgets.Input): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in this case you can downsize call override |
||
| """ | ||
| Render a textarea that automatically resizes on input. | ||
| """ | ||
|
|
||
| validation_attrs = ["required", "disabled", "readonly", "maxlength", "minlength"] | ||
|
|
||
| def __call__(self, field: Field, **kwargs: Any) -> Markup: | ||
| kwargs.setdefault("id", field.id) | ||
| flags = getattr(field, "flags", {}) | ||
| for k in dir(flags): | ||
| if k in self.validation_attrs and k not in kwargs: | ||
| kwargs[k] = getattr(flags, k) | ||
|
|
||
| class_ = kwargs.get("class") | ||
| if getattr(field, "enable_autoresize", True) is True: | ||
| class_ = " ".join(filter(None, (class_, "autoresize-textarea"))) | ||
| kwargs.setdefault("rows", "1") | ||
|
|
||
| kwargs["class"] = class_ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This happens even when class_ is None. Skip instead. |
||
|
|
||
| if hasattr(field, "_value") and callable(field._value): | ||
| kwargs["value"] = field._value() | ||
|
|
||
| if getattr(field, "show_chars_count", True) is True: | ||
| chars_count = '<div class="chars-count-label pt-1"></div>' | ||
| else: | ||
| chars_count = "" | ||
|
|
||
| return Markup( | ||
| "<textarea %s>%s</textarea>" | ||
| % (html_params(name=field.name, **kwargs), escape(kwargs.get("value", ""))) | ||
| + chars_count | ||
| ) # nosec: markupsafe_markup_xss | ||
|
mmzeynalli marked this conversation as resolved.
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo