Verified Commit 3db68d94 authored by Camil Staps's avatar Camil Staps 🙂

Support multiple select fields and dynamically changing options in Extensions.Editors.Select2

parent 278baecf
......@@ -15,6 +15,11 @@ main = ArrangeWithTabs False @>>
[ select2AndNormal "Enter expression (withSelect2)" enterExpression
, select2AndNormal "Dropdown" enterWithDropdown
, select2AndNormal "Dropdown with groups" enterWithDropdownWithGroups
, withShared fruits \shared ->
Title "Dropdown with dynamic options" @>> (
(Hint "Enter options:" @>> updateSharedInformation [] shared) ||-
select2AndNormal "" (dropdownWithDynamicOptions shared)
)
]
@! ()
where
......@@ -42,15 +47,17 @@ enterExpression select2 hint =
where
editor = if select2 (withSelect2 gEditor{|*|}) gEditor{|*|}
fruits =: ["Apple", "Banana", "Cherry"]
veggies =: ["Arugula", "Broccoli", "Chickpea"]
enterWithDropdown :: !Bool !String -> Task String
enterWithDropdown select2 hint =
Hint (concat ["Enter a value ",hint,":"]) @>>
enterInformation [EnterUsing id (choose options)] @ ((!!) options) >>! \choice ->
enterInformation [EnterUsing id (choose fruits)] @ ((!!) fruits) >>! \choice ->
Hint "You entered:" @>>
viewInformation [] choice >>! return
where
choose = if select2 chooseWithSelect2Dropdown chooseWithDropdown
options = ["Apple", "Banana", "Cherry"]
enterWithDropdownWithGroups :: !Bool !String -> Task String
enterWithDropdownWithGroups select2 hint =
......@@ -69,6 +76,22 @@ where
options =
[(opt, Just "Fruits") \\ opt <- fruits] ++
[(opt, Just "Veggies") \\ opt <- veggies]
where
fruits = ["Apple", "Banana", "Cherry"]
veggies = ["Arugula", "Broccoli", "Chickpea"]
derive class iTask ChoiceText
dropdownWithDynamicOptions :: !(SimpleSDSLens [String]) !Bool !String -> Task [String]
dropdownWithDynamicOptions options select2 hint =
ArrangeVertical @>>
Hint (concat ["Make a choice ",hint,":"]) @>>
editSelectionWithShared
[ SelectUsing
(\opts -> [{ChoiceText | id=i,text=o} \\ o <- opts & i <- [0..]])
(\opts is -> [opts !! i \\ i <- is])
editor
]
options
(const []) >>! \choices ->
Hint "You entered:" @>>
viewInformation [] choices >>! return
where
editor = if select2 select2Dropdown dropdown <<@ multipleAttr True
......@@ -73,9 +73,21 @@ where
onSelect {[0]=ev} world
# target = ev .# "target"
# world = (target .# "value" .= ev .# "params" .# "data" .# "id") world
# (value,world) = ev .# "params" .# "data" .# "id" .? world
# value = jsValToString` "" value
# (options,world) = target .# "options" .? world
# (options,world) = jsValToList` options id world
# world = selectOption value options world
# (ev,world) = jsNew "Event" "change" world
= (target .# "dispatchEvent" .$! ev) world
where
selectOption _ [] world
= world
selectOption id [opt:opts] world
# (value,world) = opt .# "value" .? world
| jsValToString` "" value == id
= (opt .# "selected" .= True) world
= selectOption id opts world
onDOMMutation onSelect {[0]=mutations} world
# (mutations,world) = jsValToList` mutations id world
......
......@@ -66,6 +66,7 @@ selectEditor [SelectInList toView fromView:_] = SelectUsing toView fromView choi
selectEditor [SelectInGrid toView fromView:_] = SelectUsing toView fromView grid
selectEditor [SelectInTree toView fromView:_] = SelectUsing toView fromView tree
selectEditor [SelectInTabs toView fromView:_] = SelectUsing toView fromView tabBar
selectEditor [SelectUsing toView fromView editor:_] = SelectUsing toView fromView editor
selectEditor [_:es] = selectEditor es
selectEditor [] = SelectUsing (const []) (\_ _ -> []) dropdown //Empty dropdown
......
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