Commit f63007c4 authored by John Kirkwood's avatar John Kirkwood

Refactor elements for mdc

parent b4c27131
......@@ -14,6 +14,14 @@ def Button(props, children):
return ce('button', props, children)
def SubmitButton(props, children):
props = props or []
props.append(
cp('type', 'submit'))
children = children or []
return ce('button', props, children)
def Script(string=""):
return ce('script', [], string)
......@@ -40,7 +48,7 @@ def Errors():
return Div(props, children)
def Input(el_type="text", el_id=None):
def Input(el_type="text", el_id=None, el_value=None):
props = [
cp('type', el_type),
]
......@@ -48,6 +56,10 @@ def Input(el_type="text", el_id=None):
props.append(
cp('id', el_id),
)
if el_value is not None:
props.append(
cp('value', el_value)
)
return ce('input', props, [])
......@@ -61,13 +73,16 @@ def Checkbox(is_checked=False, el_id=None):
def Label(label, el_for=None):
props = []
if el_for is not None:
props = [
cp("for", el_for)
]
return ce('label', props, label)
if label:
props = []
if el_for is not None:
props = [
cp("for", el_for)
]
return ce('label', props, label)
else:
return []
# def Label(name):
......
......@@ -65,24 +65,25 @@ class ChpWidgetMixin:
attrs = attrs.copy()
super().__init__(attrs)
def render(self, name, value, attrs=None, renderer=None):
"""Build a context and render the widget as a component."""
context = self.get_context(name, value, attrs)
def _add_label(self, context):
context['widget'].update(
{'label': self.label,
'id_for_label':
self.id_for_label(context['widget']['attrs']['id'])
})
# return self._render(self.template_name, context, renderer)
return self.chp_render(context)
return context
def chp_render(self, context):
def _render(self, template_name, context, renderer=None):
'''Ignore template_name and renderer (see django.forms.py)
'''
context = self._add_label(context)
raise NotImplementedError
class MdcCheckboxInput(ChpWidgetMixin, CheckboxInput):
def chp_render(self, context):
def _render(self, template_name, context, renderer=None):
context = self._add_label(context)
ast_checkbox = Checkbox(
context['widget']['attrs'].get('checked', False),
context['widget']['attrs'].get('id', None),
......@@ -114,14 +115,11 @@ def MdcInput(field):
else:
input_type = "text"
ast = Input(input_type, field.auto_id)
# widget formatted value
value = field.field.widget.format_value(field.value())
if not (value == '' or value is None):
ast["props"].append(
cp("value", value)
)
ast = Input(input_type, field.auto_id, value)
# widget-level attrs
for (key, value) in field.field.widget.attrs.items():
ast["props"].append(
......
......@@ -15,6 +15,7 @@
{{ form.render }}
<form action="{% url 'blog:post_create' %}" method="post">
{% csrf_token %}
<div class="mdc_layout_grid__cell">
<div style="display: flex;">
......@@ -64,6 +65,9 @@
</div>
</div>
</div>
<div class="mdc-button" data-mdc-auto-init="MDCButton">
<button type="submit">Submit</button>
</div>
</form>
{# {{ form.render }} #}
<script type="text/javascript" src="{% static "output.js" %}"></script>
......
......@@ -65,18 +65,21 @@ def LineRipple():
def Label(label, el_for=None, context={}):
ast = chp.Label(label, el_for)
if label:
ast = chp.Label(label, el_for)
if context.get("input_type", "text") not in ['checkbox']:
ast["props"].append(
cp("class", "mdc-floating-label")
)
if context.get("input_type", "text") not in ['checkbox']:
ast["props"].append(
cp("class", "mdc-floating-label")
)
return ast
return ast
else:
return []
def Input(el_type="text", el_id=None):
ast = chp.Input(el_type, el_id)
def Input(el_type="text", el_id=None, el_value=None):
ast = chp.Input(el_type, el_id, el_value)
ast["props"].append(
cp("class", "mdc-text-field__input"),
......@@ -97,15 +100,19 @@ def Checkbox(is_checked=False, el_id=None, context={}):
]
children = [
ast_input,
Div([cp("class", "mdc-checkbox__background")],
[]
),
Div([cp("class", "mdc-checkbox__background")], []),
]
ast_field = Div(props, children)
return ast_field
def CheckboxField(is_checked=False, el_id=None, label="", context={}):
return FormField([
Checkbox(is_checked, el_id, context),
Label(el_id, label)
])
def InputField(el_type="text", children=[]):
mdc_type = MDC_TYPE_MAP[el_type]
props = [
......@@ -119,4 +126,9 @@ def SubmitButton(name, on_click):
props = [
cp('onclick', on_click)
]
return Button(props, name)
ast = chp.SubmitButton(props, name)
props = [
cp("class", "mdc-button"),
cp("data-mdc-auto-init", None),
]
return Div(props, ast)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment