@@ -4,7 +4,6 @@ module Functora.Miso.Widgets.Field
4
4
Opts (.. ),
5
5
defOpts ,
6
6
OptsWidget (.. ),
7
- OptsWidgetPair (.. ),
8
7
ModalWidget' (.. ),
9
8
truncateUnicode ,
10
9
expandDynamicField ,
@@ -46,49 +45,59 @@ data Full model action t f = Full
46
45
data Opts model action = Opts
47
46
{ optsIcon :: Icon. Icon -> View action ,
48
47
optsLabel :: Maybe Unicode ,
49
- optsDisabled :: Bool ,
50
48
optsFullWidth :: Bool ,
51
49
optsPlaceholder :: Unicode ,
52
50
optsOnInputAction :: Maybe (Update model -> action ),
53
- --
54
- -- TODO : optsTrailingWidgets :: [ Unicode -> FocusedOrBlurred -> OptsWidget]
55
- --
56
- optsLeadingWidget :: Maybe ( OptsWidgetPair model action ),
57
- optsTrailingWidget :: Maybe ( OptsWidgetPair model action ) ,
51
+ optsTrailingWidgets ::
52
+ Unicode ->
53
+ FocusedOrBlurred ->
54
+ EnabledOrDisabled ->
55
+ [ OptsWidget model action ] ,
58
56
optsOnKeyDownAction :: Unicode -> KeyCode -> Update model ,
59
57
optsExtraAttributes :: [Attribute action ],
60
58
optsLeftRightViewer :: [View action ] -> [View action ] -> [View action ],
59
+ optsEnabledOrDisabled :: EnabledOrDisabled ,
61
60
optsExtraAttributesImage :: [Attribute action ]
62
61
}
63
62
deriving stock (Generic )
64
63
65
- data OptsWidgetPair model action = OptsWidgetPair
66
- { optsWidgetPairEmpty :: OptsWidget model action ,
67
- optsWidgetPairNonEmpty :: OptsWidget model action
68
- }
69
- deriving stock (Generic )
70
-
71
64
defOpts :: Opts model action
72
65
defOpts =
73
66
Opts
74
67
{ optsIcon = Icon. icon @ Icon. Fa ,
75
68
optsLabel = Nothing ,
76
- optsDisabled = False ,
77
69
optsFullWidth = False ,
78
70
optsPlaceholder = mempty ,
79
71
optsOnInputAction = Nothing ,
80
- optsLeadingWidget = Just $ OptsWidgetPair PasteWidget PasteWidget ,
81
- optsTrailingWidget = Just $ OptsWidgetPair ClearWidget ClearWidget ,
72
+ optsTrailingWidgets = defTrailingWidgets,
82
73
optsOnKeyDownAction = Jsm. enterOrEscapeBlur,
83
74
optsExtraAttributes = mempty ,
84
75
optsLeftRightViewer = (<>) ,
76
+ optsEnabledOrDisabled = Enabled ,
85
77
optsExtraAttributesImage = mempty
86
78
}
87
79
80
+ defTrailingWidgets ::
81
+ Unicode ->
82
+ FocusedOrBlurred ->
83
+ EnabledOrDisabled ->
84
+ [OptsWidget model action ]
85
+ defTrailingWidgets input fob = \ case
86
+ Enabled ->
87
+ case fob of
88
+ Focused | null input -> [PasteWidget mempty , ClearWidget hide]
89
+ Focused -> [PasteWidget mempty , ClearWidget mempty ]
90
+ Blurred -> [PasteWidget hide, ClearWidget hide]
91
+ Disabled ->
92
+ [PasteWidget hide, ClearWidget hide]
93
+ where
94
+ hide :: [Attribute action ]
95
+ hide = [style_ [(" display" , " none" )]]
96
+
88
97
data OptsWidget model action
89
98
= CopyWidget
90
- | ClearWidget
91
- | PasteWidget
99
+ | ClearWidget [ Attribute action ]
100
+ | PasteWidget [ Attribute action ]
92
101
| ScanQrWidget
93
102
| ShowOrHideWidget
94
103
| ModalWidget (ModalWidget' model )
@@ -130,21 +139,10 @@ field ::
130
139
[View action ]
131
140
field full@ Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts =
132
141
( do
133
- x0 <-
134
- catMaybes
135
- [ opts
136
- ^? # optsLeadingWidget
137
- . _Just
138
- . cloneTraversal widgetOptic,
139
- opts
140
- ^? # optsTrailingWidget
141
- . _Just
142
- . cloneTraversal widgetOptic
143
- ]
144
- x1 <-
145
- case x0 of
146
- ModalWidget w -> pure w
147
- _ -> mempty
142
+ x0 <- optsTrailingWidgets opts input focused eod
143
+ x1 <- case x0 of
144
+ ModalWidget w -> pure w
145
+ _ -> mempty
148
146
fieldModal args x1
149
147
)
150
148
<> ( case optsLabel opts of
@@ -190,8 +188,8 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
190
188
$ onInput onInputAction,
191
189
Just
192
190
. disabled_
193
- $ opts
194
- ^. # optsDisabled ,
191
+ $ optsEnabledOrDisabled opts
192
+ == Disabled ,
195
193
fmap placeholder_
196
194
$ if null placeholder
197
195
then optsLabel opts
@@ -206,7 +204,6 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
206
204
)
207
205
]
208
206
<> dummyWidgets
209
- <> leadingWidgets
210
207
<> trailingWidgets
211
208
<> ( if typ /= FieldTypeImage
212
209
then mempty
@@ -241,10 +238,11 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
241
238
optic = argsOptic args
242
239
action = argsAction args
243
240
placeholder = optsPlaceholder opts
244
- widgetOptic =
245
- if null . fromMaybe mempty $ getInput st
246
- then # optsWidgetPairEmpty
247
- else # optsWidgetPairNonEmpty
241
+ eod = optsEnabledOrDisabled opts
242
+ input =
243
+ fromMaybe mempty $ st ^? cloneTraversal optic . # fieldInput . # uniqueValue
244
+ focused =
245
+ fromMaybe Blurred $ st ^? cloneTraversal optic . # fieldFocusState
248
246
typ =
249
247
fromMaybe FieldTypeText
250
248
$ st
@@ -282,38 +280,12 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
282
280
)
283
281
mempty
284
282
]
285
- leadingWidgets = do
286
- let focused =
287
- st ^? cloneTraversal optic . # fieldFocusState == Just Focused
288
- maybeToList
289
- $ fmap
290
- ( ( if focused
291
- then id
292
- else appendAttrs [style_ [(" display" , " none" )]]
293
- )
294
- . fieldIcon full opts
295
- )
296
- ( opts
297
- ^? # optsLeadingWidget
298
- . _Just
299
- . cloneTraversal widgetOptic
300
- )
301
- trailingWidgets = do
302
- let focused =
303
- st ^? cloneTraversal optic . # fieldFocusState == Just Focused
304
- maybeToList
305
- $ fmap
306
- ( ( if focused
307
- then id
308
- else appendAttrs [style_ [(" display" , " none" )]]
309
- )
310
- . fieldIcon full opts
311
- )
312
- ( opts
313
- ^? # optsTrailingWidget
314
- . _Just
315
- . cloneTraversal widgetOptic
316
- )
283
+ trailingWidgets =
284
+ fmap
285
+ ( fieldIcon full opts
286
+ )
287
+ ( optsTrailingWidgets opts input focused eod
288
+ )
317
289
onBlurAction =
318
290
action . PureUpdate $ \ prev ->
319
291
prev
@@ -421,8 +393,11 @@ passwordField args opts =
421
393
( opts
422
394
& # optsPlaceholder
423
395
.~ (" Password" :: Unicode )
424
- & # optsLeadingWidget
425
- .~ Just (OptsWidgetPair ShowOrHideWidget ShowOrHideWidget )
396
+ & # optsTrailingWidgets
397
+ --
398
+ -- TODO : add other widgets
399
+ --
400
+ .~ (\ _ _ _ -> [ShowOrHideWidget ])
426
401
)
427
402
428
403
fieldIcon ::
@@ -437,8 +412,8 @@ fieldIcon full opts = \case
437
412
$ case st ^? cloneTraversal optic . # fieldInput . # uniqueValue of
438
413
Nothing -> PureUpdate id
439
414
Just txt -> Jsm. shareText txt
440
- ClearWidget ->
441
- fieldIconSimple opts Icon. IconClose mempty
415
+ ClearWidget attrs ->
416
+ fieldIconSimple opts Icon. IconClose attrs
442
417
. ( fromMaybe action $ optsOnInputAction opts
443
418
)
444
419
$ PureAndEffectUpdate
@@ -460,8 +435,8 @@ fieldIcon full opts = \case
460
435
<> uidTxt
461
436
<> " '); if (el) el.value = '';"
462
437
)
463
- PasteWidget ->
464
- fieldIconSimple opts Icon. IconPaste mempty
438
+ PasteWidget attrs ->
439
+ fieldIconSimple opts Icon. IconPaste attrs
465
440
. insertAction full
466
441
$ Jsm. selectClipboard
467
442
(st ^? cloneTraversal optic . # fieldOpfsName . _Just)
0 commit comments