Skip to content
This repository was archived by the owner on Oct 10, 2020. It is now read-only.

Commit 3a2bc8e

Browse files
committed
add OptionsForm
1 parent 54008a8 commit 3a2bc8e

File tree

2 files changed

+89
-43
lines changed

2 files changed

+89
-43
lines changed

src/Component/App.purs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ module Component.App
22
( app
33
) where
44

5+
import Component.OptionsForm as OptionsForm
56
import Data.Array as Array
6-
import Data.Functor (mapFlipped)
7+
import Data.Functor (mapFlipped, (<#>))
78
import Data.Maybe (Maybe(..), fromMaybe)
8-
import React.Basic (Component, JSX, Self, StateUpdate(..), capture, capture_, createComponent, make)
9+
import React.Basic (Component, JSX, Self, StateUpdate(..), capture, capture_, createComponent, make, send)
910
import React.Basic.DOM as H
1011
import React.Basic.DOM.Events (targetValue)
1112
import Simple.JSON (class WriteForeign, writeImpl)
@@ -123,48 +124,15 @@ render self@{ state: { built, form } } =
123124
}
124125
]
125126
, H.br {}
126-
, H.label_
127-
[ H.span_ [ H.text "option label" ]
128-
, H.input
129-
{ onChange:
130-
capture
131-
self
132-
targetValue
133-
(\v -> EditOptionLabel (fromMaybe "" v))
134-
, value: form.optionLabel
135-
}
136-
]
137-
, H.br {}
138-
, H.label_
139-
[ H.span_ [ H.text "option value" ]
140-
, H.input
141-
{ onChange:
142-
capture
143-
self
144-
targetValue
145-
(\v -> EditOptionValue (fromMaybe "" v))
146-
, value: form.optionValue
147-
}
148-
]
149-
, H.button
150-
{ onClick: capture_ self AddOption
151-
, children: [ H.text "Add Option" ]
127+
, OptionsForm.optionsForm
128+
{ label: form.optionLabel
129+
, onAddOption: send self AddOption
130+
, onChangeLabel: (\v -> send self (EditOptionLabel v))
131+
, onChangeValue: (\v -> send self (EditOptionValue v))
132+
, options: form.options <#> (\(Option label value) -> { label, value })
133+
, value: form.optionValue
152134
}
153-
, H.div_
154-
[ H.span_
155-
[ H.text "options"
156-
]
157-
, H.ul_
158-
(mapFlipped
159-
form.options
160-
(\(Option l v) ->
161-
H.li_
162-
[ H.span_
163-
[ H.text l ]
164-
, H.span_
165-
[ H.text v ]
166-
]))
167-
]
135+
168136
, H.div_
169137
[ H.button
170138
{ onClick: capture_ self AddSelect

src/Component/OptionsForm.purs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module Component.OptionsForm
2+
( optionsForm
3+
) where
4+
5+
import Data.Functor (mapFlipped)
6+
import Data.Maybe (fromMaybe)
7+
import Effect (Effect)
8+
import Prelude (Unit, (>>>))
9+
import React.Basic (Component, JSX, createComponent, makeStateless)
10+
import React.Basic.DOM as H
11+
import React.Basic.DOM.Events (preventDefault, stopPropagation, targetValue)
12+
import React.Basic.Events (handler)
13+
14+
type Props =
15+
{ label :: String
16+
, onAddOption :: Effect Unit
17+
, onChangeLabel :: String -> Effect Unit
18+
, onChangeValue :: String -> Effect Unit
19+
, options :: Array { label :: String, value :: String }
20+
, value :: String
21+
}
22+
23+
component :: Component Props
24+
component = createComponent "OptionsForm"
25+
26+
-- | options form component
27+
optionsForm :: Props -> JSX
28+
optionsForm = makeStateless component render
29+
30+
render :: Props -> JSX
31+
render props =
32+
H.div_
33+
[ H.label_
34+
[ H.span_ [ H.text "option label" ]
35+
, H.input
36+
{ onChange:
37+
handler
38+
(preventDefault >>> stopPropagation >>> targetValue)
39+
(\v -> props.onChangeLabel (fromMaybe "" v))
40+
, value: props.label
41+
}
42+
]
43+
, H.br {}
44+
, H.label_
45+
[ H.span_ [ H.text "option value" ]
46+
, H.input
47+
{ onChange:
48+
handler
49+
(preventDefault >>> stopPropagation >>> targetValue)
50+
(\v -> props.onChangeValue (fromMaybe "" v))
51+
, value: props.value
52+
}
53+
]
54+
, H.button
55+
{ onClick:
56+
handler
57+
(preventDefault >>> stopPropagation)
58+
(\_ -> props.onAddOption)
59+
, children: [ H.text "Add Option" ]
60+
}
61+
, H.div_
62+
[ H.span_
63+
[ H.text "options"
64+
]
65+
, H.ul_
66+
(mapFlipped
67+
props.options
68+
(\{ label, value } ->
69+
H.li_
70+
[ H.span_
71+
[ H.text label ]
72+
, H.span_
73+
[ H.text ":" ]
74+
, H.span_
75+
[ H.text value ]
76+
]))
77+
]
78+
]

0 commit comments

Comments
 (0)