Add Checkbox and Radio Button fields to Micron format

This commit is contained in:
zenith 2024-11-26 19:58:53 -05:00
parent a5aa2097bd
commit 912c510ab2
3 changed files with 27 additions and 5 deletions

View File

@ -31,7 +31,7 @@ The data can be `!`[submitted`:/page/input_fields.mu`username|two]`!.
>> Checkbox Fields
`B444`<?|sign_up|1`>`b Sign me up
`B444`<?|sign_up|1|*`>`b Sign me up
>> Radio group

View File

@ -1166,6 +1166,10 @@ When the checkbox is checked, it's field will be set to the provided value. If t
`B444`<?|sign_up|1`>`b Sign me up`
You can also pre-check both checkboxes and radio groups by appending a |* after the field value.
`B444`<?|checkbox|1|*`>`b Pre-checked checkbox`
>>> Radio groups
Radio groups are another input that lets the user chose from a set of options. Unlike checkboxes, radio buttons with the same field name are mutually exclusive.

View File

@ -177,7 +177,8 @@ def parse_line(line, state, url_delegate):
fv = o["value"]
flabel = o["label"]
fs = o["style"]
f = urwid.CheckBox(flabel, state=False)
fprechecked = o.get("prechecked", False)
f = urwid.CheckBox(flabel, state=fprechecked)
f.field_name = fn
f.field_value = fv
fa = urwid.AttrMap(f, fs)
@ -187,12 +188,13 @@ def parse_line(line, state, url_delegate):
fv = o["value"]
flabel = o["label"]
fs = o["style"]
if not "radio_groups" in state:
fprechecked = o.get("prechecked", False)
if "radio_groups" not in state:
state["radio_groups"] = {}
if not fn in state["radio_groups"]:
if fn not in state["radio_groups"]:
state["radio_groups"][fn] = []
group = state["radio_groups"][fn]
f = urwid.RadioButton(group, flabel, state=False, user_data=fv)
f = urwid.RadioButton(group, flabel, state=fprechecked, user_data=fv)
f.field_name = fn
f.field_value = fv
fa = urwid.AttrMap(f, fs)
@ -200,6 +202,7 @@ def parse_line(line, state, url_delegate):
columns_widget = urwid.Columns(widgets, dividechars=0)
text_widget = columns_widget
# text_widget = urwid.Text("<"+output+">", align=state["align"])
@ -501,6 +504,7 @@ def make_output(state, line, url_delegate):
field_name = field_content
field_value = ""
field_data = ""
field_prechecked = False
# check if field_content contains '|'
if '|' in field_content:
@ -526,10 +530,23 @@ def make_output(state, line, url_delegate):
except ValueError:
pass # Ignore invalid width
# Check for value and pre-checked flag
if len(f_components) > 2:
field_value = f_components[2]
else:
field_value = ""
if len(f_components) > 3:
if f_components[3] == '*':
field_prechecked = True
else:
# No '|', so field_name is field_content
field_name = field_content
field_type = "field"
field_masked = False
field_width = 24
field_value = ""
field_prechecked = False
# Find the closing '>' character
field_end = line.find('>', backtick_pos)
@ -546,6 +563,7 @@ def make_output(state, line, url_delegate):
"name": field_name,
"value": field_value if field_value else field_data,
"label": field_data,
"prechecked": field_prechecked,
"style": make_style(state)
})
else: