Adds editShowableFieldWithValidate

This function for Brick.Forms allows the user to specify an additional function
to validate input in addition to `readMaybe`, as a slightly more flexible
version of `editShowableField`.
This commit is contained in:
Ben Selfridge 2021-04-07 22:06:18 -07:00
parent 531d868323
commit f40edb025a

View File

@ -65,6 +65,7 @@ module Brick.Forms
-- * Simple form field constructors
, editTextField
, editShowableField
, editShowableFieldWithValidate
, editPasswordField
, radioField
, checkboxField
@ -83,6 +84,7 @@ module Brick.Forms
where
import Graphics.Vty hiding (showCursor)
import Control.Monad ((<=<))
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
@ -561,6 +563,32 @@ editShowableField stLens n =
renderText = txt . T.unlines
in editField stLens n limit ini val renderText id
-- | A form field using a single-line editor to edit the 'Show' representation
-- of a state field value of type @a@. This automatically uses its 'Read'
-- instance to validate the input, and also accepts an additional user-defined
-- pass for validation. This field is mostly useful in cases where the
-- user-facing representation of a value matches the 'Show' representation
-- exactly, such as with 'Int', but you don't want to accept just /any/ 'Int'.
--
-- This field responds to all events handled by 'editor', including
-- mouse events.
editShowableFieldWithValidate :: (Ord n, Show n, Read a, Show a)
=> Lens' s a
-- ^ The state lens for this value.
-> n
-- ^ The resource name for the input field.
-> (a -> Maybe a)
-- ^ Additional validation step for input.
-> s
-- ^ The initial form state.
-> FormFieldState s e n
editShowableFieldWithValidate stLens n validate =
let ini = T.pack . show
val = validate <=< readMaybe . T.unpack . T.intercalate "\n"
limit = Just 1
renderText = txt . T.unlines
in editField stLens n limit ini val renderText id
-- | A form field using an editor to edit a text value. Since the value
-- is free-form text, it is always valid.
--