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

Commit f667089

Browse files
committed
add multiple select
1 parent 2271a3a commit f667089

File tree

1 file changed

+84
-31
lines changed

1 file changed

+84
-31
lines changed

src/Component/App.purs

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Form =
2020
, optionLabel :: String
2121
, optionValue :: String
2222
, options :: Array Option
23+
, selects :: Array Select
2324
}
2425
type OptionLabel = String
2526
type OptionValue = String
@@ -29,13 +30,14 @@ type Name = String
2930
data Select = Select Name Label (Array Option)
3031

3132
type State =
32-
{ built :: Maybe Select
33+
{ built :: Maybe (Array Select)
3334
, form :: Form
3435
}
3536

3637
data Action
3738
= Noop
3839
| AddOption
40+
| AddSelect
3941
| BuildForm
4042
| EditLabel String
4143
| EditName String
@@ -48,8 +50,8 @@ component = createComponent "App"
4850
app :: JSX
4951
app = make component { initialState, render, update } {}
5052

51-
buildFromForm :: Form -> Select
52-
buildFromForm { name, options, label } = Select name label options
53+
buildFromForm :: Form -> Array Select
54+
buildFromForm { selects } = selects
5355

5456
initialForm :: Form
5557
initialForm =
@@ -58,6 +60,7 @@ initialForm =
5860
, optionLabel: ""
5961
, optionValue: ""
6062
, options: []
63+
, selects: []
6164
}
6265

6366
initialState :: State
@@ -135,25 +138,58 @@ render self@{ state: { built, form } } =
135138
self
136139
preventDefault
137140
(const AddOption)
138-
, children: [ H.text "Add" ]
141+
, children: [ H.text "Add Option" ]
139142
}
140-
]
141-
, H.div_
142-
[ H.span_
143-
[ H.text "options"
143+
, H.div_
144+
[ H.span_
145+
[ H.text "options"
146+
]
147+
, H.ul_
148+
(mapFlipped
149+
form.options
150+
(\(Option l v) ->
151+
H.li_
152+
[ H.span_
153+
[ H.text l ]
154+
, H.span_
155+
[ H.text v ]
156+
]))
157+
]
158+
, H.div_
159+
[ H.button
160+
{ onClick:
161+
capture
162+
self
163+
preventDefault
164+
(const AddSelect)
165+
, children: [ H.text "Add Select" ]
166+
}
144167
]
145-
, H.ul_
146-
(mapFlipped
147-
form.options
148-
(\(Option l v) ->
149-
H.li_
150-
[ H.span_
151-
[ H.text l ]
152-
, H.span_
153-
[ H.text v ]
154-
]
168+
, H.div_
169+
[ H.span_
170+
[ H.text "selects"
171+
]
172+
, H.ul_
173+
(mapFlipped
174+
form.selects
175+
(\(Select name label options) ->
176+
H.div_
177+
[ H.span_ [ H.text name ]
178+
, H.span_ [ H.text label ]
179+
, H.ul_
180+
(mapFlipped
181+
options
182+
(\(Option l v) ->
183+
H.li_
184+
[ H.span_
185+
[ H.text l ]
186+
, H.span_
187+
[ H.text v ]
188+
]))
189+
]
190+
)
155191
)
156-
)
192+
]
157193
]
158194
, H.div_
159195
[ H.button
@@ -168,18 +204,22 @@ render self@{ state: { built, form } } =
168204
, H.div_
169205
[ case built of
170206
Nothing -> H.text "Not build"
171-
Just (Select name label options') ->
172-
H.label_
173-
[ H.span_ [ H.text label ]
174-
, H.select
175-
{ name
176-
, children:
177-
mapFlipped
178-
options'
179-
(\(Option l v) ->
180-
H.option { children: [H.text l], value: v })
181-
}
182-
]
207+
Just selects ->
208+
H.div_
209+
(mapFlipped
210+
selects
211+
(\(Select name label options') ->
212+
H.label_
213+
[ H.span_ [ H.text label ]
214+
, H.select
215+
{ name
216+
, children:
217+
mapFlipped
218+
options'
219+
(\(Option l v) ->
220+
H.option { children: [H.text l], value: v })
221+
}
222+
]))
183223
]
184224
]
185225
}
@@ -200,6 +240,19 @@ update self@{ state } AddOption =
200240
, options = Array.snoc state.form.options (Option state.form.optionLabel state.form.optionValue)
201241
}
202242
}
243+
update self@{ state } AddSelect =
244+
Update
245+
state
246+
{ form =
247+
state.form
248+
{ label = ""
249+
, name = ""
250+
, options = []
251+
, optionLabel = ""
252+
, optionValue = ""
253+
, selects = Array.snoc state.form.selects (Select state.form.name state.form.label state.form.options)
254+
}
255+
}
203256
update self@{ state } BuildForm =
204257
Update state { built = Just (buildFromForm state.form), form = initialForm }
205258
update self@{ state } (EditLabel v) =

0 commit comments

Comments
 (0)