noredink-ui/styleguide-app/Debug/Control/Extra.elm

80 lines
1.8 KiB
Elm
Raw Normal View History

2021-10-22 03:20:29 +03:00
module Debug.Control.Extra exposing
2022-03-04 21:26:19 +03:00
( float, int
2021-10-22 03:20:29 +03:00
, list, listItem, optionalListItem
2022-03-04 21:42:09 +03:00
, css
2021-10-22 03:20:29 +03:00
)
{-|
2022-03-04 21:26:19 +03:00
@docs float, int
2021-10-22 03:20:29 +03:00
@docs list, listItem, optionalListItem
2022-03-04 21:42:09 +03:00
@docs css
2021-10-22 03:20:29 +03:00
-}
2022-03-04 21:42:09 +03:00
import Css
2021-10-22 03:20:29 +03:00
import Debug.Control as Control exposing (Control)
{-| -}
float : Float -> Control Float
float default =
Control.map (String.toFloat >> Maybe.withDefault default)
(Control.string (String.fromFloat default))
2022-03-04 21:26:19 +03:00
{-| -}
int : Int -> Control Int
int default =
Control.map (String.toInt >> Maybe.withDefault default)
(Control.string (String.fromInt default))
2021-10-22 03:20:29 +03:00
{-| Use with `listItem` and `optionalListItem`
2022-03-04 21:42:00 +03:00
list
|> listItem "first name" string
|> listItem "last name" string
2021-10-22 03:20:29 +03:00
-}
list : Control (List a)
list =
Control.record []
{-| -}
listItem : String -> Control a -> Control (List a) -> Control (List a)
listItem name accessor accumulator =
Control.field name
(Control.map List.singleton accessor)
(Control.map (++) accumulator)
{-| -}
2021-10-22 03:27:29 +03:00
optionalListItem : String -> Control a -> Control (List a) -> Control (List a)
2021-10-22 03:20:29 +03:00
optionalListItem name accessor accumulator =
Control.field name
2021-10-22 03:27:29 +03:00
(Control.map (List.singleton >> List.filterMap identity) (Control.maybe False accessor))
2021-10-22 03:20:29 +03:00
(Control.map (++) accumulator)
2022-03-04 21:42:09 +03:00
{-| -}
css : String -> Control (List Css.Style)
css exampleCss =
Control.map
(\rawStr ->
rawStr
|> String.split ";"
|> List.map
(\segment ->
case String.split ":" segment of
name :: value :: [] ->
Css.property name value
_ ->
-- Unable to parse css
Css.property "" ""
)
)
(Control.stringTextarea exampleCss)