2018-03-01 02:51:49 +03:00
|
|
|
module Examples.TextArea exposing (Msg, State, example, init, update)
|
|
|
|
|
2018-05-17 03:09:17 +03:00
|
|
|
{-|
|
|
|
|
|
2018-09-26 17:02:10 +03:00
|
|
|
@docs Msg, State, example, init, update
|
2018-05-17 03:09:17 +03:00
|
|
|
|
2018-03-01 02:51:49 +03:00
|
|
|
-}
|
|
|
|
|
2018-12-14 01:52:02 +03:00
|
|
|
import Assets exposing (assets)
|
2018-03-01 02:51:49 +03:00
|
|
|
import Dict exposing (Dict)
|
2018-10-23 19:55:30 +03:00
|
|
|
import Html.Styled as Html
|
2018-03-13 12:08:07 +03:00
|
|
|
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
|
2018-05-17 03:09:17 +03:00
|
|
|
import Nri.Ui.Text.V2 as Text
|
2019-01-04 01:37:44 +03:00
|
|
|
import Nri.Ui.TextArea.V4 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 =
|
2019-01-04 01:37:44 +03:00
|
|
|
{ filename = "Nri.Ui.TextArea.v4"
|
2018-03-01 02:51:49 +03:00
|
|
|
, category = Inputs
|
|
|
|
, content =
|
2018-10-23 19:55:30 +03:00
|
|
|
[ Text.heading [ Html.text "Textarea controls" ]
|
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
|
|
|
, 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
|
|
|
, 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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, TextArea.view
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 1 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 1
|
2019-01-04 01:37:44 +03:00
|
|
|
, onBlur = Nothing
|
2018-06-20 02:36:09 +03:00
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
2018-03-01 02:51:49 +03:00
|
|
|
, label = "TextArea.view"
|
2018-05-24 04:40:27 +03:00
|
|
|
, height =
|
2018-06-20 02:36:09 +03:00
|
|
|
if state.autoResize == Checkbox.Selected then
|
2018-05-24 04:40:27 +03:00
|
|
|
TextArea.AutoResize TextArea.SingleLine
|
2018-09-26 17:02:10 +03:00
|
|
|
|
2018-05-24 04:40:27 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
, TextArea.writing
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 2 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 2
|
2019-01-04 01:37:44 +03:00
|
|
|
, onBlur = Nothing
|
2018-06-20 02:36:09 +03:00
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
2018-03-01 02:51:49 +03:00
|
|
|
, label = "TextArea.writing"
|
2018-05-24 04:40:27 +03:00
|
|
|
, height =
|
2018-06-20 02:36:09 +03:00
|
|
|
if state.autoResize == Checkbox.Selected then
|
2018-05-24 04:40:27 +03:00
|
|
|
TextArea.AutoResize TextArea.DefaultHeight
|
2018-09-26 17:02:10 +03:00
|
|
|
|
2018-05-24 04:40:27 +03:00
|
|
|
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-03-13 12:08:07 +03:00
|
|
|
, TextArea.contentCreation
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 3 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 3
|
2019-01-04 01:37:44 +03:00
|
|
|
, onBlur = Nothing
|
2018-06-20 02:36:09 +03:00
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
2018-03-13 12:08:07 +03:00
|
|
|
, label = "TextArea.contentCreation"
|
2018-05-24 04:40:27 +03:00
|
|
|
, height =
|
2018-06-20 02:36:09 +03:00
|
|
|
if state.autoResize == Checkbox.Selected then
|
2018-05-24 04:40:27 +03:00
|
|
|
TextArea.AutoResize TextArea.DefaultHeight
|
2018-09-26 17:02:10 +03:00
|
|
|
|
2018-05-24 04:40:27 +03:00
|
|
|
else
|
|
|
|
TextArea.Fixed
|
2018-03-13 12:08:07 +03:00
|
|
|
, placeholder = "Placeholder"
|
2018-06-20 02:36:09 +03:00
|
|
|
, showLabel = state.showLabel == Checkbox.Selected
|
2018-03-13 12:08:07 +03:00
|
|
|
}
|
2019-01-04 01:47:46 +03:00
|
|
|
, TextArea.writing
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 4 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 4
|
|
|
|
, onBlur = Just (InputGiven 4 "Neener neener Blur happened")
|
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
|
|
|
, label = "TextArea.writing onBlur demonstration"
|
|
|
|
, height =
|
|
|
|
if state.autoResize == Checkbox.Selected then
|
|
|
|
TextArea.AutoResize TextArea.DefaultHeight
|
|
|
|
|
|
|
|
else
|
|
|
|
TextArea.Fixed
|
|
|
|
, placeholder = "Placeholder"
|
|
|
|
, showLabel = state.showLabel == Checkbox.Selected
|
|
|
|
}
|
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
|
2018-09-26 17:02:10 +03:00
|
|
|
|
2018-06-20 02:36:09 +03:00
|
|
|
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
|