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

module Dashi.Components.Radio where

import Clay hiding
    ( fullWidth
    , label
    , legend
    , name
    , option
    , selected
    , span_
    , type_
    )
import Dashi.Components.Icon (Phosphor (Circle, RadioButton), iconContent)
import Dashi.Components.Util (ariaRole_)
import Dashi.Prelude hiding ((#), (&))
import Dashi.Style.Util
import Miso.Html.Element (fieldset_, input_, label_, span_)
import Miso.Html.Event (onChange)
import Miso.Html.Property (checked_, name_, type_)

data Radio model action = Radio
    { forall model action. Radio model action -> MisoString
name :: MisoString
    , forall model action. Radio model action -> [View model action]
label :: [View model action]
    , forall model action. Radio model action -> Bool
selected :: Bool
    , forall model action. Radio model action -> action
onSelect :: action
    }

instance Widget (Radio model action) model action where
    widget' :: [Attribute action] -> Radio model action -> View model action
widget' [Attribute action]
attrs Radio{action
Bool
[View model action]
MisoString
name :: forall model action. Radio model action -> MisoString
label :: forall model action. Radio model action -> [View model action]
selected :: forall model action. Radio model action -> Bool
onSelect :: forall model action. Radio model action -> action
name :: MisoString
label :: [View model action]
selected :: Bool
onSelect :: action
..} =
        [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
label_
            []
            [ [Attribute action] -> View model action
forall action model. [Attribute action] -> View model action
input_ ([Attribute action] -> View model action)
-> [Attribute action] -> View model action
forall a b. (a -> b) -> a -> b
$ MisoString -> Attribute action
forall action. MisoString -> Attribute action
type_ MisoString
"radio"
                Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: MisoString -> Attribute action
forall action. MisoString -> Attribute action
name_ MisoString
name
                Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: Bool -> Attribute action
forall action. Bool -> Attribute action
checked_ Bool
selected
                Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: (MisoString -> action) -> Attribute action
forall action. (MisoString -> action) -> Attribute action
onChange (action -> MisoString -> action
forall a b. a -> b -> a
const action
onSelect)
                Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: [Attribute action]
attrs
            , [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
span_ [] [View model action]
label
            ]
    style :: Css
style =
        -- Shared style is applied in the Checkbox component
        Selector
input Selector -> Refinement -> Selector
# (MisoString
"type" MisoString -> MisoString -> Refinement
@= MisoString
"radio") Selector -> Css -> Css
? do
            Size Percentage -> Css
forall a. Size a -> Css
borderRadiusAll (Size Percentage -> Css) -> Size Percentage -> Css
forall a b. (a -> b) -> a -> b
$ Number -> Size Percentage
pct Number
50
            Refinement
before Refinement -> Css -> Css
& Content -> Css
content (Phosphor -> Content
iconContent Phosphor
Circle)
            Refinement
checked Refinement -> Refinement -> Refinement
forall a. Semigroup a => a -> a -> a
<> Refinement
before Refinement -> Css -> Css
& Content -> Css
content (Phosphor -> Content
iconContent Phosphor
RadioButton)

data RadioGroup o model action = RadioGroup
    { forall o model action. RadioGroup o model action -> MisoString
name :: MisoString
    , forall o model action. RadioGroup o model action -> [o]
options :: [o]
    , forall o model action.
RadioGroup o model action -> o -> [View model action]
label :: o -> [View model action]
    , forall o model action. RadioGroup o model action -> Maybe o
selected :: Maybe o
    , forall o model action. RadioGroup o model action -> o -> action
onSelect :: o -> action
    }

instance (Eq a) => Widget (RadioGroup a model action) model action where
    widget' :: [Attribute action]
-> RadioGroup a model action -> View model action
widget' [Attribute action]
attrs RadioGroup{[a]
Maybe a
MisoString
a -> action
a -> [View model action]
name :: forall o model action. RadioGroup o model action -> MisoString
options :: forall o model action. RadioGroup o model action -> [o]
label :: forall o model action.
RadioGroup o model action -> o -> [View model action]
selected :: forall o model action. RadioGroup o model action -> Maybe o
onSelect :: forall o model action. RadioGroup o model action -> o -> action
name :: MisoString
options :: [a]
label :: a -> [View model action]
selected :: Maybe a
onSelect :: a -> action
..} =
        [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
fieldset_ [MisoString -> Attribute action
forall action. MisoString -> Attribute action
ariaRole_ MisoString
"radiogroup"]
            ([View model action] -> View model action)
-> [View model action] -> View model action
forall a b. (a -> b) -> a -> b
$ [a]
options
            [a] -> (a -> View model action) -> [View model action]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
o ->
                [Attribute action] -> Radio model action -> View model action
forall w model action.
Widget w model action =>
[Attribute action] -> w -> View model action
widget'
                    [Attribute action]
attrs
                    Radio
                        { MisoString
name :: MisoString
name :: MisoString
name
                        , label :: [View model action]
label = a -> [View model action]
label a
o
                        , selected :: Bool
selected = Maybe a
selected Maybe a -> Maybe a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Maybe a
forall a. a -> Maybe a
Just a
o
                        , onSelect :: action
onSelect = a -> action
onSelect a
o
                        }

    style :: Css
style =
        -- Shared style is applied in the CheckboxGroup component
        () -> Css
forall a. a -> StyleM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()