Skip to content

Commit fe0c567

Browse files
authored
Merge pull request hack4impact#156 from hack4impact/custom_select
Add customselect
2 parents 6aab559 + 93e8337 commit fe0c567

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

app/templates/macros/form_macros.html

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,35 @@
7575
{% endif %}
7676
{% endmacro %}
7777

78+
{# Render a field for the form #}
79+
{% macro render_custom_select(field) %}
80+
{{ field.label }}
81+
{% if field.description %}
82+
<div class="ui pointing below label">
83+
{{ field.description }}
84+
</div>
85+
{% endif %}
86+
<div id="{{field.name}}" class="ui fluid {% if field.multiple %}multiple {% endif %}search selection dropdown">
87+
{{ field() }}
88+
<i class="dropdown icon"></i>
89+
<div class="default text">{{field.label.text}}</div>
90+
<div class="menu">
91+
{% for choice in field.choices %}
92+
<div class="item">{{choice}}</div>
93+
{% endfor %}
94+
</div>
95+
</div>
96+
{% if field.allow_custom %}
97+
<script>
98+
$(document).ready(function() {
99+
$('div#{{field.name}}').dropdown({
100+
allowAdditions: true
101+
})
102+
});
103+
</script>
104+
{% endif %}
105+
{% endmacro %}
106+
78107
{# Render a field for the form #}
79108
{% macro render_form_field(field, extra_classes='') %}
80109
{% if field.type == 'Radio Field' %}
@@ -97,8 +126,8 @@
97126
{{ field.label }}
98127
</div>
99128
{% elif field.type == 'RadioField' %}
129+
{{ field.label }}
100130
{% for item in field %}
101-
{{ field.label }}
102131
<div class="ui radio checkbox">
103132
{{ item }}
104133
{{ item.label }}
@@ -108,13 +137,15 @@
108137
{{ field(class='ui button') }}
109138
{% elif field.type == 'FormField' %}
110139
{{ render_form(field) }}
140+
{% elif field.type == 'CustomSelectField' %}
141+
{{ render_custom_select(field) }}
111142
{% else %}
112143
{{ field.label }}
113-
{% if field.description %}
114-
<div class="ui pointing below label">
115-
{{ field.description }}
116-
</div>
117-
{% endif %}
144+
{% if field.description %}
145+
<div class="ui pointing below label">
146+
{{ field.description }}
147+
</div>
148+
{% endif %}
118149
{{ field(placeholder=field.label.text) }}
119150
{% endif %}
120151
{% endmacro %}

app/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from flask import url_for
2+
from wtforms.fields import Field
3+
from wtforms.widgets import HiddenInput
4+
from wtforms.compat import text_type
25

36

47
def register_template_utils(app):
@@ -18,3 +21,24 @@ def is_hidden_field(field):
1821

1922
def index_for_role(role):
2023
return url_for(role.index)
24+
25+
26+
class CustomSelectField(Field):
27+
widget = HiddenInput()
28+
29+
def __init__(self, label='', validators=None, multiple=False,
30+
choices=[], allow_custom=True, **kwargs):
31+
super(CustomSelectField, self).__init__(label, validators, **kwargs)
32+
self.multiple = multiple
33+
self.choices = choices
34+
self.allow_custom = allow_custom
35+
36+
def _value(self):
37+
return text_type(self.data) if self.data is not None else ''
38+
39+
def process_formdata(self, valuelist):
40+
if valuelist:
41+
self.data = valuelist[1]
42+
self.raw_data = [valuelist[1]]
43+
else:
44+
self.data = ''

0 commit comments

Comments
 (0)