Commit 87318373 authored by John Kirkwood's avatar John Kirkwood

Refactor props/children consistently

parent f63007c4
from .pyreact import *
from pyreact import get_prop
def Div(props, children):
......@@ -6,19 +7,15 @@ def Div(props, children):
return ce('div', props, children)
def Button(props, children):
props = props or []
def Button(props=[], children=[]):
props.append(
cp('type', 'button'))
children = children or []
return ce('button', props, children)
def SubmitButton(props, children):
props = props or []
def SubmitButton(props=[], children=[]):
props.append(
cp('type', 'submit'))
children = children or []
return ce('button', props, children)
......@@ -33,11 +30,7 @@ def ScriptBefore(children, script_text):
return Div([], children)
def Form(children, action="#", method="POST"):
props = [
cp('action', action),
cp('method', method),
]
def Form(props=[], children=[]):
return ce('form', props, children)
......@@ -48,39 +41,20 @@ def Errors():
return Div(props, children)
def Input(el_type="text", el_id=None, el_value=None):
props = [
cp('type', el_type),
]
if id is not None:
props.append(
cp('id', el_id),
)
if el_value is not None:
props.append(
cp('value', el_value)
)
return ce('input', props, [])
def Input(props=[], children=[]):
return ce('input', props, children)
def Checkbox(is_checked=False, el_id=None):
ast = Input("checkbox", el_id)
if is_checked:
ast["props"].append(
cp('checked', "")
def Checkbox(props=[], children=[]):
props.append(
cp('type', "checkbox")
)
return ast
return Input(props, children)
def Label(label, el_for=None):
if label:
props = []
if el_for is not None:
props = [
cp("for", el_for)
]
return ce('label', props, label)
def Label(props=[], children=[]):
if children != []:
return ce('label', props, children)
else:
return []
......
......@@ -84,16 +84,27 @@ class MdcCheckboxInput(ChpWidgetMixin, CheckboxInput):
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),
context
)
children = [ast_checkbox,
MdcLabelWidget(context)
]
return FormField(children)
props, children = [], []
props.append(
cp("type", "checkbox"),
)
checked = context['widget']['attrs'].get('checked', False)
if checked:
props.append(cp("checked", checked))
el_id = context['widget']['attrs'].get('id', None)
if el_id:
props.append(cp("id", el_id))
label = context['widget']['label']
if isinstance(label, Promise):
label = conditional_escape(label)
context.update({
"label": label,
"for": context['widget']['id_for_label'],
"type": context['widget']['type'],
})
return CheckboxField(props, children, context)
def MdcCheckbox(field):
......@@ -110,23 +121,25 @@ def MdcCheckbox(field):
def MdcInput(field):
if field.mdc_type == "MDCDateField":
input_type = "date"
else:
input_type = "text"
# typ = field.field.widget.input_type
typ = field.mdc_type
# widget formatted value
value = field.field.widget.format_value(field.value())
ast = Input(input_type, field.auto_id, value)
props = [
cp("type", typ),
]
if value:
props.append(cp("value", value))
if field.auto_id:
props.append(cp("id", field.auto_id))
# widget-level attrs
for (key, value) in field.field.widget.attrs.items():
ast["props"].append(
props.append(
cp(key, value)
)
return ast
return Input(props, [])
def DjLabel(field):
......@@ -135,41 +148,33 @@ def DjLabel(field):
if isinstance(label, Promise):
label = conditional_escape(label)
props = [
cp("for", field.id_for_label),
]
context = {
"input_type": field.field.widget.input_type,
"id_for_label": field.id_for_label,
"type": field.mdc_type,
"for": field.id_for_label,
"label": label,
}
return Label(label, context["id_for_label"], context)
def MdcLabelWidget(context):
label = context['widget']['label']
# render gettext_lazy strings so they are recognised by AST renderer
if isinstance(label, Promise):
label = str(label)
el_for = context['widget']['id_for_label']
el_context = {}
el_context["input_type"] = context['widget']['type']
return Label(label, el_for, el_context)
return Label(props, label, context)
def MdcTextField(field):
if not hasattr(field, 'mdc_type'):
field.mdc_type = 'MDCTextField'
# to differentiate from Django date field
if not hasattr(field, "mdc_type"):
field.mdc_type = "text"
props = [
cp("type", field.mdc_type),
]
children = [
MdcInput(field),
DjLabel(field),
LineRipple(),
]
return InputField(field.field.widget.input_type, children)
return InputField(props, children)
def MdcDateField(field):
if not hasattr(field, 'mdc_type'):
field.mdc_type = 'MDCDateField'
field.mdc_type = "date"
return MdcTextField(field)
......@@ -33,6 +33,10 @@ class PostForm(forms.ModelForm):
def render(self, *args, **kwargs):
form = Form([
cp('action', reverse('blog:post_create')),
cp('method', "POST"),
],
[
Cell([
Div(
[cp("style", "display: flex;")],
......@@ -44,8 +48,7 @@ class PostForm(forms.ModelForm):
],
),
])
],
action=reverse('blog:post_create'))
])
return form
......
......@@ -41,7 +41,8 @@ def FormSchema(store_content, store_content_json):
return f"store_updates.add_todo({store_name})"
def render():
form = Form([
form = Form([],
[
Div(
[cp("style", "display: flex;")],
[
......
from .. import components as chp
from ..pyreact import (ce, cp)
from ..pyreact import (ce, cp, get_prop)
MDC_TYPE_MAP = {
"text": {
......@@ -9,7 +9,7 @@ MDC_TYPE_MAP = {
"init": "MDCTextField",
},
"date": {
"class": "mdc-date-field",
"class": "mdc-text-field",
"init": "MDCTextField",
},
"select": {
......@@ -44,8 +44,8 @@ def Cell(children=[]):
return Div(props, children)
def Form(children, action="#", method="POST"):
ast = chp.Form(children, action, method)
def Form(props, children):
ast = chp.Form(props, children)
ast["props"].append(
cp('class', 'mdc-layout-grid__cell')
)
......@@ -64,57 +64,61 @@ def LineRipple():
return Div([cp("class", "mdc-line-ripple")], [])
def Label(label, el_for=None, context={}):
if label:
ast = chp.Label(label, el_for)
if context.get("input_type", "text") not in ['checkbox']:
ast["props"].append(
def Label(props, children=[], context={}):
if children != []:
if context.get("type", "text") not in ["checkbox"]:
props.append(
cp("class", "mdc-floating-label")
)
return ast
return chp.Label(props, children)
else:
return []
def Input(el_type="text", el_id=None, el_value=None):
ast = chp.Input(el_type, el_id, el_value)
ast["props"].append(
def Input(props, children):
props.append(
cp("class", "mdc-text-field__input"),
)
return ast
return chp.Input(props, children)
def Checkbox(is_checked=False, el_id=None, context={}):
ast_input = chp.Checkbox(is_checked, el_id)
ast_input["props"].append(
def Checkbox(props, children, context={}):
props.append(
cp('class', 'mdc-checkbox__native-control')
)
ast_input = chp.Checkbox(props, children)
props = [
props_field = [
cp("class", "mdc-checkbox"),
cp("data-mdc-auto-init", "MDCCheckbox"),
]
children = [
children_field = [
ast_input,
Div([cp("class", "mdc-checkbox__background")], []),
]
ast_field = Div(props, children)
ast_field = Div(props_field, children_field)
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]
def CheckboxField(props, children, context={}):
ast_checkbox = Checkbox(props, children, context)
children_formfield = [ast_checkbox]
label = context.get("label", "")
if label != "":
el_id = get_prop(props, "id")
if el_id is not None:
lbl_props = [
cp("for", el_id["value"]),
]
ast_label = Label(lbl_props, label, context)
children_formfield.append(ast_label)
return FormField(children_formfield)
def InputField(props, children=[]):
typ = get_prop(props, "type")
mdc_type = MDC_TYPE_MAP[typ["value"]]
props = [
cp("class", mdc_type["class"]),
cp("data-mdc-auto-init", mdc_type["init"]),
......@@ -122,11 +126,8 @@ def InputField(el_type="text", children=[]):
return Div(props, children)
def SubmitButton(name, on_click):
props = [
cp('onclick', on_click)
]
ast = chp.SubmitButton(props, name)
def SubmitButton(props, children):
ast = chp.SubmitButton(props, children)
props = [
cp("class", "mdc-button"),
cp("data-mdc-auto-init", None),
......
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