noredink-ui/styleguide-app/Examples/TextArea.elm

190 lines
6.6 KiB
Elm
Raw Normal View History

2018-03-01 02:51:49 +03:00
module Examples.TextArea exposing (Msg, State, example, init, update)
{-|
@docs Msg, State, example, init, update,
2018-03-01 02:51:49 +03:00
-}
import Dict exposing (Dict)
import Html
import Html.Styled
import ModuleExample as ModuleExample exposing (Category(..), ModuleExample)
2018-06-20 02:36:09 +03:00
import Nri.Ui.AssetPath exposing (Asset(..))
2018-06-15 04:14:47 +03:00
import Nri.Ui.Checkbox.V3 as Checkbox
import Nri.Ui.Text.V2 as Text
import Nri.Ui.TextArea.V3 as TextArea
2018-03-01 02:51:49 +03:00
{-| -}
type Msg
= InputGiven Id String
| ToggleLabel Bool
| ToggleAutoResize Bool
| ToggleErrorState Bool
| NoOp
{-| -}
type alias State =
{ textValues : Dict Int String
2018-06-20 02:36:09 +03:00
, showLabel : Checkbox.IsSelected
, isInError : Checkbox.IsSelected
, autoResize : Checkbox.IsSelected
2018-03-01 02:51:49 +03:00
}
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
2018-07-25 00:12:27 +03:00
{ filename = "Nri.Ui.TextArea.V3"
2018-03-01 02:51:49 +03:00
, category = Inputs
, content =
[ Text.heading [ Html.Styled.text "Textarea controls" ]
|> Html.Styled.toUnstyled
2018-03-01 02:51:49 +03:00
, Html.div []
2018-06-20 02:36:09 +03:00
[ Checkbox.viewWithLabel assets
2018-03-01 02:51:49 +03:00
{ identifier = "show-textarea-label"
, label = "Show Label"
, setterMsg = ToggleLabel
2018-06-20 02:36:09 +03:00
, selected = state.showLabel
2018-03-01 02:51:49 +03:00
, disabled = False
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-03-01 02:51:49 +03:00
, noOpMsg = NoOp
}
2018-06-20 02:36:09 +03:00
|> Html.Styled.toUnstyled
, Checkbox.viewWithLabel assets
2018-03-01 02:51:49 +03:00
{ identifier = "textarea-autoresize"
, label = "Autoresize"
, setterMsg = ToggleAutoResize
2018-06-20 02:36:09 +03:00
, selected = state.autoResize
2018-03-01 02:51:49 +03:00
, disabled = False
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-03-01 02:51:49 +03:00
, noOpMsg = NoOp
}
2018-06-20 02:36:09 +03:00
|> Html.Styled.toUnstyled
, Checkbox.viewWithLabel assets
2018-03-01 02:51:49 +03:00
{ identifier = "textarea-isInError"
, label = "Show Error State"
, setterMsg = ToggleErrorState
2018-06-20 02:36:09 +03:00
, selected = state.isInError
2018-03-01 02:51:49 +03:00
, disabled = False
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-03-01 02:51:49 +03:00
, noOpMsg = NoOp
}
2018-06-20 02:36:09 +03:00
|> Html.Styled.toUnstyled
2018-03-01 02:51:49 +03:00
]
, TextArea.view
{ value = Maybe.withDefault "" <| Dict.get 1 state.textValues
, autofocus = False
, onInput = InputGiven 1
2018-06-20 02:36:09 +03:00
, isInError = state.isInError == Checkbox.Selected
2018-03-01 02:51:49 +03:00
, label = "TextArea.view"
, height =
2018-06-20 02:36:09 +03:00
if state.autoResize == Checkbox.Selected then
TextArea.AutoResize TextArea.SingleLine
else
TextArea.Fixed
2018-03-01 02:51:49 +03:00
, placeholder = "Placeholder"
2018-06-20 02:36:09 +03:00
, showLabel = state.showLabel == Checkbox.Selected
2018-03-01 02:51:49 +03:00
}
2018-05-18 12:20:18 +03:00
|> Html.Styled.toUnstyled
2018-03-01 02:51:49 +03:00
, TextArea.writing
{ value = Maybe.withDefault "" <| Dict.get 2 state.textValues
, autofocus = False
, onInput = InputGiven 2
2018-06-20 02:36:09 +03:00
, isInError = state.isInError == Checkbox.Selected
2018-03-01 02:51:49 +03:00
, label = "TextArea.writing"
, height =
2018-06-20 02:36:09 +03:00
if state.autoResize == Checkbox.Selected then
TextArea.AutoResize TextArea.DefaultHeight
else
TextArea.Fixed
2018-03-01 02:51:49 +03:00
, placeholder = "Placeholder"
2018-06-20 02:36:09 +03:00
, showLabel = state.showLabel == Checkbox.Selected
2018-03-01 02:51:49 +03:00
}
2018-05-18 12:20:18 +03:00
|> Html.Styled.toUnstyled
, TextArea.contentCreation
{ value = Maybe.withDefault "" <| Dict.get 3 state.textValues
, autofocus = False
, onInput = InputGiven 3
2018-06-20 02:36:09 +03:00
, isInError = state.isInError == Checkbox.Selected
, label = "TextArea.contentCreation"
, height =
2018-06-20 02:36:09 +03:00
if state.autoResize == Checkbox.Selected then
TextArea.AutoResize TextArea.DefaultHeight
else
TextArea.Fixed
, placeholder = "Placeholder"
2018-06-20 02:36:09 +03:00
, showLabel = state.showLabel == Checkbox.Selected
}
2018-05-18 12:20:18 +03:00
|> Html.Styled.toUnstyled
2018-03-01 02:51:49 +03:00
]
|> List.map (Html.map parentMessage)
}
{-| -}
init : State
init =
{ textValues = Dict.empty
2018-06-20 02:36:09 +03:00
, showLabel = Checkbox.Selected
, isInError = Checkbox.NotSelected
, autoResize = Checkbox.NotSelected
2018-03-01 02:51:49 +03:00
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
2018-06-20 02:36:09 +03:00
let
toggle bool =
if bool then
Checkbox.Selected
else
Checkbox.NotSelected
in
2018-03-01 02:51:49 +03:00
case msg of
InputGiven id newValue ->
( { state | textValues = Dict.insert id newValue state.textValues }
, Cmd.none
)
ToggleLabel bool ->
2018-06-20 02:36:09 +03:00
( { state | showLabel = toggle bool }
2018-03-01 02:51:49 +03:00
, Cmd.none
)
ToggleErrorState bool ->
2018-06-20 02:36:09 +03:00
( { state | isInError = toggle bool }
2018-03-01 02:51:49 +03:00
, Cmd.none
)
ToggleAutoResize bool ->
2018-06-20 02:36:09 +03:00
( { state | autoResize = toggle bool }
2018-03-01 02:51:49 +03:00
, Cmd.none
)
NoOp ->
( state, Cmd.none )
-- INTERNAL
type alias Id =
Int
2018-06-20 02:36:09 +03:00
assets =
{ checkboxUnchecked_svg = Asset "checkboxUnchecked_svg"
, checkboxChecked_svg = Asset "data:image/png;base64,iVBORw0KGgoAAAANSUh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artially_svg"
, iconPremiumUnlocked_png = Asset "iconPremiumUnlocked_png"
, iconPremiumLocked_png = Asset "iconPremiumLocked_png"
, checkboxLockOnInside_svg = Asset "checkboxLockOnInside_svg"
, iconPremiumKey_png = Asset "iconPremiumKey_png"
, iconPremiumFlag_svg = Asset "iconPremiumFlag_svg"
}