Skip to content
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

ColorField implementation #755

Merged
merged 1 commit into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ Convenience Fields

{{ form.textarea(rows=7, cols=90) }}

.. autoclass:: ColorField(default field arguments)


Field Enclosures
----------------
Expand Down
9 changes: 9 additions & 0 deletions src/wtforms/fields/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"TelField",
"URLField",
"EmailField",
"ColorField",
)


Expand Down Expand Up @@ -162,3 +163,11 @@ class EmailField(StringField):
"""

widget = widgets.EmailInput()


class ColorField(StringField):
"""
Represents an ``<input type="color">``.
"""

widget = widgets.ColorInput()
16 changes: 16 additions & 0 deletions tests/fields/test_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from wtforms import widgets
from wtforms.fields import ColorField
from wtforms.form import Form


class F(Form):
a = ColorField(
widget=widgets.ColorInput(), default="#ff0000"
)
b = ColorField(default="#00ff00")


def test_color_field():
form = F()
assert form.a() == """<input id="a" name="a" type="color" value="#ff0000">"""
assert form.b() == """<input id="b" name="b" type="color" value="#00ff00">"""
9 changes: 9 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from markupsafe import Markup

from wtforms.widgets.core import CheckboxInput
from wtforms.widgets.core import ColorInput
from wtforms.widgets.core import FileInput
from wtforms.widgets.core import HiddenInput
from wtforms.widgets.core import html_params
Expand Down Expand Up @@ -147,6 +148,14 @@ def test_file(self, basic_widget_dummy_field):
== '<input id="id" multiple name="bar" type="file">'
)

def test_color_input(self, basic_widget_dummy_field):
assert 'type="color"' in ColorInput()(basic_widget_dummy_field)
assert 'value="foo"' in ColorInput()(basic_widget_dummy_field)
basic_widget_dummy_field.data = "#ff0000"
assert 'value="#ff0000"' in ColorInput()(
basic_widget_dummy_field
)


class TestSelect:
def test_select(self, select_dummy_field):
Expand Down