Skip to content

Commit 1b24b28

Browse files
authored
Merge pull request #210 from citizennet/AS-1787/toggle-single-tree-path
AS-1787 Add ToggleSingle Query to Tree component
2 parents a6ff95e + d138401 commit 1b24b28

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "purescript-ocelot",
3-
"version": "0.31.2",
3+
"version": "0.32.1",
44
"private": true,
55
"scripts": {
66
"build-all": "make build",

src/Components/Tree/Component.purs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import Ocelot.Block.Icon as Icon
2222
import Ocelot.Data.Tree (ItemPath, Node(..), IndexPath, _expanded, _selected, _children)
2323
import Ocelot.HTML.Properties (css)
2424

25+
type ComponentM item m =
26+
H.HalogenM (State item) (Action item) () (Message item) m
27+
2528
type State item =
2629
{ items :: Array (Node item)
2730
, initial :: Array (Node item)
@@ -36,6 +39,7 @@ data Action item
3639
data Query item a
3740
= SetItems (Array (Node item)) a
3841
| SetSelections (Array (ItemPath item)) a
42+
| ToggleSingle (ItemPath item) Boolean a
3943

4044
type Input item =
4145
{ renderItem :: item -> HH.PlainHTML
@@ -146,11 +150,24 @@ handleQuery = case _ of
146150
SetSelections itemPaths a -> do
147151
{ items, initial } <- H.get
148152
let paths = flip itemPathToIndexPath items <$> itemPaths
149-
updates = (\p r -> r { items = expandPath p r.items }) <$> paths
153+
updates = (\p r -> r { items = expandPath true p r.items }) <$> paths
150154
updater = A.foldl (>>>) (_ { items = initial }) updates
151155
H.modify_ updater
152156
pure $ Just a
153157

158+
ToggleSingle itemPath checked a -> toggleSingle itemPath checked $> Just a
159+
160+
toggleSingle ::
161+
forall item m.
162+
Eq item =>
163+
ItemPath item ->
164+
Boolean ->
165+
ComponentM item m Unit
166+
toggleSingle itemPath checked = do
167+
H.modify_ \old ->
168+
old { items = expandPath checked (itemPathToIndexPath itemPath old.items) old.items }
169+
170+
154171
-----
155172
-- Helper functions for expanding paths, toggling checkboxes, etc.
156173

@@ -176,8 +193,8 @@ pathToLens path lastProp = (<<<) _items <$> pathToLens'
176193

177194
-- TODO : update this to use lenses, possibly using pathToLens on increasing subsections of array
178195
-- e.g. [pathToLens [0] _expanded, pathToLens [0, 2] _expanded, pathToLens [0, 2, 1] _checked]
179-
expandPath :: a. IndexPath -> Array (Node a) -> Array (Node a)
180-
expandPath path traits = do
196+
expandPath :: a. Boolean -> IndexPath -> Array (Node a) -> Array (Node a)
197+
expandPath checked path traits = do
181198
expandPath' (A.head path) (A.tail path) traits
182199
where
183200
expandPath' (Just ix) (Just p) ts | A.length p > 0 = fromMaybe [] $ A.modifyAt ix (expand p) ts
@@ -187,7 +204,7 @@ expandPath path traits = do
187204
{ expanded = true
188205
, children = expandPath' (A.head p) (A.tail p) t.children
189206
}
190-
check (Node t) = Node $ t { selected = true }
207+
check (Node t) = Node $ t { selected = checked }
191208

192209
itemPathToIndexPath :: a. Eq a => ItemPath a -> Array (Node a) -> IndexPath
193210
itemPathToIndexPath path ns =

0 commit comments

Comments
 (0)