from beakerx import *

f = EasyForm("Form and Run")
f.addTextField("first")
f['first'] = "First"
f.addTextField("last")
f['last'] = "Last"
f.addButton("Go!", tag="run")
f

"""
Explanation: Python API to EasyForm
End of explanation
"""


"Good morning " + f["first"] + " " + f["last"]

f['last'][::-1] + '...' + f['first']

"""
Explanation: You can access the values from the form by treating it as an array indexed on the field names:
End of explanation
"""


f['first'] = 'Beaker'
f['last'] = 'Berzelius'

"""
Explanation: The array works both ways, so you set default values on the fields by writing the array:
End of explanation
"""


import operator

f1 = EasyForm("OnInit and OnChange")
f1.addTextField("first", width=15)
f1.addTextField("last", width=15)\
    .onInit(lambda: operator.setitem(f1, 'last', "setinit1"))\
    .onChange(lambda text: operator.setitem(f1, 'first', text + ' extra'))

button = f1.addButton("action", tag="action_button")
button.actionPerformed = lambda: operator.setitem(f1, 'last', 'action done')
f1

f1['last'] + ", " + f1['first']

f1['last'] = 'new Value'

f1['first'] = 'new Value2'

"""
Explanation: Event Handlers for Smarter Forms
You can use onInit and onChange to handle component events. For button events use actionPerformed or addAction.
End of explanation
"""


g = EasyForm("Field Types")
g.addTextField("Short Text Field", width=10)
g.addTextField("Text Field")
g.addPasswordField("Password Field", width=10)
g.addTextArea("Text Area")
g.addTextArea("Tall Text Area", 10, 5)
g.addCheckBox("Check Box")
options = ["a", "b", "c", "d"]
g.addComboBox("Combo Box", options)
g.addComboBox("Combo Box editable", options, editable=True)

g.addList("List", options)
g.addList("List Single", options, multi=False)
g.addList("List Two Row", options, rows=2)

g.addCheckBoxes("Check Boxes", options)
g.addCheckBoxes("Check Boxes H", options, orientation=EasyForm.HORIZONTAL)

g.addRadioButtons("Radio Buttons", options)
g.addRadioButtons("Radio Buttons H", options, orientation=EasyForm.HORIZONTAL)

g.addDatePicker("Date")

g.addButton("Go!", tag="run2")
g

result = dict()
for child in g:
    result[child] = g[child]

TableDisplay(result)

"""
Explanation: All Kinds of Fields
End of explanation
"""


gdp = EasyForm("Field Types")
gdp.addDatePicker("Date")
gdp

gdp['Date']

"""
Explanation: Dates
End of explanation
"""


easyForm = EasyForm("Field Types")
easyForm.addDatePicker("Date", value=datetime.today().strftime('%Y%m%d'))
easyForm

"""
Explanation: SetData
End of explanation
"""


h = EasyForm("Default Values")
h.addTextArea("Default Value", value = "Initial value")
h.addTextArea("Place Holder", placeholder = "Put here some text")
h.addCheckBox("Default Checked", value = True)
h.addButton("Press", tag="check")
h

result = dict()
for child in h:
    result[child] = h[child]

TableDisplay(result)

"""
Explanation: Default Values and placeholder
End of explanation
"""


from ipywidgets import * 

w = IntSlider()

widgetForm = EasyForm("python widgets")
widgetForm.addWidget("IntSlider", w)
widgetForm.addButton("Press", tag="widget_test")
widgetForm

widgetForm['IntSlider']

"""
Explanation: JupyterJSWidgets work with EasyForm
The widgets from JupyterJSWidgets are compatible and can appear in forms.
End of explanation
"""


