Skip to content

Commit

Permalink
Merge pull request #755 from PanderMusubi/colorfield
Browse files Browse the repository at this point in the history
ColorField implementation
  • Loading branch information
azmeuk committed Dec 24, 2022
2 parents 4b94af0 + 7818c80 commit c0fb01e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
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

0 comments on commit c0fb01e

Please sign in to comment.