from beakerx import *

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

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

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

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

h = EasyForm(title="Form and Run")

h.addTextField("first", width=10)
h.addTextField("default")
h.addTextArea("Text Area 1", height=5, width=20)
h.addTextArea("Text Area 2")
h.addTextArea("Text Area 3", height=10)
h.addTextArea("Text Area 4",width=20)
h

g2 = EasyForm("Field Types")
options = ["a", "b", "c", "d", "e", "f"]
g2.addList("List Single", options, multi=False)
g2.addList("List Two Row", options, rows=2)
g2

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

f['last'] = "new Value"

f['first'] = "new Value2"

# All Kinds of Fields

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]

result

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

gdp['Date']

f.put("first", "Micheal")
f.put("last", "Fox")
# Read values from form
firstName = f.get("first")
lastName = f.get("last")

print("Good morning " + firstName + " " + lastName)

f = EasyForm("actionPerformed demo")
f.addTextField("first")
f['first'] = "First"
b = f.addButton("Action!")
b.actionPerformed = lambda: print("clicked "+f["first"])
f

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", "action_button")
button.actionPerformed = lambda: operator.setitem(f1, 'last', 'action done')
f1

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


f3c = EasyForm("form3")
f3c = EasyForm("form3")
f3c.addTextArea("Default Value", value = "Initial value")
f3c.addTextArea("Place Holder", placeholder = "Put here some text")
f3c.addCheckBox("Default Checked", value = True)
f3c

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

result

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


from beakerx import *
from ipywidgets import * 
w = IntSlider()

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

f['slider']

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


