Skip to content

Updates for 0.12.0 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Updates for 0.12.0
  • Loading branch information
maddie927 committed Jun 1, 2018
commit 12670ff3f986f31f413f6b67dfe6a4b646275a8a
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,112 @@ Available on [Pursuit](https://pursuit.purescript.org/packages/purescript-react-

```purescript
module Example where

import Prelude

import Data.Array ((!!), drop, mapWithIndex, take)
import Data.Foldable (for_)
import Data.Maybe (Maybe(Nothing), fromMaybe, maybe)
import React.Basic (ReactComponent, createElement, fragment, react)
import React.Basic.DOM as R
import React.Basic.DOM.Events (targetChecked)
import React.Basic.Events as Events
import React.Basic.ReactDND (DragDrop, DragDropContextProps, DragDropItemType(..), createDragDrop, createDragDropContext)
import React.Basic.ReactDND.Backends.HTML5Backend (html5Backend)

dndContext :: ReactComponent DragDropContextProps
dndContext = createDragDropContext html5Backend

dnd :: DragDrop { itemId :: String, index :: Int }
dnd = createDragDrop (DragDropItemType "TODO_ITEM")

component :: ReactComponent {}
component = react { displayName: "BasicExample", initialState, receiveProps, render }
where
initialState =
{ todos:
[ { id: "a", text: "PureScript", done: true }
, { id: "b", text: "React-Basic", done: true }
, { id: "c", text: "React-DND-Basic", done: false }
]
}

receiveProps _ _ _ =
pure unit

render _ state setState =
createElement dndContext
{ child:
fragment
[ R.h1_ [ R.text "Todos" ]
, R.p_ [ R.text "Drag to reorder the list:" ]
, R.section_ (mapWithIndex renderTodo state.todos)
]
}

where
renderTodo index todo =
createElement dnd.dragSource
{ beginDrag: \_ -> pure
{ itemId: todo.id
, index
}
, endDrag: const (pure unit)
, canDrag: const (pure true)
, isDragging: \{ item: draggingItem } ->
pure $ maybe false (\i -> i.itemId == todo.id) draggingItem
, render: \{ connectDragSource, isDragging } ->
createElement dnd.dropTarget
{ drop: \{ item: dragItem } -> do
for_ (_.index <$> dragItem) \dragItemIndex ->
setState \s -> s
{ todos = moveItem dragItemIndex index s.todos
}
pure Nothing
, hover: const (pure unit)
, canDrop: const (pure true)
, render: \{ connectDropTarget, isOver, item: maybeDragItem } ->
connectDragSource $ connectDropTarget $
R.div
{ style: R.css
{ padding: "0.3rem 0.8rem"
, alignItems: "center"
, borderTop:
if isOver && (fromMaybe false ((\dragItem -> dragItem.index > index) <$> maybeDragItem))
then "0.2rem solid #0044e4"
else "0.2rem solid transparent"
, borderBottom:
if isOver && (fromMaybe false ((\dragItem -> dragItem.index < index) <$> maybeDragItem))
then "0.2rem solid #0044e4"
else "0.2rem solid transparent"
, opacity: if isDragging then 0.1 else 1.0
}
, children:
[ R.input
{ "type": "checkbox"
, checked: todo.done
, onChange: Events.handler targetChecked \checked -> do
setState \s -> s
{ todos = s.todos <#> \t ->
if t.id == todo.id
then t { done = fromMaybe false checked }
else t
}
}
, R.text todo.text
]
}
}
}

moveItem :: forall a. Int -> Int -> Array a -> Array a
moveItem fromIndex toIndex items =
let
item = items !! fromIndex
items' = take fromIndex items <> drop (fromIndex + 1) items
in
take toIndex items'
<> maybe [] pure item
<> drop toIndex items'

```
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"url": "https://github.com/lumihq/purescript-react-dnd-basic.git"
},
"dependencies": {
"purescript-prelude": "^3.3.0",
"purescript-react-basic": "^0.10.0",
"purescript-nullable": "^3.0.0",
"purescript-promises": "^2.0.0"
"purescript-prelude": "^4.0.0",
"purescript-react-basic": "git@github.com:justinwoo/purescript-react-basic.git#893c554bb4c657a01a50f43d820fd8341be4eb00",
"purescript-nullable": "^4.0.0",
"purescript-promises": "^3.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
"purescript-psci-support": "^4.0.0"
}
}
6 changes: 3 additions & 3 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"author": "",
"dependencies": {
"pulp": "^12.2.0",
"react": "^16.2.0",
"react": "^16.4.0",
"react-dnd": "^2.6.0",
"react-dnd-html5-backend": "^2.6.0",
"react-dom": "^16.2.0"
"react-dom": "^16.4.0"
},
"devDependencies": {
"browserify": "^16.1.0"
"browserify": "^16.2.2"
}
}
Loading