2020-03-31 22:43:32 +03:00
|
|
|
module Examples.TextArea exposing (Msg, State, example)
|
2018-03-01 02:51:49 +03:00
|
|
|
|
2018-05-17 03:09:17 +03:00
|
|
|
{-|
|
|
|
|
|
2020-03-31 22:43:32 +03:00
|
|
|
@docs Msg, State, example
|
2018-05-17 03:09:17 +03:00
|
|
|
|
2018-03-01 02:51:49 +03:00
|
|
|
-}
|
|
|
|
|
2020-06-19 23:41:28 +03:00
|
|
|
import AtomicDesignType exposing (AtomicDesignType(..))
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2018-03-01 02:51:49 +03:00
|
|
|
import Dict exposing (Dict)
|
2020-03-31 23:20:03 +03:00
|
|
|
import Example exposing (Example)
|
2018-10-23 19:55:30 +03:00
|
|
|
import Html.Styled as Html
|
2018-06-20 02:36:09 +03:00
|
|
|
import Nri.Ui.AssetPath exposing (Asset(..))
|
2019-06-04 01:17:39 +03:00
|
|
|
import Nri.Ui.Checkbox.V5 as Checkbox
|
2019-07-23 17:53:43 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
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
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
2020-03-31 23:20:03 +03:00
|
|
|
example : Example State Msg
|
2020-03-31 22:43:32 +03:00
|
|
|
example =
|
2019-05-03 19:56:43 +03:00
|
|
|
{ name = "Nri.Ui.TextArea.V4"
|
2020-03-31 22:43:32 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = \_ -> Sub.none
|
2020-03-31 22:43:32 +03:00
|
|
|
, categories = [ Inputs ]
|
2020-06-19 23:41:28 +03:00
|
|
|
, atomicDesignType = AtomicDesignType.Molecule
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
|
|
|
[ Heading.h1 [] [ Html.text "Textarea controls" ]
|
|
|
|
, Html.div []
|
|
|
|
[ Checkbox.viewWithLabel
|
|
|
|
{ identifier = "show-textarea-label"
|
|
|
|
, label = "Show Label"
|
|
|
|
, setterMsg = ToggleLabel
|
|
|
|
, selected = state.showLabel
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "textarea-autoresize"
|
|
|
|
, label = "Autoresize"
|
|
|
|
, setterMsg = ToggleAutoResize
|
|
|
|
, selected = state.autoResize
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "textarea-isInError"
|
|
|
|
, label = "Show Error State"
|
|
|
|
, setterMsg = ToggleErrorState
|
|
|
|
, selected = state.isInError
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, TextArea.view
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 1 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 1
|
|
|
|
, onBlur = Nothing
|
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
|
|
|
, label = "TextArea.view"
|
|
|
|
, height =
|
|
|
|
if state.autoResize == Checkbox.Selected then
|
|
|
|
TextArea.AutoResize TextArea.SingleLine
|
|
|
|
|
|
|
|
else
|
|
|
|
TextArea.Fixed
|
|
|
|
, placeholder = "Placeholder"
|
|
|
|
, showLabel = state.showLabel == Checkbox.Selected
|
2018-03-01 02:51:49 +03:00
|
|
|
}
|
2020-03-31 22:43:32 +03:00
|
|
|
, TextArea.writing
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 2 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 2
|
|
|
|
, onBlur = Nothing
|
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
|
|
|
, label = "TextArea.writing"
|
|
|
|
, 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
|
|
|
}
|
2020-03-31 22:43:32 +03:00
|
|
|
, TextArea.contentCreation
|
|
|
|
{ value = Maybe.withDefault "" <| Dict.get 3 state.textValues
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = InputGiven 3
|
|
|
|
, onBlur = Nothing
|
|
|
|
, isInError = state.isInError == Checkbox.Selected
|
|
|
|
, label = "TextArea.contentCreation"
|
|
|
|
, height =
|
|
|
|
if state.autoResize == Checkbox.Selected then
|
|
|
|
TextArea.AutoResize TextArea.DefaultHeight
|
|
|
|
|
|
|
|
else
|
|
|
|
TextArea.Fixed
|
|
|
|
, placeholder = "Placeholder"
|
|
|
|
, showLabel = state.showLabel == Checkbox.Selected
|
|
|
|
}
|
|
|
|
, 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
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
|
|
|
type alias Id =
|
|
|
|
Int
|