Elm 单选按钮反映模型

Elm 单选按钮反映模型,elm,Elm,在中,初始的fontSize为Medium。这反映在字体的大小上,但未选择“中等”单选按钮。是否有办法使单选按钮始终反映当前的fontSize?这就是问题中的代码: view : Model -> Html Msg view model = div [] [ fieldset [] [ radio "Small" (SwitchTo Small) , radio "Medium" (SwitchTo Medium) , radio

在中,初始的
fontSize
Medium
。这反映在字体的大小上,但未选择“中等”单选按钮。是否有办法使单选按钮始终反映当前的
fontSize

这就是问题中的代码:

view : Model -> Html Msg
view model =
  div []
    [ fieldset []
        [ radio "Small" (SwitchTo Small)
        , radio "Medium" (SwitchTo Medium)
        , radio "Large" (SwitchTo Large)
        ]
    , Markdown.toHtml [ sizeToStyle model.fontSize ] model.content
    ]


radio : String -> msg -> Html msg
radio value msg =
  label
    [ style [("padding", "20px")]
    ]
    [ input [ type_ "radio", name "font-size", onClick msg ] []
    , text value
    ]
这是用于呈现无线电输入的线条:

input [ type_ "radio", name "font-size", onClick msg ] []
收音机()有一个
选中的
属性,因此看起来您可以根据当前字体大小添加它?比如:

radio : String -> Bool -> msg -> Html msg
radio value isChecked msg =
  label
    [ style [("padding", "20px")]
    ]
    [ input [ type_ "radio", name "font-size", checked isChecked, onClick msg ] []
    , text value
    ]

…然后根据
视图
函数中的模型设置
isChecked
参数。

这就是您要找的吗

view : Model -> Html Msg
view model =
  div []
    [ fieldset []
        [ radio "Small" (model.fontSize == Small) (SwitchTo Small)
        , radio "Medium"(model.fontSize == Medium)  (SwitchTo Medium)
        , radio "Large" (model.fontSize == Large) (SwitchTo Large)
        ]
    , Markdown.toHtml [ sizeToStyle model.fontSize ] model.content
    ]


radio : String -> Bool > msg -> Html msg
radio value isChecked msg =
  label
    [ style [("padding", "20px")]
    ]
    [ input [ type_ "radio", name "font-size", onInput msg, checked isChecked ] []
    , text value
    ]
注意,我将onClick改为onInput,我认为这对于表单选择是更好的实践

另外,我倾向于将msg参数放在类型签名的开头,因为它最不可能是函数管道的一部分:

radio : msg -> Bool -> String -> Html msg
为什么这位官员不谈论切换设置?这是相当令人沮丧的是,看到指南不完整。。。