-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathForm.purs
986 lines (925 loc) · 33 KB
/
Form.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
module Lumi.Components.Form
( module Defaults
, module Internal
, module Validation
, build
, static
, section
, inputBox
, textbox
, passwordBox
, textarea
, switch
, radioGroup
, file
, genericSelect
, class GenericSelect
, fromString
, toString
, select
, multiSelect
, asyncSelect
, asyncSelectByKey
, number
, array
, fixedSizeArray
, arrayModal
, fetch
, fetch_
, asyncEffect
, effect
, initializer
, via
, focus
, match
, match_
, withProps
, withValue
, mapProps
, indent
, filterWithProps
, withKey
, styles
) where
import Prelude
import Color (cssStringHSLA)
import Data.Array ((:))
import Data.Array as Array
import Data.Either (Either(..))
import Data.Foldable (fold, surround)
import Data.Generic.Rep (class Generic, Constructor(..), NoArguments(..), Sum(..), to, from)
import Data.Lens (Iso', Lens', Prism, Prism', matching, review, view)
import Data.Maybe (Maybe(..), fromMaybe, isJust, isNothing, maybe)
import Data.Monoid (guard)
import Data.Newtype (un)
import Data.Nullable (notNull, null, toNullable)
import Data.String as String
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Traversable (intercalate, traverse, traverse_)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import JSS (JSS, jss)
import Lumi.Components.Color (colorNames, colors)
import Lumi.Components.Column (column)
import Lumi.Components.FetchCache as FetchCache
import Lumi.Components.Form.Defaults (formDefaults) as Defaults
import Lumi.Components.Form.Internal (FormBuilder(..), SeqFormBuilder, Tree(..), formBuilder, formBuilder_, invalidate, pruneTree, sequential)
import Lumi.Components.Form.Internal (FormBuilder, SeqFormBuilder, formBuilder, formBuilder_, invalidate, listen, parallel, revalidate, sequential) as Internal
import Lumi.Components.Form.Validation (Validated(..), Validator, _Validated, fromValidated, mustBe, mustEqual, nonEmpty, nonEmptyArray, nonNull, validNumber, validInt, optional, setFresh, setModified, validated, warn) as Validation
import Lumi.Components.Input (alignToInput)
import Lumi.Components.Input as Input
import Lumi.Components.LabeledField (RequiredField(..), labeledField, labeledFieldValidationErrorStyles, labeledFieldValidationWarningStyles)
import Lumi.Components.Link as Link
import Lumi.Components.Loader (loader)
import Lumi.Components.Modal (modalLink)
import Lumi.Components.NativeSelect as NativeSelect
import Lumi.Components.Orientation (Orientation(..))
import Lumi.Components.Row (row)
import Lumi.Components.Select as Select
import Lumi.Components.Text (body, body_, subsectionHeader, text)
import Lumi.Components.Textarea as Textarea
import Lumi.Components.Upload as Upload
import Prim.Row (class Nub, class Union)
import React.Basic (JSX, createComponent, element, empty, fragment, keyed, makeStateless)
import React.Basic.Components.Async (async, asyncWithLoader)
import React.Basic.DOM as R
import React.Basic.DOM.Events (capture, stopPropagation, targetChecked, targetValue)
import React.Basic.Events as Events
import Unsafe.Coerce (unsafeCoerce)
-- | Create a React component for a form from a `FormBuilder`.
-- |
-- | _Note_: this function should be fully applied, to avoid remounting
-- | the component on each render.
build
:: forall props unvalidated result
. FormBuilder { readonly :: Boolean | props } unvalidated result
-> { value :: unvalidated
, onChange :: (unvalidated -> unvalidated) -> Effect Unit
, inlineTable :: Boolean
, forceTopLabels :: Boolean
, readonly :: Boolean
| props
}
-> JSX
build editor = makeStateless (createComponent "Form") render where
render props@{ value, onChange, inlineTable, forceTopLabels, readonly } =
let forest = Array.mapMaybe pruneTree $ edit onChange
where
props' = contractProps props
{ edit } = un FormBuilder editor props' value
contractProps
:: { value :: unvalidated
, onChange :: (unvalidated -> unvalidated) -> Effect Unit
, inlineTable :: Boolean
, forceTopLabels :: Boolean
, readonly :: Boolean
| props
}
-> { readonly :: Boolean
| props
}
contractProps = unsafeCoerce
fieldDivider = R.hr { className: "lumi field-divider" }
toRow = case _ of
Child { key, child } ->
maybe identity keyed key $ child
Wrapper { key, children } ->
R.div
{ key: fromMaybe "" key
, children: [ intercalate fieldDivider (map toRow children) ]
}
Node { label, key, required, validationError, children } ->
maybe identity keyed key $ labeledField
{ label: text body
{ children = [ label ]
, className = toNullable (pure "field-label")
}
, value: intercalate fieldDivider (map toRow children)
, validationError: validationError
, required: required
, forceTopLabel: forceTopLabels
, style: R.css {}
}
in element (R.unsafeCreateDOMComponent "lumi-form")
{ "class": String.joinWith " " $ fold
[ guard inlineTable ["inline-table"]
, guard readonly ["readonly"]
]
, children: surround fieldDivider (map toRow forest)
}
-- | Create an always-valid `FormBuilder` that renders the supplied `JSX`.
static :: forall props value. JSX -> FormBuilder props value Unit
static edit = formBuilder \_ _ -> { edit: const edit, validate: pure unit }
-- | A formatted section header used to visually separate the parts of a form
section :: forall props value. String -> FormBuilder props value Unit
section title =
static $
text subsectionHeader
{ children = [ R.text title ]
, style = R.css
{ marginBottom: "16px"
, display: "block"
}
}
-- | A configurable input box makes a `FormBuilder` for strings
inputBox
:: forall props
. Input.InputProps
-> FormBuilder
{ readonly :: Boolean | props }
String
String
inputBox inputProps = formBuilder_ \{ readonly } s onChange ->
if readonly
then Input.alignToInput $ body_ s
else Input.input inputProps
{ value = s
, onChange = capture targetValue (traverse_ onChange)
, style = R.css { width: "100%" }
}
-- | A simple text box makes a `FormBuilder` for strings
textbox
:: forall props
. FormBuilder
{ readonly :: Boolean | props }
String
String
textbox = inputBox Input.text_
-- | A simple password box makes a `FormBuilder` for strings
passwordBox
:: forall props
. FormBuilder
{ readonly :: Boolean | props }
String
String
passwordBox = inputBox Input.password
-- | A simple text box makes a `FormBuilder` for strings
textarea
:: forall props
. FormBuilder
{ readonly :: Boolean | props }
String
String
textarea = formBuilder_ \{ readonly } s onChange ->
if readonly
then Input.alignToInput $ R.text s
else Textarea.textarea Textarea.defaults
{ value = s
, onChange = capture targetValue (traverse_ onChange)
, style = R.css { width: "100%" }
}
-- | A `switch` is an editor for booleans which displays Yes or No.
switch
:: forall props
. FormBuilder
{ readonly :: Boolean | props }
Boolean
Boolean
switch = formBuilder_ \{ readonly } b onChange ->
if readonly
then Input.alignToInput $ R.text (if b then "Yes" else "No")
else Input.input Input.switch
{ checked = if b then Input.On else Input.Off
, onChange = Events.handler (stopPropagation >>> targetChecked) (traverse_ onChange)
}
-- | A form that edits an optional structure represented by group of radio
-- | buttons, visually oriented in either horizontal or vertical fashion.
-- |
-- | This is similar to `select`, but displays radio buttons instead.
radioGroup
:: forall props a
. Eq a
=> Orientation
-> Array { label :: JSX, value :: a }
-> FormBuilder { readonly :: Boolean | props } (Maybe a) (Maybe a)
radioGroup orientation options = formBuilder_ \props selected onChange ->
wrapper $ options <#> \{ label, value } ->
Input.label
{ style: R.css
{ flexDirection: "row"
, alignSelf: "stretch"
}
, for: null
, children:
[ Input.input Input.radio
{ style = R.css { marginRight: 8 }
, checked = if Just value == selected then Input.On else Input.Off
, onChange =
Events.handler targetChecked \s ->
when (fromMaybe false s) $
onChange (Just value)
}
, lumiAlignToInput
{ style: { flex: "1" }
, children: label
}
]
}
where
lumiAlignToInput = element (R.unsafeCreateDOMComponent "lumi-align-to-input")
wrapper :: Array JSX -> JSX
wrapper children =
case orientation of
Horizontal ->
row
{ children
, style: R.css
{ alignItems: "center"
, justifyContent: "flex-start"
, flexWrap: "wrap"
}
}
Vertical ->
column
{ children
, style: R.css { alignItems: "flex-start" }
}
-- | A editor consisting of a file picker.
file
:: forall props
. { variant :: Upload.UploadVariant
, backend :: Upload.UploadBackend
}
-> FormBuilder
{ readonly :: Boolean | props }
(Array Upload.FileId)
(Array Upload.FileId)
file opts = formBuilder_ \{ readonly } value onChange ->
Upload.upload Upload.defaults
{ value = value
, variant = opts.variant
, onChange = onChange
, readonly = readonly
, backend = opts.backend
}
-- | A editor consisting of a single-select dropdown.
select
:: forall props a
. (a -> String)
-> (String -> Maybe a)
-> Array { label :: String, value :: a }
-> FormBuilder { readonly :: Boolean | props } (Maybe a) (Maybe a)
select toString fromString opts = formBuilder_ \{ readonly } selected onChange ->
if readonly
then
Input.alignToInput $ R.text (maybe "" toString selected)
else
NativeSelect.nativeSelect NativeSelect.defaults
{ options = { label: "Select an option ...", value: "" } : map (\{ label, value } -> { label, value: toString value }) opts
, onChange = capture targetValue \newValue -> onChange (fromString =<< newValue)
, value = maybe "" toString selected
}
class GenericSelect rep where
fromString :: String -> Maybe rep
toString :: rep -> String
instance sumGenericSelect :: (GenericSelect a, GenericSelect b) => GenericSelect (Sum a b) where
toString (Inl a) = toString a
toString (Inr a) = toString a
fromString s = case fromString s of
Just s'-> Just (Inl s')
Nothing -> map Inr (fromString s)
instance sumGenericSelectConstructor :: (IsSymbol name) => GenericSelect (Constructor name NoArguments) where
toString _ = reflectSymbol (SProxy :: SProxy name)
fromString s | s == reflectSymbol (SProxy :: SProxy name) = Just (Constructor NoArguments)
| otherwise = Nothing
-- | given values which are composed from a sum-type, generate a select dropdown from its elements
-- |
-- | data Yup = One | Two
-- | genericSelect [{label: "1st", value: One}, {label: "2nd", value: Two}]
-- | This would serialize to- and from select options automatically
genericSelect
:: forall props a rep
. Generic a rep
=> GenericSelect rep
=> Array { label :: String, value :: a }
-> FormBuilder { readonly :: Boolean | props } (Maybe a) (Maybe a)
genericSelect opts =
select (toString <<< from) (map to <<< fromString) opts
-- | An editor consisting of a multi-select dropdown.
multiSelect
:: forall props a
. (a -> String)
-> Array { label :: String, value :: a }
-> FormBuilder { readonly :: Boolean | props } (Array a) (Array a)
multiSelect encode opts = formBuilder_ \{ readonly } selected onChange ->
if readonly
then
alignToInput $ R.text (String.joinWith "," $ map encode selected)
else
Select.multiSelect
{ value:
selected # Array.mapMaybe \sel ->
opts # Array.find \{ value } -> encode value == encode sel
, options: opts
, optionSort: Nothing
, onChange: onChange <<< map _.value
, className: ""
, style: R.css {}
, searchable: true
, id: ""
, name: ""
, noResultsText: "No results"
, placeholder: "Select an option ..."
, disabled: false
, loading: false
, optionRenderer: R.text <<< _.label
, toSelectOption: \r -> { value: encode r.value, label: r.label }
}
-- | An editor which uses an API call to populate a single-select
-- | drop-down.
asyncSelect
:: forall props a
. (String -> Aff (Array a))
-> (a -> Select.SelectOption)
-> (a -> JSX)
-> FormBuilder { readonly :: Boolean | props } (Maybe a) (Maybe a)
asyncSelect loadOptions toSelectOption optionRenderer =
formBuilder_ \props@{ readonly } value onChange ->
if readonly
then case value of
Nothing -> empty
Just value_ -> alignToInput $
text body
{ children = [ optionRenderer value_ ]
}
else Select.asyncSingleSelect
{ value
, loadOptions
, onChange: onChange
, className: ""
, style: R.css {}
, searchable: true
, id: ""
, name: ""
, disabled: false
, loading: false
, noResultsText: "No results"
, placeholder: "Search ..."
, optionRenderer
, optionSort: Nothing
, toSelectOption
}
-- | Similar to `asyncSelect` but allows the current value to be specified using only its key.
asyncSelectByKey
:: forall props id a
. (id -> Aff a)
-> (String -> Aff (Array a))
-> (id -> String)
-> (String -> id)
-> (a -> Select.SelectOption)
-> (a -> JSX)
-> FormBuilder { readonly :: Boolean | props } (Maybe id) (Maybe id)
asyncSelectByKey getData loadOptions fromId toId toSelectOption optionRenderer =
formBuilder_ \props@{ readonly } value onChange ->
FetchCache.single
{ getData: getData <<< toId
, id: map fromId value
, render: \data_ ->
if readonly
then case value of
Nothing -> empty
Just _ -> alignToInput
case data_ of
Nothing ->
loader
{ style: R.css { width: "20px", height: "20px", borderWidth: "2px" }
, testId: toNullable Nothing
, color: colorNames.black2
, bgColor: colorNames.white
}
Just data_' -> text body
{ children = [ optionRenderer data_' ]
}
else
Select.asyncSingleSelect
{ value: data_
, loadOptions
, onChange: onChange <<< map (toId <<< _.value <<< toSelectOption)
, className: ""
, style: R.css {}
, searchable: true
, id: ""
, name: ""
, disabled: false
, loading: isJust value && isNothing data_
, noResultsText: "No results"
, placeholder: "Search ..."
, optionRenderer
, optionSort: Nothing
, toSelectOption
}
}
-- | A form which edits a number. The form produces a string as a result in
-- | order to allow more control over validation (e.g. to allow special
-- | handling of the empty string, or to distinguish 1, 1.0, and 1.00000 from
-- | each other).
-- |
-- | See also `validNumber` from the Validation module.
number
:: forall props
. { min :: Maybe Number
, max :: Maybe Number
, step :: Input.InputStep
}
-> FormBuilder { readonly :: Boolean | props } String String
number { min, max, step } = formBuilder \{ readonly } value ->
{ edit: \onChange ->
if readonly
then Input.alignToInput (R.text value)
else
Input.input Input.number
{ value = value
, onChange = Events.handler targetValue (traverse_ (onChange <<< const))
, style = R.css { width: "100%" }
, min = toNullable min
, max = toNullable max
, step = notNull step
}
, validate: Just value
}
-- | Edit an `Array` of values.
-- |
-- | This `FormBuilder` displays a removable section for each array element,
-- | along with an "Add..." button in the final row.
array
:: forall props u a
. { label :: String
, addLabel :: String
, defaultValue :: u
, editor :: FormBuilder { readonly :: Boolean | props } u a
}
-> FormBuilder { readonly :: Boolean | props } (Array u) (Array a)
array { label, addLabel, defaultValue, editor } = FormBuilder \props@{ readonly } xs ->
let editAt i f xs' = fromMaybe xs' (Array.modifyAt i f xs')
wrapper children = Array.singleton $ Wrapper { key: Nothing, children }
in { edit: \onChange ->
wrapper $ xs # Array.mapWithIndex (\i x ->
Node
{ label:
fragment
[ if readonly
then empty
else Link.link Link.defaults
{ navigate = pure $ onChange (\xs' -> fromMaybe xs' (Array.deleteAt i xs'))
, text = R.text "×"
}
, R.text " "
, R.text label
, R.text (" #" <> show (i + 1))
]
, key: Nothing
, required: Neither
, validationError: Nothing
, children: (un FormBuilder editor props x).edit (onChange <<< editAt i)
})
# if readonly then identity else flip Array.snoc
(Child
{ key: Nothing
, child:
Link.link Link.defaults
{ navigate = pure $ onChange (flip append [defaultValue])
, className = pure "lumi"
, text = Input.alignToInput $ body_ ("+ " <> addLabel)
}
})
, validate: traverse (un FormBuilder editor props >>> _.validate) xs
}
-- | Edit an `Array` of values without letting the user add or remove entries.
fixedSizeArray
:: forall props u a
. { label :: String
, editor :: FormBuilder { readonly :: Boolean | props } u a
}
-> FormBuilder { readonly :: Boolean | props } (Array u) (Array a)
fixedSizeArray { label, editor } = FormBuilder \props xs ->
let editAt i f xs' = fromMaybe xs' (Array.modifyAt i f xs')
in { edit: \onChange ->
xs # Array.mapWithIndex (\i x ->
Node
{ key: Just (show i)
, label:
fragment
[ R.text " "
, R.text label
, R.text (" #" <> show (i + 1))
]
, required: Neither
, validationError: Nothing
, children: (un FormBuilder editor props x).edit (onChange <<< editAt i)
})
, validate: traverse (un FormBuilder editor props >>> _.validate) xs
}
-- | Edit an `Array` of values.
-- |
-- | Unlike `array`, this `FormBuilder` uses a modal popup for adding and
-- | editing array elements.
-- |
-- | `Note`: `arrayModal` does not support validation, in the sense that the
-- | component _inside_ the modal popup cannot reject its form state when the
-- | use clicks the save button.
arrayModal
:: forall props componentProps a
. Union
componentProps
( value :: a
, onChange :: a -> Effect Unit
)
( value :: a
, onChange :: a -> Effect Unit
| componentProps
)
=> Nub
( value :: a
, onChange :: a -> Effect Unit
| componentProps
)
( value :: a
, onChange :: a -> Effect Unit
| componentProps
)
=> { label :: String
, addLabel :: String
, defaultValue :: a
, summary :: { readonly :: Boolean | props } -> a -> JSX
, component :: { value :: a
, onChange :: a -> Effect Unit
| componentProps
}
-> JSX
, componentProps :: Record componentProps
}
-> FormBuilder { readonly :: Boolean | props } (Array a) (Array a)
arrayModal { label, addLabel, defaultValue, summary, component, componentProps } = FormBuilder \props@{ readonly } xs ->
let editAt i f xs' = fromMaybe xs' (Array.modifyAt i f xs')
wrapper children = Array.singleton $ Wrapper { key: Nothing, children }
in { edit : \onChange ->
wrapper $ xs # Array.mapWithIndex (\i x ->
Node
{ label:
fragment
[ if readonly
then empty
else Link.link Link.defaults
{ navigate = pure $ onChange (\xs' -> fromMaybe xs' (Array.deleteAt i xs'))
, text = R.text "×"
}
, R.text " "
, R.text label
, R.text (" #" <> show (i + 1))
]
, key: Nothing
, required: Neither
, validationError: Nothing
, children: pure $ Child $
{ key: Nothing
, child:
Input.alignToInput
if readonly
then summary props x
else modalLink
{ label: summary props x
, title: addLabel
, value: x
, onChange: onChange <<< editAt i <<< const
, actionButtonTitle: addLabel
, component
, componentProps
, style: R.css {}
}
}
})
# if readonly then identity else flip Array.snoc
(Child
{ key: Nothing
, child:
Input.alignToInput $
modalLink
{ label: body_ ("+ " <> addLabel)
, title: addLabel
, value: defaultValue
, onChange: \x -> onChange (flip append [x])
, actionButtonTitle: addLabel
, component
, componentProps
, style: R.css {}
}
})
, validate: pure xs
}
-- | Form that performs an asynchronous effect whenever `id` changes.
-- | The result is `Nothing` while the effect is not completed, and a `Just`
-- | after the value is available.
fetch :: forall props a. JSX -> String -> (String -> Aff a) -> FormBuilder props (Maybe a) (Maybe a)
fetch = \loading id getData ->
formBuilder_ \props value onChange ->
FetchCache.single
{ id: Just id
, getData: \id' -> do
liftEffect $ onChange Nothing
getData id'
, render: \v ->
if isJust v
then keyed id $ async (mempty <$ liftEffect (onChange v))
else loading
}
-- | Performs an asynchronous effect once and returns its result encapsulated in
-- | `Just`. The result is `Nothing` while the effect is not completed.
fetch_ :: forall props a. JSX -> Aff a -> FormBuilder props (Maybe a) (Maybe a)
fetch_ loading getData = fetch loading "" (const getData)
-- | A dummy form that, whenever the specified key changes, performs an
-- | asynchronous effect. It displays the specified JSX while the effect is not
-- | complete, sets the form data to the result of the effect and returns it.
asyncEffect :: forall props a. String -> JSX -> Aff (a -> a) -> FormBuilder props a a
asyncEffect key loader aff =
withKey key $ formBuilder \_ value ->
{ edit: \onChange ->
keyed key $ asyncWithLoader loader do
f <- aff
liftEffect $ onChange f
mempty
, validate: Just value
}
-- | A dummy form that, whenever the specified key changes, performs an
-- | effect. It sets the form data to the result of the effect and returns it.
effect :: forall props a. String -> Effect (a -> a) -> FormBuilder props a a
effect key = asyncEffect key mempty <<< liftEffect
-- | Sequential `SeqFormBuilder` used for asynchronously initializing some
-- | piece of form data, invalidating the form and preventing the rendering of
-- | subsequent fields while the supplied `Aff` is not completed.
-- |
-- | For example:
-- |
-- | ```purescript
-- | myForm = parallel do
-- | initializer mempty \props value -> do
-- | foo <- fetchDefaultFoo
-- | pure $ value
-- | { foo = foo
-- | , bar = props.bar
-- | }
-- |
-- | sequential ado
-- | foo <- focus (SProxy :: SProxy "foo") textbox
-- | bar <- focus (SProxy :: SProxy "bar") switch
-- | in { foo, bar }
-- | ```
initializer
:: forall props value
. Nub (initialized :: Boolean | value) (initialized :: Boolean | value)
=> JSX
-> (props -> {| value } -> Aff ({ initialized :: Boolean | value } -> { initialized :: Boolean | value }))
-> SeqFormBuilder props { initialized :: Boolean | value } Unit
initializer loader aff =
sequential "initializer" $ withValue \value@{ initialized } -> withProps \props ->
if initialized then
pure unit
else
invalidate
$ void
$ asyncEffect "" loader
$ map (_{ initialized = true } <<< _)
$ aff props (contractValue value)
where
contractValue :: { initialized :: Boolean | value } -> {| value }
contractValue = unsafeCoerce
-- | Modify a `FormBuilder` using a (partial) `Iso`.
-- |
-- | Technically, we don't require `to (from s) = s`, but we do require
-- | `from (to a) = a` for the form to act sensibly. Since there's no
-- | `PartialIso` in `profunctor-lenses`, we use `Iso` here.
-- |
-- | Caveat emptor, you get what you pay for if you pass in a dodgy
-- | `Iso` here.
via
:: forall props s a result
. Iso' s a
-> FormBuilder props a result
-> FormBuilder props s result
via i e = FormBuilder \props s ->
let { edit, validate } = un FormBuilder e props (view i s)
-- TODO: make this point-free
in { edit: \k -> edit (k <<< i)
, validate
}
-- | Focus a `FormBuilder` on a smaller piece of state, using a `Lens`.
focus
:: forall props s a result
. Lens' s a
-> FormBuilder props a result
-> FormBuilder props s result
focus l e = FormBuilder \props s ->
let { edit, validate } = un FormBuilder e props (view l s)
in { edit: \k -> edit (k <<< l)
, validate
}
-- | Focus a `FormBuilder` on a possible type of state, using a `Prism`,
-- | ignoring validation.
match_
:: forall props s a
. Prism' s a
-> FormBuilder props a a
-> FormBuilder props s s
match_ p = match p p
-- | Focus a `FormBuilder` on a possible type of state, using a `Prism`.
-- |
-- | We need two `Prism`s in order to change the result type for
-- | validation purposes.
match
:: forall props result s t a
. Prism s s a a
-> Prism s t a result
-> FormBuilder props a result
-> FormBuilder props s t
match p1 p2 e = FormBuilder \props s ->
case matching p2 s of
Left t -> { edit: mempty, validate: pure t }
Right a ->
let { edit, validate } = un FormBuilder e props a
in { edit: \k -> edit (k <<< p1)
, validate: map (review p2) validate
}
-- | Change the props type.
mapProps
:: forall p q u a
. (q -> p)
-> FormBuilder p u a
-> FormBuilder q u a
mapProps f form = FormBuilder (un FormBuilder form <<< f)
-- | Make the props available, for convenience.
withProps
:: forall props unvalidated result
. (props -> FormBuilder props unvalidated result)
-> FormBuilder props unvalidated result
withProps f = FormBuilder \props value -> un FormBuilder (f props) props value
-- | Make the value available, for convenience.
withValue
:: forall props unvalidated result
. (unvalidated -> FormBuilder props unvalidated result)
-> FormBuilder props unvalidated result
withValue f = FormBuilder \props value -> un FormBuilder (f value) props value
-- | Indent a `Forest` of editors by one level, providing a label.
indent
:: forall props u a
. String
-> RequiredField
-> FormBuilder props u a
-> FormBuilder props u a
indent label required editor = FormBuilder \props val ->
let { edit, validate } = un FormBuilder editor props val
in { edit: \k ->
pure $ Node
{ label: R.text label
, key: Nothing
, required: required
, validationError: Nothing
, children: edit k
}
, validate
}
-- | Filter parts of the form based on the current value (and the props).
filterWithProps
:: forall props u a
. (props -> u -> Boolean)
-> FormBuilder props u a
-> FormBuilder props u a
filterWithProps p editor = FormBuilder \props value ->
let { edit, validate } = un FormBuilder editor props value
in { edit: \onChange ->
if p props value
then edit onChange
else []
, validate
}
-- | TODO: document
withKey
:: forall props u a
. String
-> FormBuilder props u a
-> FormBuilder props u a
withKey key editor = FormBuilder \props value ->
let { edit, validate } = un FormBuilder editor props value
in { edit: \onChange ->
edit onChange # Array.mapWithIndex case _, _ of
i, Child a -> Child a { key = Just (key <> "--" <> show i) }
i, Wrapper a -> Wrapper a { key = Just (key <> "--" <> show i) }
i, Node n -> Node n { key = Just (key <> "--" <> show i) }
, validate
}
styles :: JSS
styles = jss
{ "@global":
{ "lumi-form":
{ "& hr.lumi.field-divider":
{ margin: "0.8rem 0"
, height: "0"
, display: "none"
, "@media (max-width: 860px)":
{ margin: "0.4rem 0"
}
}
-- hide outer borders in nested Forms
, "& .labeled-field--right > lumi-form > hr.lumi.field-divider":
{ "&:first-child, &:last-child": { display: "none" }
}
, "&.readonly label.lumi":
{ cursor: "auto"
, userSelect: "auto"
}
-- TODO: this will probably just be handled by lumi Link styles
, "& a.lumi":
{ color: cssStringHSLA colors.primary
, cursor: "pointer"
, "&:hover": { textDecoration: "underline" }
}
-- not InlineTable Form rules
, "& .labeled-field":
{ paddingBottom: "16px"
, "&[data-force-top-label=\"true\"] lumi-align-to-input":
{ padding: "0 0 4px 0"
}
}
, "&.inline-table":
{ -- If necessary, override the not(.inline-table)
-- rule above (for nested forms)
"& .labeled-field":
{ paddingBottom: "0"
, "&[data-force-top-label=\"true\"] lumi-align-to-input":
{ padding: "0"
}
}
, "& hr.lumi.field-divider":
{ height: "0.1rem"
, display: "block"
}
, "& label.lumi:not(:hover)":
{ "& input.lumi, & select.lumi":
{ borderColor: "transparent"
}
}
, "& lumi-body.field-label":
{ color: cssStringHSLA colors.black1
}
}
, "& .labeled-field--validation-error":
{ extend: labeledFieldValidationErrorStyles
, marginBottom: "calc(4 * 4px)"
}
, "& .labeled-field--validation-warning":
{ extend: labeledFieldValidationWarningStyles
, marginBottom: "calc(4 * 4px)"
}
, "& lumi-editable-table":
{ "& table tr td:not(.lumi)":
{ verticalAlign: "top"
, "& lumi-column":
{ flex: "1 1 auto"
}
}
, "& .labeled-field--validation-error, & .labeled-field--validation-warning":
{ marginBottom: "0"
}
}
}
}
}