Skip to content

Commit 2fae7b6

Browse files
committed
field widgets lite refactoring
1 parent 4ead298 commit 2fae7b6

File tree

5 files changed

+86
-133
lines changed

5 files changed

+86
-133
lines changed

ghcjs/delivery-calculator/src/App/Widgets/Menu.hs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,15 @@ menu st =
126126
Field.argsAction = PushUpdate . Instant,
127127
Field.argsEmitter = pushActionQueue st . Instant
128128
}
129-
( let disabled =
130-
st
131-
^. #modelState
132-
. #stOnlineOrOffline
133-
== Online
129+
( let eod =
130+
if st ^. #modelState . #stOnlineOrOffline == Offline
131+
then Enabled
132+
else Disabled
134133
in Field.defOpts @Model @Action
135-
& #optsDisabled
136-
.~ disabled
134+
& #optsEnabledOrDisabled
135+
.~ eod
137136
& #optsLabel
138-
.~ Just
139-
( inspectExchangeRate $ modelState st
140-
)
141-
& ( if disabled
142-
then
143-
(#optsTrailingWidget .~ Nothing)
144-
. ( #optsLeadingWidget .~ Nothing ::
145-
Field.Opts Model Action ->
146-
Field.Opts Model Action
147-
)
148-
else id
149-
)
137+
.~ Just (inspectExchangeRate $ modelState st)
150138
)
151139
<> Field.dynamicField
152140
Field.Args

ghcjs/miso-functora/src/Functora/Miso/Types.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module Functora.Miso.Types
4949
OnlineOrOffline (..),
5050
StaticOrDynamic (..),
5151
LeadingOrTrailing (..),
52+
EnabledOrDisabled (..),
5253
FocusedOrBlurred (..),
5354
OpenedOrClosed (..),
5455
Update (..),
@@ -543,6 +544,12 @@ data LeadingOrTrailing
543544
deriving stock (Eq, Ord, Show, Read, Enum, Bounded, Data, Generic)
544545
deriving (Binary) via GenericType LeadingOrTrailing
545546

547+
data EnabledOrDisabled
548+
= Enabled
549+
| Disabled
550+
deriving stock (Eq, Ord, Show, Read, Enum, Bounded, Data, Generic)
551+
deriving (Binary) via GenericType EnabledOrDisabled
552+
546553
data FocusedOrBlurred
547554
= Focused
548555
| Blurred

ghcjs/miso-functora/src/Functora/Miso/Widgets/Field.hs

Lines changed: 53 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Functora.Miso.Widgets.Field
44
Opts (..),
55
defOpts,
66
OptsWidget (..),
7-
OptsWidgetPair (..),
87
ModalWidget' (..),
98
truncateUnicode,
109
expandDynamicField,
@@ -46,49 +45,59 @@ data Full model action t f = Full
4645
data Opts model action = Opts
4746
{ optsIcon :: Icon.Icon -> View action,
4847
optsLabel :: Maybe Unicode,
49-
optsDisabled :: Bool,
5048
optsFullWidth :: Bool,
5149
optsPlaceholder :: Unicode,
5250
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],
5856
optsOnKeyDownAction :: Unicode -> KeyCode -> Update model,
5957
optsExtraAttributes :: [Attribute action],
6058
optsLeftRightViewer :: [View action] -> [View action] -> [View action],
59+
optsEnabledOrDisabled :: EnabledOrDisabled,
6160
optsExtraAttributesImage :: [Attribute action]
6261
}
6362
deriving stock (Generic)
6463

65-
data OptsWidgetPair model action = OptsWidgetPair
66-
{ optsWidgetPairEmpty :: OptsWidget model action,
67-
optsWidgetPairNonEmpty :: OptsWidget model action
68-
}
69-
deriving stock (Generic)
70-
7164
defOpts :: Opts model action
7265
defOpts =
7366
Opts
7467
{ optsIcon = Icon.icon @Icon.Fa,
7568
optsLabel = Nothing,
76-
optsDisabled = False,
7769
optsFullWidth = False,
7870
optsPlaceholder = mempty,
7971
optsOnInputAction = Nothing,
80-
optsLeadingWidget = Just $ OptsWidgetPair PasteWidget PasteWidget,
81-
optsTrailingWidget = Just $ OptsWidgetPair ClearWidget ClearWidget,
72+
optsTrailingWidgets = defTrailingWidgets,
8273
optsOnKeyDownAction = Jsm.enterOrEscapeBlur,
8374
optsExtraAttributes = mempty,
8475
optsLeftRightViewer = (<>),
76+
optsEnabledOrDisabled = Enabled,
8577
optsExtraAttributesImage = mempty
8678
}
8779

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+
8897
data OptsWidget model action
8998
= CopyWidget
90-
| ClearWidget
91-
| PasteWidget
99+
| ClearWidget [Attribute action]
100+
| PasteWidget [Attribute action]
92101
| ScanQrWidget
93102
| ShowOrHideWidget
94103
| ModalWidget (ModalWidget' model)
@@ -130,21 +139,10 @@ field ::
130139
[View action]
131140
field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts =
132141
( 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
148146
fieldModal args x1
149147
)
150148
<> ( case optsLabel opts of
@@ -190,8 +188,8 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
190188
$ onInput onInputAction,
191189
Just
192190
. disabled_
193-
$ opts
194-
^. #optsDisabled,
191+
$ optsEnabledOrDisabled opts
192+
== Disabled,
195193
fmap placeholder_
196194
$ if null placeholder
197195
then optsLabel opts
@@ -206,7 +204,6 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
206204
)
207205
]
208206
<> dummyWidgets
209-
<> leadingWidgets
210207
<> trailingWidgets
211208
<> ( if typ /= FieldTypeImage
212209
then mempty
@@ -241,10 +238,11 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
241238
optic = argsOptic args
242239
action = argsAction args
243240
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
248246
typ =
249247
fromMaybe FieldTypeText
250248
$ st
@@ -282,38 +280,12 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
282280
)
283281
mempty
284282
]
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+
)
317289
onBlurAction =
318290
action . PureUpdate $ \prev ->
319291
prev
@@ -421,8 +393,11 @@ passwordField args opts =
421393
( opts
422394
& #optsPlaceholder
423395
.~ ("Password" :: Unicode)
424-
& #optsLeadingWidget
425-
.~ Just (OptsWidgetPair ShowOrHideWidget ShowOrHideWidget)
396+
& #optsTrailingWidgets
397+
--
398+
-- TODO : add other widgets
399+
--
400+
.~ (\_ _ _ -> [ShowOrHideWidget])
426401
)
427402

428403
fieldIcon ::
@@ -437,8 +412,8 @@ fieldIcon full opts = \case
437412
$ case st ^? cloneTraversal optic . #fieldInput . #uniqueValue of
438413
Nothing -> PureUpdate id
439414
Just txt -> Jsm.shareText txt
440-
ClearWidget ->
441-
fieldIconSimple opts Icon.IconClose mempty
415+
ClearWidget attrs ->
416+
fieldIconSimple opts Icon.IconClose attrs
442417
. ( fromMaybe action $ optsOnInputAction opts
443418
)
444419
$ PureAndEffectUpdate
@@ -460,8 +435,8 @@ fieldIcon full opts = \case
460435
<> uidTxt
461436
<> "'); if (el) el.value = '';"
462437
)
463-
PasteWidget ->
464-
fieldIconSimple opts Icon.IconPaste mempty
438+
PasteWidget attrs ->
439+
fieldIconSimple opts Icon.IconPaste attrs
465440
. insertAction full
466441
$ Jsm.selectClipboard
467442
(st ^? cloneTraversal optic . #fieldOpfsName . _Just)

ghcjs/miso-functora/src/Functora/Miso/Widgets/FieldPairs.hs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,12 @@ fieldPairEditor
163163
( optsField opts
164164
& #optsPlaceholder
165165
.~ ("Label " <> idxTxt)
166-
& ( #optsLeadingWidget ::
167-
Lens'
168-
(Field.Opts model action)
169-
(Maybe (Field.OptsWidgetPair model action))
170-
)
171-
.~ Just
172-
( let w = Field.DownWidget optic idx mempty
173-
in Field.OptsWidgetPair w w
174-
)
175-
& #optsTrailingWidget
176-
.~ Just
177-
( let w = Field.UpWidget optic idx mempty
178-
in Field.OptsWidgetPair w w
179-
)
166+
& #optsTrailingWidgets
167+
.~ ( \_ _ _ ->
168+
[ Field.DownWidget optic idx mempty,
169+
Field.UpWidget optic idx mempty
170+
]
171+
)
180172
)
181173
<> Field.dynamicField
182174
Field.Args
@@ -198,26 +190,17 @@ fieldPairEditor
198190
. to userFieldType
199191
)
200192
)
201-
& ( #optsLeadingWidget ::
202-
Lens'
203-
(Field.Opts model action)
204-
(Maybe (Field.OptsWidgetPair model action))
205-
)
206-
.~ Just
207-
( let w =
208-
Field.ModalWidget
209-
$ Field.ModalFieldWidget
210-
optic
211-
idx
212-
#fieldPairValue
213-
Dynamic
214-
in Field.OptsWidgetPair w w
215-
)
216-
& #optsTrailingWidget
217-
.~ Just
218-
( let w = Field.DeleteWidget optic idx mempty
219-
in Field.OptsWidgetPair w w
220-
)
193+
& #optsTrailingWidgets
194+
.~ ( \_ _ _ ->
195+
[ Field.ModalWidget
196+
$ Field.ModalFieldWidget
197+
optic
198+
idx
199+
#fieldPairValue
200+
Dynamic,
201+
Field.DeleteWidget optic idx mempty
202+
]
203+
)
221204
)
222205
where
223206
idxTxt :: Unicode

ghcjs/miso-functora/src/Functora/Miso/Widgets/Money.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ moneyViewer Args {argsModel = st, argsOptic = optic, argsAction = action} opts =
5151
Field.argsEmitter = error "TODO_MONEY_EMITTER"
5252
}
5353
( Field.defOpts
54-
& #optsDisabled
55-
.~ True
54+
& #optsEnabledOrDisabled
55+
.~ Disabled
5656
& #optsPlaceholder
5757
.~ inspectCurrencyInfo
5858
( money ^. #moneyCurrency . #currencyOutput

0 commit comments

Comments
 (0)