{-# LANGUAGE DuplicateRecordFields #-}
{-# OPTIONS_GHC -Wno-missing-poly-kind-signatures #-}
{-# OPTIONS_GHC -Wno-missing-role-annotations #-}
{-# OPTIONS_GHC -Wno-term-variable-capture #-}

module Dashi.Components.Select where

import Clay hiding (label, name, none, selected, value)
import Dashi.Prelude hiding ((&))
import Dashi.Style.Pseudo (pressable)
import Miso.Html.Element (option_, select_)
import Miso.Html.Property (disabled_, name_, selected_, value_)

data Option model action = Option
    { forall model action. Option model action -> MisoString
value :: MisoString
    , forall model action. Option model action -> [View model action]
label :: [View model action]
    , forall model action. Option model action -> Bool
selected :: Bool
    }

data Select o model action = Select
    { forall o model action. Select o model action -> MisoString
name :: MisoString
    , forall o model action. Select o model action -> [o]
options :: [o]
    , forall o model action. Select o model action -> o -> Bool
selected :: o -> Bool
    , forall o model action. Select o model action -> o -> MisoString
value :: o -> MisoString
    , forall o model action.
Select o model action -> o -> [View model action]
label :: o -> [View model action]
    }

instance Widget (Option model action) model action where
    widget' :: [Attribute action] -> Option model action -> View model action
widget' [Attribute action]
attrs Option{Bool
[View model action]
MisoString
value :: forall model action. Option model action -> MisoString
label :: forall model action. Option model action -> [View model action]
selected :: forall model action. Option model action -> Bool
value :: MisoString
label :: [View model action]
selected :: Bool
..} = [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
option_ (MisoString -> Attribute action
forall action. MisoString -> Attribute action
value_ MisoString
value Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: Bool -> Attribute action
forall action. Bool -> Attribute action
selected_ Bool
selected Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: [Attribute action]
attrs) [View model action]
label
    style :: Css
style = () -> Css
forall a. a -> StyleM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

instance (Eq o) => Widget (Select o model action) model action where
    widget' :: [Attribute action] -> Select o model action -> View model action
widget' [Attribute action]
attrs Select{[o]
MisoString
o -> Bool
o -> [View model action]
o -> MisoString
name :: forall o model action. Select o model action -> MisoString
options :: forall o model action. Select o model action -> [o]
selected :: forall o model action. Select o model action -> o -> Bool
value :: forall o model action. Select o model action -> o -> MisoString
label :: forall o model action.
Select o model action -> o -> [View model action]
name :: MisoString
options :: [o]
selected :: o -> Bool
value :: o -> MisoString
label :: o -> [View model action]
..} =
        [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
select_ (MisoString -> Attribute action
forall action. MisoString -> Attribute action
name_ MisoString
name Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: [Attribute action]
attrs)
            ([View model action] -> View model action)
-> [View model action] -> View model action
forall a b. (a -> b) -> a -> b
$ [[Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
option_ [MisoString -> Attribute action
forall action. MisoString -> Attribute action
value_ MisoString
"", Attribute action
forall action. Attribute action
disabled_, Bool -> Attribute action
forall action. Bool -> Attribute action
selected_ Bool
True] [] | (o -> Bool) -> [o] -> Bool
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Bool
none o -> Bool
selected [o]
options]
            [View model action] -> [View model action] -> [View model action]
forall a. Semigroup a => a -> a -> a
<> [ Option model action -> View model action
forall w model action.
Widget w model action =>
w -> View model action
widget
                    Option
                        { value :: MisoString
value = o -> MisoString
value o
o
                        , label :: [View model action]
label = o -> [View model action]
label o
o
                        , selected :: Bool
selected = o -> Bool
selected o
o
                        }
               | o
o <- [o]
options
               ]
    style :: Css
style =
        Selector
Clay.select Selector -> Css -> Css
? do
            Css
pressable
            Refinement
before Refinement -> Css -> Css
& do
                Display -> Css
display Display
block
                Content -> Css
content (Content -> Css) -> Content -> Css
forall a b. (a -> b) -> a -> b
$ MisoString -> Content
stringContent MisoString
"x"