nano-ui-form-0.2.0.0: lib/NanoUI/Form/Input.hs
-- | Form inputs with named or positional identity and an optional caption.
-- "NanoUI.Form" re-exports these. Use a string literal for a labelled, named
-- field, or 'unnamed' for a positional input.
module NanoUI.Form.Input
( -- * Field names
FieldName (..)
, named
, unnamed
-- * Input fields
, inputText
, inputTextWithPlaceholder
, inputPassword
, inputTextArea
, inputCheckbox
, inputSlider
, inputSelect
, inputEnumSelect
, inputRadio
, inputEnumRadio
, inputColor
-- * Static labels and separators
, formLabel
, formSeparator
-- * Errors
, errors
, childErrors
, withErrors
, withChildErrors
, withFieldErrors
)
where
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Ditto.Backend (FormError)
import Ditto.Core qualified as Ditto
import Ditto.Generalized.Named qualified as Named
import NanoUI
( Color
, TextInputConfig (..)
, checkbox'
, colorFromHex
, colorPicker'
, colorToHexA
, defaultTextInputConfig
, radio'
, respChanged
, respClicked
, select'
, slider'
, textArea'
, textInput'
, textInputConfigured'
)
import NanoUI qualified as NUI
import NanoUI.Form.Internal.Backend (FormInput (..), formInputToText)
import NanoUI.Form.Internal.Field
( decodeBool
, decodeFloatInput
, decodeInt
, enumField
, fieldErrors
, inputWidget
, Publish (..)
)
import NanoUI.Form.Types (FieldName (..), Form, FormView (..), named, unnamed)
-- | Single-line text input field.
inputText :: FormError FormInput err => FieldName -> Text -> Form err Text
inputText = textField textInput'
-- | Single-line text input field with custom placeholder text.
inputTextWithPlaceholder ::
FormError FormInput err => Text -> FieldName -> Text -> Form err Text
inputTextWithPlaceholder placeholder =
textField
(textInputConfigured' defaultTextInputConfig {ticPlaceholder = placeholder})
-- | Password text input masking entered characters.
inputPassword :: FormError FormInput err => FieldName -> Text -> Form err Text
inputPassword = textField (textInputConfigured' defaultTextInputConfig {ticPassword = True})
-- | Multi-line text area input.
inputTextArea :: FormError FormInput err => FieldName -> Text -> Form err Text
inputTextArea = textField textArea'
textField ::
FormError FormInput err =>
(NUI.Var Text -> NUI.NanoUI NUI.Response) -> FieldName -> Text -> Form err Text
textField widget name = captioned name formInputToText FormInputText widget
-- | Checkbox toggle input.
inputCheckbox :: FormError FormInput err => FieldName -> Bool -> Form err Bool
inputCheckbox name initial =
inputWidget
(fieldKey name)
(Right . decodeBool initial)
(OnChangeOr respClicked)
FormInputBool
(checkbox' (fromMaybe "" (fieldLabel name)))
initial
-- | Floating-point slider input across the range @[minV, maxV]@.
inputSlider ::
FormError FormInput err =>
FieldName -> Float -> Float -> Float -> Form err Float
inputSlider name minV maxV initial =
captioned name (decodeFloatInput initial) FormInputFloat (slider' minV maxV) initial
-- | Dropdown selection in fold order (returns selected index).
inputSelect ::
(Foldable f, FormError FormInput err) =>
FieldName -> f Text -> Int -> Form err Int
inputSelect name options initial =
captioned name (decodeInt initial) FormInputInt (select' options) initial
-- | Dropdown selection for any bounded enumeration type.
inputEnumSelect ::
forall a err.
(Bounded a, Enum a, Show a, FormError FormInput err) =>
FieldName -> a -> Form err a
inputEnumSelect name = enumField (inputSelect name)
-- | Radio button group (returns selected index).
inputRadio ::
(Foldable f, FormError FormInput err) =>
FieldName -> f Text -> Int -> Form err Int
inputRadio name options initial =
captioned name (decodeInt initial) FormInputInt (radio' options) initial
-- | Radio button group for any bounded enumeration type.
inputEnumRadio ::
forall a err.
(Bounded a, Enum a, Show a, FormError FormInput err) =>
FieldName -> a -> Form err a
inputEnumRadio name = enumField (inputRadio name)
-- | Color picker input. As with 'colorPicker'', an edit makes the colour
-- opaque.
inputColor :: FormError FormInput err => FieldName -> Color -> Form err Color
inputColor name initial = captioned name decode (FormInputText . colorToHexA) colorPicker' initial
where
decode = \case
FormInputText t -> fromMaybe initial (colorFromHex t)
_ -> initial
-- | A field with its optional caption drawn above the control. Edits to the
-- control count as changes.
captioned ::
(Eq a, FormError FormInput err) =>
FieldName -> (FormInput -> a) -> (a -> FormInput) -> (NUI.Var a -> NUI.NanoUI NUI.Response) -> a -> Form err a
captioned name decode encode widget =
inputWidget (fieldKey name) (Right . decode) (OnChangeOr respChanged) encode $ \value ->
mapM_ NUI.label (fieldLabel name) >> widget value
-- | Static label inside a form.
formLabel :: Text -> Form err ()
formLabel txt = Ditto.view (FormView (NUI.label txt))
-- | Visual separator line inside a form.
formSeparator :: Form err ()
formSeparator = Ditto.view (FormView NUI.separator)
-- | Render error messages originating directly from this form node.
errors :: ([err] -> FormView) -> Form err ()
errors = Named.errors
-- | Render error messages originating from this form node and any descendant nodes.
childErrors :: ([err] -> FormView) -> Form err ()
childErrors = Named.childErrors
-- | Wrap a form with a custom error handler for its direct errors.
withErrors :: (FormView -> [err] -> FormView) -> Form err a -> Form err a
withErrors = Named.withErrors
-- | Wrap a form with a custom error handler for errors from it or any child.
withChildErrors :: (FormView -> [err] -> FormView) -> Form err a -> Form err a
withChildErrors = Named.withChildErrors
-- | Automatically display validation errors directly below the widget.
withFieldErrors :: Form Text a -> Form Text a
withFieldErrors = withChildErrors fieldErrors