Skip to content

Commit a6f5f9a

Browse files
authored
Add FileUploadReactHooks (#242)
* Add FileUploadReactHooks * Update README * Refactor handleChange * Refactor handleChange once more
1 parent d5d5e05 commit a6f5f9a

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Running a web-compatible recipe:
111111
| | :heavy_check_mark: | [DriverRoutingHalogenHooks](recipes/DriverRoutingHalogenHooks) | Demonstrates using `hashchange` events to drive the root component in a Halogen application via the driver. |
112112
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/DriverWebSocketsHalogenHooks/src/Main.purs) - [fixme](recipes/DriverWebSocketsHalogenHooks/tryFixMe.md)) | [DriverWebSocketsHalogenHooks](recipes/DriverWebSocketsHalogenHooks) | Demonstrates using a WebSocket to drive the main component in a Halogen application. |
113113
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/FileUploadHalogenHooks/src/Main.purs)) | [FileUploadHalogenHooks](recipes/FileUploadHalogenHooks) | A Halogen port of the ["Files - Upload" Elm Example](https://elm-lang.org/examples/upload). |
114+
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/FileUploadReactHooks/src/Main.purs)) | [FileUploadReactHooks](recipes/FileUploadReactHooks) | A React port of the ["Files - Upload" Elm Example](https://elm-lang.org/examples/upload). |
114115
| | :heavy_check_mark: | [FindDomElementJs](recipes/FindDomElementJs) | This recipe shows how to find elements in the DOM by using query selectors. |
115116
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/FormsReactHooks/src/Main.purs)) | [FormsReactHooks](recipes/FormsReactHooks) | A React port of the ["User Interface - Forms" Elm Example](https://elm-lang.org/examples/forms). |
116117
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/GroceriesHalogenHooks/src/Main.purs)) | [GroceriesHalogenHooks](recipes/GroceriesHalogenHooks) | A Halogen port of the ["HTML - Groceries" Elm Example](https://elm-lang.org/examples/groceries). |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*
10+
/.spago
11+
/.cache/
12+
/dist/
13+
/web-dist/
14+
/prod-dist/
15+
/prod/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# FileUploadReactHooks
2+
3+
A React port of the ["Files - Upload" Elm Example](https://elm-lang.org/examples/upload).
4+
5+
## Expected Behavior:
6+
7+
### Browser
8+
9+
The browser will display an upload button and the names of the last-uploaded files. When the user clicks the upload button and uploads 1 or more files, the label will show the names of those files.
10+
11+
## Dependencies used
12+
13+
[react](https://www.npmjs.com/package/react)
14+
[react-dom](https://www.npmjs.com/package/react-dom)
15+
[@emotion/core](https://www.npmjs.com/package/@emotion/core)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{ name = "FileUploadReactHooks"
2+
, dependencies =
3+
[ "console"
4+
, "effect"
5+
, "psci-support"
6+
, "react-basic-dom"
7+
, "react-basic-hooks"
8+
]
9+
, packages = ../../packages.dhall
10+
, sources = [ "recipes/FileUploadReactHooks/src/**/*.purs" ]
11+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module FileUploadReactHooks.Main where
2+
3+
import Prelude
4+
import Data.Foldable (for_)
5+
import Data.Maybe (Maybe(..))
6+
import Effect (Effect)
7+
import Effect.Exception (throw)
8+
import React.Basic.DOM (render)
9+
import React.Basic.DOM as R
10+
import React.Basic.DOM.Events (currentTarget)
11+
import React.Basic.Events (handler)
12+
import React.Basic.Hooks (Component, component, fragment, useState', (/\))
13+
import React.Basic.Hooks as React
14+
import Web.File.File as File
15+
import Web.File.FileList as FileList
16+
import Web.HTML (window)
17+
import Web.HTML.HTMLDocument (body)
18+
import Web.HTML.HTMLElement (toElement)
19+
import Web.HTML.HTMLInputElement as HTMLInputElement
20+
import Web.HTML.Window (document)
21+
22+
main :: Effect Unit
23+
main = do
24+
body <- body =<< document =<< window
25+
case body of
26+
Nothing -> throw "Could not find body."
27+
Just b -> do
28+
fileUploadComponent <- mkFileUploadComponent
29+
render (fileUploadComponent {}) (toElement b)
30+
31+
mkFileUploadComponent :: Component {}
32+
mkFileUploadComponent = do
33+
component "FileUploadComponent" \_ -> React.do
34+
fileList /\ setFileList <- useState' []
35+
let
36+
handleChange t =
37+
for_ (HTMLInputElement.fromEventTarget t) \fileInput -> do
38+
maybeFiles <- HTMLInputElement.files fileInput
39+
for_ maybeFiles (setFileList <<< map File.name <<< FileList.items)
40+
pure
41+
$ fragment
42+
[ R.input
43+
{ type: "file"
44+
, multiple: true
45+
, onChange: handler currentTarget handleChange
46+
}
47+
, R.pre_
48+
[ R.text (show fileList)
49+
]
50+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>FileUploadReactHooks</title>
7+
</head>
8+
9+
<body>
10+
<div id="root"></div>
11+
<script src="./index.js"></script>
12+
</body>
13+
14+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
require("../../../output/FileUploadReactHooks.Main/index.js").main();

0 commit comments

Comments
 (0)